Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

covariance_matrix

Conversion of a covariance matrix to a distance or correlation matrix


Description

Computes a correlation matrix or a Euclidean distance matrix from a covariance matrix

Usage

cov2dist(V, void = FALSE)

cov2cor2(V, void = FALSE)

Arguments

V

(numeric matrix) Symmetric variance-covariance matrix among p variables. It can be of the "float32" type as per the 'float' R-package

void

TRUE or FALSE to whether return or not return the output. When FALSE no result is displayed but the input V is modified. Default void=FALSE

Details

For any variables Xi and Xj with mean zero and with sample vectors xi = (xi1,...,xin)' and xj = (xj1,...,xjn)' , their (sample) variances are equal (up-to a constant) to their cross-products, this is, var(Xi) = x'ixi and var(Xj) = x'jxj. Likewise, the covariance is cov(Xi,Xj) = x'ixj.

Distance.

The square of the distance d(Xi,Xj) between the variables expressed in terms of cross-products is

d2(Xi,Xj) = x'ixi + x'jxj - 2x'ixj

Therefore, the output (square) distance matrix will contain as off-diagonal entries

d2(Xi,Xj) = var(Xi) + var(Xj) - 2cov(Xi,Xj)

while in the diagonal, the distance between one variable with itself is d2(Xi,Xi) = 0

Correlation.

The correlation between the variables is obtained from variances y covariances as

cor(Xi,Xj) = cov(Xi,Xj)/(var(Xi)var(Xj))

while in the diagonal, the correlation between one variable with itself is cor(Xi,Xi) = 1

Variances are obtained from the diagonal values while covariances are obtained from the out-diagonal.

Value

Function 'cov2dist' returns a matrix containing the (square) Euclidean distances. Function 'cov2cor2' returns a correlation matrix

Author(s)

Marco Lopez-Cruz (maraloc@gmail.com) and Gustavo de los Campos

Examples

require(SFSI)
  
  # Simulate matrix
  n = 100; p=5
  X = scale(matrix(rnorm(n*p),ncol=p),FALSE,rep(1,p))
  (V = crossprod(X))             # Covariance matrix 
  
  # Covariance matrix to distance matrix
  (D1 = cov2dist(V))
  # it must equal (but faster) to:
  D0 = as.matrix(dist(t(X)))^2
  max(abs(D0-D1))
  
  # Covariance to a correlation matrix
  (R1 = cov2cor2(V))
  # it must equal (but faster) to:
  R0 = cov2cor(V)
  max(abs(R0-R1))
  
  if(requireNamespace("float")){
   # Using a 'float' type variable
   V2 = float::fl(V)
   D2 = cov2dist(V2)
   max(abs(D1-D2))   # discrepancy with previous matrix
   R2 = cov2cor2(V2)
   max(abs(R1-R2))   # discrepancy with previous matrix
  }
  
  # Using void=TRUE
  cov2dist(V,void=TRUE)
  V       # notice that V was modified
  cov2dist(V2,void=TRUE)
  V2       # notice that V2 was modified

SFSI

Sparse Family and Selection Index

v0.3.0
GPL-3
Authors
Marco Lopez-Cruz [aut, cre], Gustavo de los Campos [aut], Paulino Perez-Rodriguez [ctb]
Initial release
2021-04-29

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.