Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create multi-normal data in R using mvrnorm

Tags:

r

I was using mvrnorm to generate data from multi-normal distribution with the mean mu <- rep(0,4) and Sigma be some positive definite symmetric matrix. However, I found that the last element in the vector I am generating is always 0, any ideas about why is that?

> mvrnorm(n = 1, mu, Sigma, tol = 1e-6, empirical = FALSE, EISPACK = FALSE)
[1]  0.1813268 -0.8993918  0.7461007  0.0000000
> mvrnorm(n = 1, mu, Sigma, tol = 1e-6, empirical = FALSE, EISPACK = FALSE)
[1] 3.2539025 2.9855514 0.7313427 0.0000000
> mvrnorm(n = 1, mu, Sigma, tol = 1e-6, empirical = FALSE, EISPACK = FALSE)
[1] -0.8133201 -1.0011971 -0.3800518  0.0000000

Thanks in advance!

Edit: Thanks for the responses, yes, I checked the Sigma, there is something wrong with it.

like image 717
Nan Avatar asked Jun 29 '26 06:06

Nan


1 Answers

The reason is that your Sigma is not deemed full rank under tol = 1e-6. However, the way that mvrnorm does rank-detection is a bit odd. See inside MASS::mvrnorm:

eS <- eigen(Sigma, symmetric = TRUE)
ev <- eS$values
if (!all(ev >= -tol * abs(ev[1L]))) 
    stop("'Sigma' is not positive definite")
X <- matrix(rnorm(p * n), n)
#[...omitted...]
X <- drop(mu) + eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% t(X)

Instead of

ev >= tol * abs(ev[1L])

it does

ev >= -tol * abs(ev[1L])

Therefore, you must have negative eigenvalues to get rank-deficiency.

like image 104
Zheyuan Li Avatar answered Jun 30 '26 22:06

Zheyuan Li



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!