# Generating correlated numbers with predefined correlations

I'm trying to generate correlated data (preferably multinormal) with predefined correlations (e.g. 0.35 or 0.9). Any idea how I can do it? I'm using R and I did find a way to generate this (using mvrnorm), but you need to supply a covariance matrix. I have a covariance matrix with correlations around 0.9; however, I don't know how I can modify its entries to change the correlation. If I can do that, I'll be able to generate correlated data with the correlations I need.

Regards,

• You just need to play with the values in the covariance matrix in mvrnorm and relate them with the definition of correlation matrix.
– user10525
Commented Apr 23, 2012 at 12:00
• If you post the code for you covariance matrix we can tell you how to modify it to get other correlations. Commented Apr 23, 2012 at 12:11
• Procrastinator, I can't just change the values in the matrix to whatever I want, changing any number has an effect on other entries in the matrix and I must know how the other entries change (inc. or dec.) before changing anything. For example, changing the variance of any variable will change its covaraince with the other variables. Commented Apr 23, 2012 at 12:30
• The correlation between $X_i$ and $X_j$ is given $$Cor(X_i,X_j) = \frac{Cov(X_i, X_j)}{sd(X_i)sd(X_j)}.$$ If your correlation matrix is V this is $$Cor(X_i,X_j) = \frac{V_{ij}}{\sqrt{V_{ii}}\sqrt{V_{jj}}}.$$ Maybe this can help you set up your covariance matrix, especially if you are able to simplify your problem by standardizing each variable.
– Erik
Commented Apr 23, 2012 at 13:25
• This question has been discussed on here before. For example, look here: stats.stackexchange.com/questions/13382/… Commented Apr 23, 2012 at 14:52

I will give an example. Imagine you have 3 Gaussian variables $X_1, X_2$ and $X_3$. You want the correlation between $X_1$ and $X_2$ to be 0, and all correlations with $X_3$ to be 1. This is obviously impossible because $X_1 = X_3$ and $X_3 = X_1$ says that $X_1 = X_2$ (up to shifting and scaling), which contrasts with the assumption that they are independent!
You cannot choose arbitrary correlation values, but you can check whether they define a valid correlation matrix. Say that you have a symmetric square matrix mat with required correlation coefficients. You can test that it is positive definite as shown below.
all(eigen(mat)$values >= 0)  For symmetric real matrices, positive definite is equivalent to having all eigenvalues positive. • It might be good to make the inequality in the code nonstrict to allow for perfect correlations between linear combinations of variables. Commented Jun 3, 2012 at 14:41 • @cardinal Done. But that is purely for demonstration purposes. Testing strict equality of real numbers is something R cannot do as (.3-.2) == (.2-.1) shows. Commented Jun 3, 2012 at 14:47 • Good point; it was actually the larger conceptual point I was trying to address. That "limitation" has more to do with floating point representation, than R itself, though. Testing against zero is a bit special. Some related routines in R will truncate small values to zero if they fall below a tolerance. Commented Jun 3, 2012 at 14:54 • @cardinal 'That "limitation" has more to do with floating point representation, than R itself, though' Yes of course. Apologies to the R team :-) Commented Jun 3, 2012 at 15:01 The MASS package has a function called mvrnorm() that can generate a group or random numbers to a specified level of correlation. An example of the setup can be found in the beginning of the example here: http://menugget.blogspot.de/2011/11/propagation-of-error.html • Sorry, didn't see that Jawad had already pointed you to the same function. In any case, the example posted might help you understand how to set it up. Commented Apr 23, 2012 at 12:47 • Thanks Marc, from the page I understand that all I have to do is set the diagonal elements of my covariance matrix to rho and the off-diagonal elements to 1 and I should get the data I need correlated by rho? Commented Apr 23, 2012 at 13:16 • Not exactly - the covariance matrix will depend on your defined standard deviations. If sd=1 for all series, then you are correct. Otherwise, you will need to define your std. devs for each series. Commented Apr 23, 2012 at 13:26 • No. The variances of the variables should be along the diagonal and the off-diagonal elements should be rho (if$\sigma^2=1\$). Commented Apr 23, 2012 at 13:38