R/dtmcorr.R

Defines functions dtmcorr

Documented in dtmcorr

#' @title Pearson Correlation for Sparse Matrices
#' @description Pearson Correlation for Sparse Matrices.
#' More memory and time-efficient than \code{cor(as.matrix(x))}.
#' @param x A matrix, potentially a sparse matrix such as a "dgCMatrix" object
#' @return a correlation matrix
#' @export
dtmcorr <- function(x) {
  n <- nrow(x)
  covmat <- (as.matrix(Matrix::crossprod(x)) - n * Matrix::tcrossprod(Matrix::colMeans(x))) / (n - 1)
  cormat <- covmat / Matrix::tcrossprod(sqrt(Matrix::diag(covmat)))
  cormat
}

Try the LDABiplots package in your browser

Any scripts or data that you put into this service are public.

LDABiplots documentation built on July 18, 2022, 5:06 p.m.