Author: Kevin E. Wells, PhD
Date: 2025-06-26

# flexIC: Minimal, Runnable Demo

#### Kevin E. Wells, PhD

#### 2025-06-26

## 1 Simulate marginals

    n <- 200; k <- 5
    Sigma <- matrix(0.6, k, k); diag(Sigma) <- 1
    X0  <- mvtnorm::rmvnorm(n, sigma = Sigma)
    R_star <- cor(X0, method = "spearman")

## 2 IC vs flexIC

    out_ic   <- flexIC(X0, R_star, eps = "none")
    out_flex <- flexIC(X0, R_star, eps = 0.02, max_iter = 50)
    X_ic     <- out_ic
    X_flex   <- out_flex

## 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 (eps = 'none')", "flexIC (eps = 0.02)"),
      max_rank_error = c(err_ic, err_flex)
    )
    #>                method max_rank_error
    #> 1   IC (eps = 'none')     0.09301133
    #> 2 flexIC (eps = 0.02)     0.04386710

## 4 Speed benchmark

    microbenchmark(
      IC     = flexIC(X0, R_star, eps = "none"),
      flexIC = flexIC(X0, R_star, eps = 0.02, max_iter = 50),
      times  = 100L
    )
    #> Unit: microseconds
    #>    expr    min       lq      mean   median       uq     max neval cld
    #>      IC  751.7   844.40  1021.888   903.95   931.85  6931.9   100  a
    #>  flexIC 4392.6 17940.95 19571.516 19111.20 19721.50 34530.2   100   b

## 5 Optional: Visual check

    if (requireNamespace("ggplot2", quietly = TRUE)) {
      flexIC::plot_marginals_grid(X0, X_flex, bins = 30)
    }
    #> Showing first 4 of 5 variables.

## 6 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.4
    #>
    #> loaded via a namespace (and not attached):
    #>  [1] Matrix_1.7-1      gtable_0.3.6      jsonlite_1.8.9    dplyr_1.1.4
    #>  [5] compiler_4.4.2    tidyselect_1.2.1  jquerylib_0.1.4   splines_4.4.2
    #>  [9] scales_1.3.0      yaml_2.3.10       fastmap_1.2.0     lattice_0.22-6
    #> [13] TH.data_1.1-2     ggplot2_3.5.2     R6_2.5.1          labeling_0.4.3
    #> [17] generics_0.1.3    knitr_1.49        MASS_7.3-61       tibble_3.2.1
    #> [21] munsell_0.5.1     bslib_0.8.0       pillar_1.10.1     rlang_1.1.4
    #> [25] multcomp_1.4-26   cachem_1.1.0      xfun_0.50         sass_0.4.9
    #> [29] cli_3.6.3         withr_3.0.2       magrittr_2.0.3    digest_0.6.37
    #> [33] grid_4.4.2        rstudioapi_0.17.1 sandwich_3.1-1    lifecycle_1.0.4
    #> [37] vctrs_0.6.5       evaluate_1.0.3    glue_1.8.0        farver_2.1.2
    #> [41] codetools_0.2-20  zoo_1.8-12        survival_3.7-0    colorspace_2.1-1
    #> [45] rmarkdown_2.29    tools_4.4.2       pkgconfig_2.0.3   htmltools_0.5.8.1