| Title: | Flexible Rank-Preserving Correlation Engine |
|---|---|
| Description: | Implements a fast, flexible method for simulating continuous variables with specified rank correlations using the Iman–Conover transformation (Iman & Conover, 1982 <doi:10.1080/03610918208812265>) and back-ranking. Includes plotting tools and error-diagnostics. |
| Authors: | Kevin Wells [aut, cre] |
| Maintainer: | Kevin Wells <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.4 |
| Built: | 2026-06-04 09:46:46 UTC |
| Source: | https://github.com/theotherdrwells/flexic |
Applies a rank-based correlation structure to a numeric matrix using
a flexible, iterative variant of the Iman–Conover algorithm.
The method reorders each column of x based on the rank structure
of a multivariate normal draw whose correlation matrix matches target_r.
If eps is specified, the algorithm will iteratively draw candidates and
select the one with the closest match to the target Spearman structure.
The marginal distributions of x are preserved exactly.
flexIC(x, target_r, eps = "none", max_iter = 20)flexIC(x, target_r, eps = "none", max_iter = 20)
x |
Numeric matrix or data frame. Columns should be independent prior to transformation. |
target_r |
Target Spearman correlation matrix to impose. Must be square, symmetric, and positive-definite. |
eps |
Convergence tolerance (maximum absolute deviation allowed between achieved and target Spearman correlation).
If |
max_iter |
Maximum number of candidate draws to evaluate when |
A numeric matrix with same dimensions as x, with transformed columns preserving marginal distributions
and approximately matching the specified rank correlation structure.
set.seed(1) x <- cbind(rexp(100), rbinom(100, 5, 0.4)) R_target <- matrix(c(1, 0.6, 0.6, 1), 2) out <- flexIC(x, R_target, eps = 0.02, max_iter = 50) cor(out, method = "spearman")set.seed(1) x <- cbind(rexp(100), rbinom(100, 5, 0.4)) R_target <- matrix(c(1, 0.6, 0.6, 1), 2) out <- flexIC(x, R_target, eps = 0.02, max_iter = 50) cor(out, method = "spearman")
Applies the classic Iman–Conover procedure to reorder the columns of a numeric matrix to approximately match a target rank correlation structure, while preserving marginals.
ic_exact(x, target_r)ic_exact(x, target_r)
x |
A numeric matrix or data frame with independent columns (desired marginals). |
target_r |
A square, positive-definite correlation matrix to impose. |
A numeric matrix with the same marginal distributions as x and
approximately matching the target Spearman correlation.
set.seed(123) x <- matrix(rnorm(300), ncol = 3) R_target <- matrix(c(1, 0.5, 0.3, 0.5, 1, 0.4, 0.3, 0.4, 1), 3) out <- ic_exact(x, R_target) cor(out, method = "spearman")set.seed(123) x <- matrix(rnorm(300), ncol = 3) R_target <- matrix(c(1, 0.5, 0.3, 0.5, 1, 0.4, 0.3, 0.4, 1), 3) out <- ic_exact(x, R_target) cor(out, method = "spearman")
Facetted histograms of marginals before and after flexIC
original |
Matrix or data frame of the original variables. |
flex_out |
Either the list returned by |
bins |
Number of histogram bins. |
after_lab |
Facet-strip label for the post-flexIC panel. |
A ggplot object (returned invisibly).
set.seed(1) x <- matrix(rnorm(300), ncol = 3) target <- cor(x, method = "spearman") fo <- flexIC(x, target, eps = 0.02, max_iter = 5) plot_marginals_grid(x, fo, bins = 30)set.seed(1) x <- matrix(rnorm(300), ncol = 3) target <- cor(x, method = "spearman") fo <- flexIC(x, target, eps = 0.02, max_iter = 5) plot_marginals_grid(x, fo, bins = 30)