2025-01-23-correlated-se

Author

Gibran Hemani

Published

March 20, 2025

Background

The standard error in a region is

\[ cov(\hat{\beta}) = S \rho S \]

where \(S\) is the diagonal matrix of standard errors, \(\rho\) is the LD correlation matrix.

Quite slow to calculate, how to speed it up?

library(randcorr)
library(exvatools)
library(microbenchmark)

rho <- randcorr(2000)
hist(rho)

S <- diag(runif(2000))

se1 <- S %*% rho %*% S
se2 <- multd(dmult(S, rho), S)

cor(c(se1), c(se2))
[1] 1
microbenchmark(se1 <- S %*% rho %*% S, se2 <- multd(dmult(S, rho), S), times = 5)
Warning in microbenchmark(se1 <- S %*% rho %*% S, se2 <- multd(dmult(S, : less
accurate nanosecond times to avoid potential integer overflows
Unit: milliseconds
                           expr       min         lq       mean     median
         se1 <- S %*% rho %*% S 5246.6404 5371.90881 5540.00547 5414.32240
 se2 <- multd(dmult(S, rho), S)   60.4561   86.70983   90.28943   95.21467
        uq       max neval cld
 5784.9195 5882.2362     5  a 
  102.2686  106.7979     5   b

sessionInfo()
R version 4.4.3 (2025-02-28)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.6.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/London
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] microbenchmark_1.5.0 exvatools_0.9.0      randcorr_1.0        

loaded via a namespace (and not attached):
 [1] cli_3.6.3         knitr_1.48        rlang_1.1.4       TH.data_1.1-2    
 [5] xfun_0.48         jsonlite_1.8.9    zoo_1.8-12        htmltools_0.5.8.1
 [9] rmarkdown_2.27    grid_4.4.3        evaluate_1.0.1    MASS_7.3-64      
[13] fastmap_1.2.0     yaml_2.3.10       mvtnorm_1.2-5     compiler_4.4.3   
[17] multcomp_1.4-26   codetools_0.2-20  sandwich_3.1-1    htmlwidgets_1.6.4
[21] lattice_0.22-6    digest_0.6.37     splines_4.4.3     Matrix_1.7-2     
[25] tools_4.4.3       survival_3.8-3