fast_cov | R Documentation |
Speed up covariance calculation for large matrices. The
default behavior is the same as cov
('pearson'
,
no NA
handling).
fast_cov(x, y = NULL, col_x = NULL, col_y = NULL, df = NA)
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 |
col_x |
integers indicating the subset indices (columns) of |
col_y |
integers indicating the subset indices (columns) of |
df |
a scalar indicating the degrees of freedom; default is
|
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.
# 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)
# Calculate covariance of subsets
fast_cov(x, col_x = 1, col_y = 1:2)
# 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
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.