mardi 29 septembre 2015

generating correlated summated scales from random normal variables in r

I want to generate 5 random normal variables in r each with given mean and sd

x1 <- rnorm(100,20,5)
x2 <- rnorm(100,22,4)
y1 <- rnorm(100,21,3)
y2 <- rnorm(100,23,4)
y3 <- rnorm(100,22,5)

and with summated scales x and y having a predefined correlation as

x <- rowMeans(cbind(x1,x2))
y <- rowMeans(cbind(y1,y2,y3))
cor(x,y) # must be about 0.6

I believe using a loop should be an inefficient solution for this. also y=r*x+sqrt(1-r*r)*rnorm(n) was not applicable. I'm a bit confused how to start. any suggestion would be greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire