Skip to contents

Calculate a correlation matrix from a sparse matrix.

Usage

cor_sparse(x)

Arguments

x

A sparse matrix (e.g., Matrix::dgCMatrix).

Value

dsyMatrix

Details

Let \(X \in \mathbb{R}^{n \times p}\) with column sums \(s\) and means \(m = s/n\). The centered sum-of-products is $$S = X^\top X - \frac{s s^\top}{n} = X^\top X - n m m^\top.$$ The correlation matrix is \(R = S / (\sigma \sigma^\top)\) with \(\sigma = \sqrt{\operatorname{diag}(S)}\). Any common normalization factor (e.g., dividing by \(n\) or \(n-1\)) cancels in \(R\).

References

https://stackoverflow.com/a/45655597

Examples

#----------------------------------------------------------------------------
# cor_sparse() examples
#----------------------------------------------------------------------------
library(bkstat)

x <- sample(0:10, 1e7, replace = TRUE, p = c(0.99, rep(0.001, 10)))
x <- Matrix::Matrix(x, ncol = 5)

cor_sparse(x)
#> 5 x 5 Matrix of class "dsyMatrix"
#>               [,1]          [,2]          [,3]          [,4]          [,5]
#> [1,]  1.0000000000  1.018741e-03 -1.990573e-04  0.0006211965 -0.0007504959
#> [2,]  0.0010187412  1.000000e+00  8.712487e-05 -0.0007897415  0.0011169559
#> [3,] -0.0001990573  8.712487e-05  1.000000e+00 -0.0001096579  0.0003227664
#> [4,]  0.0006211965 -7.897415e-04 -1.096579e-04  1.0000000000  0.0006899934
#> [5,] -0.0007504959  1.116956e-03  3.227664e-04  0.0006899934  1.0000000000