flexIC: Minimal, Runnable Demo

Kevin E. Wells, PhD

2025-05-15

1 Simulate marginals

n <- 200; k <- 5
Sigma <- matrix(0.6, k, k); diag(Sigma) <- 1
X0  <- mvtnorm::rmvnorm(n, sigma = Sigma)      # raw data
R_star <- cor(X0, method = "spearman")         # target rank‑R

2 IC (m = 1) vs flexIC (m = 50)

## 2  IC (m = 1) vs flexIC (m = 20)

out_ic   <- flexIC(X0, R_star, m = 1 ,  eps = 0   )   # classic IC
out_flex <- flexIC(X0, R_star, m = 20, eps = 0.02)   # flexIC

X_ic   <- out_ic[[1]]
X_flex <- out_flex[[1]]



if (is.null(dim(X_ic)))   X_ic   <- matrix(X_ic  , nrow = n, ncol = k)
if (is.null(dim(X_flex))) X_flex <- matrix(X_flex, nrow = n, ncol = k)

3 Maximum rank‑error

err_ic   <- max(abs(cor(X_ic  , method = "spearman") - R_star))
err_flex <- max(abs(cor(X_flex, method = "spearman") - R_star))

data.frame(
  method         = c("IC (m = 1)", "flexIC (m = 50)"),
  max_rank_error = c(err_ic, err_flex)
)
#>            method max_rank_error
#> 1      IC (m = 1)     0.03500488
#> 2 flexIC (m = 50)     0.03500488

4 Speed benchmark

microbenchmark(
  IC     = flexIC(X0, R_star, m = 1 ,  eps = 0   ),
  flex50 = flexIC(X0, R_star, m = 50, eps = 0.02),
  times  = 100L
)
#> Unit: microseconds
#>    expr     min       lq      mean  median       uq     max neval cld
#>      IC   946.3  1011.10  1165.532  1124.9  1179.65  5573.9   100  a 
#>  flex50 51406.9 55061.65 57436.427 57485.4 59910.70 67847.4   100   b

5 Session info

sessionInfo()
#> R version 4.4.2 (2024-10-31 ucrt)
#> Platform: x86_64-w64-mingw32/x64
#> Running under: Windows 11 x64 (build 22631)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=C                          
#> [2] LC_CTYPE=English_United States.utf8   
#> [3] LC_MONETARY=English_United States.utf8
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.utf8    
#> 
#> time zone: America/Chicago
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] microbenchmark_1.5.0 mvtnorm_1.3-3        flexIC_0.1.3        
#> 
#> loaded via a namespace (and not attached):
#>  [1] cli_3.6.3         knitr_1.49        TH.data_1.1-2     rlang_1.1.4      
#>  [5] xfun_0.50         jsonlite_1.8.9    zoo_1.8-12        htmltools_0.5.8.1
#>  [9] sass_0.4.9        rmarkdown_2.29    grid_4.4.2        evaluate_1.0.3   
#> [13] jquerylib_0.1.4   MASS_7.3-61       fastmap_1.2.0     yaml_2.3.10      
#> [17] lifecycle_1.0.4   compiler_4.4.2    multcomp_1.4-26   codetools_0.2-20 
#> [21] sandwich_3.1-1    rstudioapi_0.17.1 lattice_0.22-6    digest_0.6.37    
#> [25] R6_2.5.1          splines_4.4.2     bslib_0.8.0       Matrix_1.7-1     
#> [29] tools_4.4.2       survival_3.7-0    cachem_1.1.0