# generate normals, check correlations
X <- array(rnorm(9000), dim = c(3000, 3))
cor(X)
#
desired correlation
M <- c(1.0, 0.9, 0.6, 0.9, 1.0, 0.3, 0.6, 0.3, 1.0)
dim(M) <- c(3,3)
M
# [,1] [,2] [,3]
#[1,] 1.0 0.9 0.6
#[2,] 0.9 1.0 0.3
#[3,] 0.6 0.3 1.0
# adjust correlations for uniforms
for (i in 1:3){
for (j in 1:3){
if (i != j){
M[i, j] <- 2 * sin(pi * M[i, j] / 6)
M[j, i] <- 2 * sin(pi * M[j, i] / 6)
}
}
}
# induce correlation, check correlations
C <- chol(M)
Y <- X %*% C
cor(Y)
# create uniforms, check correlations
Y <- pnorm(Y)
cor(Y)
x=Y[,1]
y=Y[,2]
z=Y[,3]
cor(x,y) #0.9102382
cor(y,z) #0.3213543
cor(x,z) #0.6197905
# plot results (marginals)
par(mfrow = c(1,3))
hist(x, main = paste("x"), xlab = "")
hist(y, main = paste("y"), xlab = "")
hist(z, main = paste("z"), xlab = "")