I have a vector: a<-rnorm(100)

I would like to create 5 variables that are correlated with a to a different extent.

For example each of the 5 variables should have a correlation of .5, .6, .7, .8, .9 with a respectively

How can this be done?


As it is, there are many ways to formulate (and thus solve) your problem. you are looking for the symmetric, psd matrix $A$ with $A_{1\cdot}=\{1,0.9,0.8,0.7,0.6,0.5\}$ and $A_{ii}=1,\;i=1,\ldots,6$. There are many matrices satisfying these requirements. To pick one we need to add some more criterion/constraints. For example: if we add the criterion that we also want $|A|$ to be smallest and all the entries of $A\setminus \{A_{ii},A_{1\cdot}\}$ to be equal to some constant $\nu$. This is now a convex problem with a unique solution. In R you would write solve it as:



and the value $\nu\approx0.6$, yielding the matrix:


     a   b    c    d    e    f
a  1.0  0.9  0.8  0.7  0.6  0.5
b  0.9  1.0  0.6  0.6  0.6  0.6
c  0.8  0.6  1.0  0.6  0.6  0.6
d  0.7  0.6  0.6  1.0  0.6  0.6
e  0.6  0.6  0.6  0.6  1.0  0.6
f  0.5  0.6  0.6  0.6  0.6  1.0

which satisfies your requirements. Then, to actually generate your matrix of variables simply do:


and you can check that, for example


returns 0.9.

  • $\begingroup$ what I actually meant is that the second variable should be a different set of random numbers that should be correlated with a. for example b<-a+rnorm(100,mean=0,sd=5) cor(a,b)=0.31 but instead of 0.31 I would like to predetermine what the correlation should be $\endgroup$ – tuw Sep 4 '13 at 11:54
  • $\begingroup$ yes, you can check that the second variable (in the example above) has a correlation with 'a' of 90% (as you originally asked) $\endgroup$ – user603 Sep 4 '13 at 11:56
  • $\begingroup$ right sorry. how easy is it to modify the code to apply to a different matrix length of A1? e.g. if I would like to have 6 instead of 5 correlated variables. is the problem very different? $\endgroup$ – tuw Sep 4 '13 at 11:59
  • $\begingroup$ not difficult at all. In fact I will rewrite the code to make the dimension of A a parameter! $\endgroup$ – user603 Sep 4 '13 at 12:00
  • $\begingroup$ is it also possible to have each of the 5 variables correlate with 'a' say .59 and each of the five variables correlate with each other .7? $\endgroup$ – tuw Sep 4 '13 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.