covariance_matrix: Conversion of a covariance matrix to a distance/correlation...

2. Covariance matrix transformationsR Documentation

Conversion of a covariance matrix to a distance/correlation matrix

Description

Transformation into correlation matrix or distance matrix from a covariance matrix

Usage

cov2dist(A, a = 1, inplace = FALSE)

cov2cor2(A, a = 1, inplace = FALSE)

Arguments

A

(numeric matrix) Variance-covariance matrix

inplace

TRUE or FALSE to whether operate directly on the input matrix. When TRUE no result is produced but the input A is modified. Default inplace=FALSE

a

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

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 Euclidean distance d(Xi,Xj) between the variables expressed in terms of cross-products is

d(Xi,Xj) = (x'ixi + x'jxj - 2x'ixj)1/2

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

d(Xi,Xj) = (var(Xi) + var(Xj) - 2cov(Xi,Xj))1/2

while in the diagonal, the distance between one variable with itself is d(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.

Value

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

Examples

  require(SFSI)
  data(wheatHTP)
  
  X = scale(M)[1:100,]/sqrt(ncol(M))
  A = tcrossprod(X)        # A 100x100 covariance matrix 
  
  # Covariance matrix to distance matrix
  D = cov2dist(A)
  # (it must equal (but faster) to:)
  D0 = as.matrix(dist(X))
  max(D-D0)
  
  # Covariance to a correlation matrix
  R = cov2cor2(A)
  # (it must equal (but faster) to:)
  R0 = cov2cor(A)
  max(R-R0)
  
  # Inplace calculation
  A[1:5,1:5]
  cov2dist(A, inplace=TRUE)
  A[1:5,1:5]  # notice that A was modified
  

SFSI documentation built on Sept. 11, 2024, 9:11 p.m.