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,
  ...
)

Arguments

x

Sparse matrix or character vector.

y

Sparse matrix or character vector.

method

Method to use for calculating the correlation coefficient.

allow_neg

Logical. Whether to allow negative values or set them to 0.

remove_na

Logical. Whether to replace NA values with 0.

remove_inf

Logical. Whether to replace infinite values with 1.

...

Other arguments passed to cor function.

Value

A correlation matrix.

Examples

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]
} # }