5
I need to generate data series that have correlations defined using R. I used a method I found here in the OS (How to generate correlated variables in R?) and I was able to create the variables with the desired correlation, however, by trying to automate this process for the creation of 1000 estimates and for different correlations, the result obtained is a 1000x5 matrix with all identical values. The code I’m using is as follows::
set.seed(2423049)
corr = matrix(,1000,5)
for(k in 1:5){
for (i in 1:5){
for(j in 1:1000){
rho = c(-0.7,-0.3,0,0.1,0.5) # correlações que preciso utilizar
xstar=rnorm(1000,2,2) # x* com distribuicao normal N(2,2)
a2=rnorm(1000,2,2) # parametro criado para obter w a partir da correlacao rho com x*
w = rho[k]*xstar+sqrt(1-rho[k]^2)*a2 # w calculado a partir de uma correlacao definida com x
corr[j,i]=cor(xstar,w) # matriz de correlacoes entre x* e w
}
}
}
Through this process, the result obtained was a 1000x5 matrix where all values were 0.5499732
What am I doing wrong?
thank you so much for your help! I managed to do what I needed!
– Pedro Henrique Costa