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 sqrt(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.