Learn R Programming

SFSI (version 1.2.0)

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, a = 1, void = FALSE)

Value

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

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

a

(numeric) A number to multiply the whole resulting matrix by. Default a=1

Author

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

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 and covariances as

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

where sd(Xi)=sqrt(var(Xi)); 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.

Examples

Run this code
  require(SFSI)
  data(wheatHTP)
  
  X = scale(Y[,4:7])
  (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
  

Run the code above in your browser using DataLab