Conversion of a covariance matrix to a distance or correlation matrix
Computes a correlation matrix or a Euclidean distance matrix from a covariance matrix
cov2dist(V, void = FALSE) cov2cor2(V, void = FALSE)
V |
(numeric matrix) Symmetric variance-covariance matrix among p variables. It can be of the "float32" type as per the 'float' R-package |
void |
|
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.
The square of the distance d(Xi,Xj) between the variables expressed in terms of cross-products is
Therefore, the output (square) distance matrix will contain as off-diagonal entries
while in the diagonal, the distance between one variable with itself is d2(Xi,Xi) = 0
The correlation between the variables is obtained from variances y covariances as
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.
Function 'cov2dist' returns a matrix containing the (square) Euclidean distances. Function 'cov2cor2' returns a correlation matrix
Marco Lopez-Cruz (maraloc@gmail.com) and Gustavo de los Campos
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
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.