# Using the covariance matrix to calculate correlations

I'm a bit lost here. I have a matrix of response variables, $y$, and I fit a model to account for a number of predictor variables, say $x_1$, $x_2$ and $x_3$:

lm.m <- lm( y ~ x1 + x2 + x3 )


I can then extract the estimated covariance matrix of the residuals (if I understand correctly), for example using the estVar function from R:

c <- estVar( lm.m )


What should I do to turn this into a correlation matrix? Divide each cell by the product of square roots variances of the residuals? Or the original variables? I'm confused.

What I'm trying to achieve is to calculate the correlation matrix of the residuals from the model. Something that could be achieved by

y2 <- apply( y, 2, function( yy ) lm( yy ~ x1 + x2 + x3 )\$residuals )
c <- cor( y2 )

• This is great. Please post that in the answer, even if you think that this is too trivial; it will give me a good feeling for accepting it :-) Also, it will be helpful for people who browse the questions. – January Jul 16 '13 at 8:45
• Answered here. – Scortchi - Reinstate Monica Jul 16 '13 at 8:56
• See also this. One should be aware that basically covariance-to-correlation conversion and correlation-to-covariance conversion is the same thing. – ttnphns Jul 16 '13 at 9:05

To convert a covariance matrix into a correlation matrix, you can use the cov2cor function. The function is in the base package, so no need to install or load a separate package.

Here is an example:

set.seed(123)
x <- rnorm(50, sd=runif(30, 2, 50))
d <- matrix(x, 10)
V <- cov(d)
V

[,1]      [,2]        [,3]        [,4]       [,5]
[1,]  743.05095 -504.7858  240.966105   31.241463  190.86439
[2,] -504.78580 1149.2908 -149.312409   32.251202 -950.09657
[3,]  240.96610 -149.3124  575.844899    2.912683  -12.99241
[4,]   31.24146   32.2512    2.912683  378.453935 -131.68655
[5,]  190.86439 -950.0966  -12.992409 -131.686551 1150.75884

cor(d)

[,1]        [,2]         [,3]         [,4]        [,5]
[1,]  1.0000000 -0.54623919  0.368378182  0.058913599  0.20640654
[2,] -0.5462392  1.00000000 -0.183538918  0.048901752 -0.82615325
[3,]  0.3683782 -0.18353892  1.000000000  0.006239272 -0.01596044
[4,]  0.0589136  0.04890175  0.006239272  1.000000000 -0.19954587
[5,]  0.2064065 -0.82615325 -0.015960436 -0.199545874  1.00000000

cov2cor(V)

[,1]        [,2]         [,3]         [,4]        [,5]
[1,]  1.0000000 -0.54623919  0.368378182  0.058913599  0.20640654
[2,] -0.5462392  1.00000000 -0.183538918  0.048901752 -0.82615325
[3,]  0.3683782 -0.18353892  1.000000000  0.006239272 -0.01596044
[4,]  0.0589136  0.04890175  0.006239272  1.000000000 -0.19954587
[5,]  0.2064065 -0.82615325 -0.015960436 -0.199545874  1.00000000


Using linear Algebra directly:

solve(diag(sqrt(diag(V)))) %*% V %*% solve(diag(sqrt(diag(V))))

[,1]        [,2]         [,3]         [,4]        [,5]
[1,]  1.0000000 -0.54623919  0.368378182  0.058913599  0.20640654
[2,] -0.5462392  1.00000000 -0.183538918  0.048901752 -0.82615325
[3,]  0.3683782 -0.18353892  1.000000000  0.006239272 -0.01596044
[4,]  0.0589136  0.04890175  0.006239272  1.000000000 -0.19954587
[5,]  0.2064065 -0.82615325 -0.015960436 -0.199545874  1.00000000


To convert the correlation matrix back to a covariance matrix, you need the variances/SDs (the variances are the diagonal elements in the covariance matrix):

diag(sqrt(diag(V))) %*% cov2cor(V) %*% diag(sqrt(diag(V)))

[,1]      [,2]        [,3]        [,4]       [,5]
[1,]  743.05095 -504.7858  240.966105   31.241463  190.86439
[2,] -504.78580 1149.2908 -149.312409   32.251202 -950.09657
[3,]  240.96610 -149.3124  575.844899    2.912683  -12.99241
[4,]   31.24146   32.2512    2.912683  378.453935 -131.68655
[5,]  190.86439 -950.0966  -12.992409 -131.686551 1150.75884