R/utils.R
sparse_cor.Rd
Safe correlation function which returns a sparse matrix without missing values
sparse_cor(
x,
y = NULL,
method = "pearson",
allow_neg = TRUE,
remove_na = TRUE,
remove_inf = TRUE,
...
)
Sparse matrix or character vector.
Sparse matrix or character vector.
Method to use for calculating the correlation coefficient.
Logical. Whether to allow negative values or set them to 0.
Logical. Whether to replace NA values with 0.
Logical. Whether to replace infinite values with 1.
Other arguments passed to cor
function.
A correlation matrix.
if (FALSE) { # \dontrun{
m1 <- simulate_sparse_matrix(
2000, 2000,
density = 0.01
)
m2 <- simulate_sparse_matrix(
2000, 1000,
density = 0.05
)
all.equal(
as_matrix(sparse_cor(m1)),
as_matrix(cor(as_matrix(m1)))
)
all.equal(
as_matrix(sparse_cor(m1, m2)),
as_matrix(cor(as_matrix(m1), as_matrix(m2)))
)
system.time(
sparse_cor(m1)
)
system.time(
cor(as_matrix(m1))
)
system.time(
sparse_cor(m1, m2)
)
system.time(
cor(as_matrix(m1), as_matrix(m2))
)
m1[sample(1:500, 10)] <- NA
m2[sample(1:500, 10)] <- NA
sparse_cor(m1, m2)[1:5, 1:5]
} # }