7
I managed to create the uniform variables X
and W
, both with normal distribution, using the formula rnorm
in the R
. However, I wanted to create variables so that they had a -0.8 correlation value.
Which control used for this??
7
I managed to create the uniform variables X
and W
, both with normal distribution, using the formula rnorm
in the R
. However, I wanted to create variables so that they had a -0.8 correlation value.
Which control used for this??
3
There are at least two ways to do this:
Since the two variables are Normal, you can use the function mvrnorm()
package MASS
.
Another option is to follow these instructions:
# gerar dois vetores N(0,1), independentes
z1 = rnorm(100)
z2 = rnorm(100)
rho = -0.8 # o coeficiente de correlacao
e1 = z1
e2 = rho*z1+sqrt(1-rho^2)*z2
cor(e1,e2) # aproximadamente -0.8
Note however that the solution 2 must be adapted if the vectors are to be e1
and e2
have different means of 0.
The original question does not make it clear whether the vectors X
and W
follow the uniform or normal distribution; my answer assumes that it is the 2nd case.
Browser other questions tagged r rnorm random-numbers
You are not signed in. Login or sign up in order to post.
Since I am not a 100% R enthusiast, I believe these two posts solve your problem (they are in English): http://stackoverflow.com/questions/18826793/create-correlated-variables-existing-variable and http://stackoverflow.com/questions/17047033/r-constructing-correlated-variables/17049940#17049940.
– Victor Martins