Skip to main content
3 of 6
added 62 characters in body
Patrick
  • 1.6k
  • 2
  • 18
  • 21

obtaining covariance matrix from correlation matrix

I am trying to figure out how to convert a correlation matrix (R) to a covariance matrix (S) for input into a random number generator that only accepts S (rmvnorm("mvtnorm") in R)

library("mvtnorm")

TRUTH= 0.8 # target correlation value between X1 and X2

R <- as.matrix(data.frame(c(1, TRUTH), c(TRUTH, 1))) 

V <- diag(c(sqrt(1), sqrt(1))) # diagonal matrix of variances

S <- V %*% R %*% V

cor(rmvnorm(100, sigma=S) )

repeat this to get an idea of the variance around Pearson's estimator

Instance where variances are not equal to 1

V <- diag(c(sqrt(3), sqrt(2))) 

    S <- V %*% R %*% V

cor(rmvnorm(100, sigma=S) )

This seems to be correct, but I would like expert criticism.

Patrick
  • 1.6k
  • 2
  • 18
  • 21