The next reproducible code generates 394 observations of 183 random normales, and tries to correlate them with Cholesky decomposition:
Generate the parameters
d <- 211
l <- 183
m <- -0.006495094
vectorsd <- rep(0.29, 183)
Generate random normals uncorrelated
rnormd <- as.data.frame(rnorm(l, mean = m, sd = vectorsd))
for (i in 1:(d+l)) {
rnormd[,i] <- rnorm(l, mean = m, sd = vectorsd)
}
Generate a random semidefinite positive matrix of correlations
v <- runif(183,0.6,0.8)
corr <- `diag<-`(tcrossprod(v),1)
Generate cholesky matrix
cholesky <- chol(corr)
Correlate the normals and transpose the output
rnormd <- t(t(rnormd)%*%cholesky)
In this last instruction I get the error
Error in rnormd * cholesky : non-conformable arrays
At first a thought that the problem was going to be solved transposing my cholesky matrix, but then I realized that chol()
function already transposes it.
Can anyone help me?
cholesky %*% as.matrix(rnormd)
? – alan ocallaghant(as.matrix(rnormd)) %*% cholesky
works fine. You need to ensure (1) to use matrix multiplkication%*%
not (*
), and (2) that ncol of A equals nrow of B inA %*% B
– dww