2. Covariance matrix transformations | R Documentation |
Transformation into correlation matrix or distance matrix from a covariance matrix
cov2dist(A, a = 1, inplace = FALSE)
cov2cor2(A, a = 1, inplace = FALSE)
A |
(numeric matrix) Variance-covariance matrix |
inplace |
|
a |
(numeric) A number to multiply the whole resulting matrix by. Default |
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.
Function 'cov2dist' returns a matrix containing the Euclidean distances. Function 'cov2cor2' returns a correlation matrix
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.