2
votes

I am trying to make a random matrix correlation over 183 variables to calculate a Cholesky decomposition and correlate 183 random normals. For the creation of the correlation matrix the following reproducible code is the one I am using:

First I set the seed

set.seed(2)

Then I generate random numbers between 0.6 and 0.8

corr <- matrix(runif(183*183, min = 0.6, max = 0.8), 183, 183)

The next step is turning the diagonal into ones

for (i in 1:183) {
  for (j in 1:183) {
    if (i == j) {
    corr[i,j] <- 1
    }
  }
}

Last step is making it symmetric

for (i in 1:183) {
  for (j in 1:183) {
    if (i < j) {
    corr[i,j] <- corr[j,i]
    }
  }
}

The problem I run into arise when I try to make the cholesky decomposition

cholesky <- chol(corr)

I get the following error:

Error in chol.default(corr) : the leading minor of order 14 is not positive definite

How could I make my correlation matrix semi definite positive?

1
Not an answer : But you can set diagonal to 1 by doing diag(corr) <- 1Ronak Shah
Semi-positive definite matrices are not defined by once on the diagonal elements and symmetry. This question is more a mathematical question rather than a R code question. You may follow math.stackexchange.com/questions/357980/…Freakazoid
@Freakazoid, thanks for your answer, I think I am aware of what semi-definite positive matrix means, however, I have looked up how to do it in R and I can't get any ideas for a concrete case of a correlation matrix, My question is more about how to do it to this concrete case in RMauro
@Mauro You can generate a random covariance matrix with a Wishart distribution, and turn it into a correlation matrix with cov2cor.Stéphane Laurent

1 Answers

1
votes

Maybe you can try the code below to generate semi-definite positive matrix

set.seed(2)
v <- runif(183,0.6,0.8)
corr <- `diag<-`(tcrossprod(v),1)