1
$\begingroup$

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?

$\endgroup$
1
$\begingroup$

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:

fx01<-function(nu,FixedRow){
    p<-lenght(FixedRow)
    a1<-matrix(nu,p,p)
    diag(a1)<-1
    a1[1,]<-a1[,1]<-FixedRow
    -det(a1)
}


FixedRow<-c(1,.9,.8,.7,.6,.5)
a1<-optimize(fx01,interval=c(-1,1),FixedRow=FixedRow)

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

nu<-a1$min
a2<-matrix(nu,6,6)
diag(a2)<-1
a2[1,]<-a2[,1]<-c(1,.9,.8,.7,.6,.5)


     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:

library("MASS")
set.seed(123)
X<-mvrnorm(100,rep(0,6),a2,empirical=TRUE)
cor(X)

and you can check that, for example

cor(X[,1],X[,2])

returns 0.9.

$\endgroup$
  • $\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.