0
votes

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?

1
What about cholesky %*% as.matrix(rnormd)?alan ocallaghan
t(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 in A %*% Bdww
@dww So, the problem is that it is not coerced automatically to a matrix format. It worked. upvotedMauro
NP. BTW why did you ever convert to data.frame in the first place? Seems the whole calculation is better done with matricesdww
voting to close as a typo, since was simply down to performing matrix multiplication on a non-matrixdww

1 Answers

-1
votes

If you want multiply two matrices you should use %*% instead of * sign. So here is the part you should fix.

rnormd <- t(t(rnormd)%*%cholesky)