Speed up covariance calculation for large matrices. The
default behavior is the same as cov ('pearson',
no NA handling).
Arguments
- x
a numeric vector, matrix or data frame; a matrix is highly recommended to maximize the performance
- y
NULL (default) or a vector, matrix or data frame with compatible dimensions to x; the default is equivalent to
y = x- col_x
integers indicating the subset indices (columns) of
xto calculate the covariance, orNULLto include all the columns; default isNULL- col_y
integers indicating the subset indices (columns) of
yto calculate the covariance, orNULLto include all the columns; default isNULL- df
a scalar indicating the degrees of freedom; default is
nrow(x)-1
Value
A covariance matrix of x and y. Note that there is no
NA handling. Any missing values will lead to NA in the
resulting covariance matrices.
Examples
# Set ncores = 2 to comply to CRAN policy. Please don't run this line
ravetools_threads(n_threads = 2L)
x <- matrix(rnorm(400), nrow = 100)
# Call `cov(x)` to compare
fast_cov(x)
#> [,1] [,2] [,3] [,4]
#> [1,] 1.08752344 -0.05411074 -0.10165651 0.1748222
#> [2,] -0.05411074 1.12428420 -0.02034545 -0.1443692
#> [3,] -0.10165651 -0.02034545 0.91685597 -0.1479509
#> [4,] 0.17482223 -0.14436922 -0.14795088 1.1772617
# Calculate covariance of subsets
fast_cov(x, col_x = 1, col_y = 1:2)
#> [,1] [,2]
#> [1,] 1.087523 -0.05411074
# \donttest{
# Speed comparison, better to use multiple cores (4, 8, or more)
# to show the differences.
ravetools_threads(n_threads = -1)
x <- matrix(rnorm(100000), nrow = 1000)
microbenchmark::microbenchmark(
fast_cov = {
fast_cov(x, col_x = 1:50, col_y = 51:100)
},
cov = {
cov(x[,1:50], x[,51:100])
},
unit = 'ms', times = 10
)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> fast_cov 1.352285 1.359488 1.468069 1.405478 1.520448 1.913171 10
#> cov 5.426709 5.443450 5.618673 5.461329 5.481492 7.051222 10
# }