Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate covariance matrix from correlation matrix

Tags:

r

I have a correlation matrix:

a <- matrix(c(1, .8, .8, .8, 1, .8, .8, .8, 1), 3)

##      [,1] [,2] [,3]
## [1,]  1.0  0.8  0.8
## [2,]  0.8  1.0  0.8
## [3,]  0.8  0.8  1.0

I would now like to create a covariance matrix from the correlation matrix. How can this be done in R?

I tried:

e1.sd <- 3
e2.sd <- 10
e3.sd <- 3
e.cov <- a * as.matrix(c, e1.sd, e2.sd, e3.sd) %*% t(as.matrix(c(e1.sd, e2.sd, e3.sd)))

But I get the error:

Error in a * as.matrix(c, e1.sd, e2.sd, e3.sd) %*% t(as.matrix(c(e1.sd,  : 
  non-conformable arrays

What am I doing wrong?

like image 568
user1984076 Avatar asked Sep 11 '13 12:09

user1984076


1 Answers

If you know the standard deviations of your individual variables, you can:

stdevs <- c(e1.sd, e2.sd, e3.sd)
#stdevs is the vector that contains the standard deviations of your variables
b <- stdevs %*% t(stdevs)  
# b is an n*n matrix whose generic term is stdev[i]*stdev[j] (n is your number of variables)
a_covariance <- b * a  #your covariance matrix

On the other hand, if you don't know the standard deviations, it's impossible.

like image 134
S4M Avatar answered Oct 25 '22 02:10

S4M