2
votes

I would like to generate N number of random variables that each have a fixed correlation p with each other.

The variables should have mean = 1 and a std = 5

N=10 #number of variables
mu <- rep(1,N) #means
p=0.7 #correlation
Sigma <- matrix(p,ncol=N,nrow=N) #variance covariance matrix
diag(Sigma) <- 5 #standard deviations


library(MASS)
set.seed(12)
data <- mvrnorm(10000,mu,Sigma)

However, the resulting standard deviations are not 5.

apply(data,2,sd)

 [1] 2.264853 2.219811 2.224703 2.245595 2.216712 2.236484 2.240794 2.220532 2.227445 
[10] 2.247943

Is there a systematic way in which I could vary the standard deviations while keeping the other parameters fixed?

2
I'm pretty sure that Sigma is the variance-covariance matrix, so if you want standard deviations of 5, you should feed it diag(Sigma) <- 25. Note that 2.264853^2 is 5.129559 which is pretty close to 5...lmo

2 Answers

2
votes

Your variance-covariance (VCV) matrix is incorrect. The diagonals should be the variances, not the standard deviations. If you do

apply(data,2,var)

Then you will see that the variances are ~5. If you want the standard deviation to be 5, then you need the off-diagonals to be 25.

In addition, you are entering correlations into the off-diagonals of the VCV matrix, and those should be covariances. To get from correlations to covariances, you need to multiply the correlation by the standard deviations of the two variables being correlated. In your case, assuming the standard deviation of both variables is 5, the covariances in your VCV should be p = 0.7 * 5 * 5. So your final VCV should be

p <- 0.7 * 5 * 5
Sigma <- matrix(p,ncol=N,nrow=N) 
diag(Sigma) <- 25 

The resulting N random variables will have means 1, covariances p, and correlations p/25.

1
votes

You have created a matrix Sigma where the diagonal elements are 5, but the mvnorm wants a covariance matrix (not a standard deviation matrix) as Sigma. So you need to have the diagonal elements equal 25. It's also not using the value of p=0.7 properly, those are correlations, not covariances. You need to turn the correlations into covariances.

See:

http://blogs.sas.com/content/iml/2010/12/10/converting-between-correlation-and-covariance-matrices.html

https://math.stackexchange.com/questions/446093/generate-correlated-normal-random-variables