I am trying to generate a set 10 normally distributed random variables that are negatively correlated with each other with $\rho=-0.5$.
I've generated a covariance matrix S
with 10's on the diagonals and -5's everywhere else using the following code (when I check the correlation matrix using cov2cor
it has 1's on the diagonal and -0.5's everywhere else):
> S = matrix(-5, 10, 10)
> diag(S) <- 10
> S
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 10 -5 -5 -5 -5 -5 -5 -5 -5 -5
[2,] -5 10 -5 -5 -5 -5 -5 -5 -5 -5
[3,] -5 -5 10 -5 -5 -5 -5 -5 -5 -5
[4,] -5 -5 -5 10 -5 -5 -5 -5 -5 -5
[5,] -5 -5 -5 -5 10 -5 -5 -5 -5 -5
[6,] -5 -5 -5 -5 -5 10 -5 -5 -5 -5
[7,] -5 -5 -5 -5 -5 -5 10 -5 -5 -5
[8,] -5 -5 -5 -5 -5 -5 -5 10 -5 -5
[9,] -5 -5 -5 -5 -5 -5 -5 -5 10 -5
[10,] -5 -5 -5 -5 -5 -5 -5 -5 -5 10
My plan was to either use the Cholesky decomposition or to use the mvrnorm
function to generate the variables. However, when I use mvrnorm
I get an error saying that the matrix is not positive definite.
> mvrnorm(n=1, rep(5, 10), S)
Error in mvrnorm(n = 1, rep(5, 10), S) :
'Sigma' is not positive definite
When I check the eigenvalues, the last is, in fact, -35. My question is, what am I doing wrong?