I need to generate data series that have correlations defined using R. I used a method that I found here in the OS (#) and managed to create the variables with the desired correlation, however, when trying to automate this process for the creation of 1000 estimates and for different correlations, the result obtained is a 1000x5 matrix with all the 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 was a 1000x5 array where all values were 0.5499732
What am I doing wrong?