Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle slightly negative eigenvalue when simulating #957

Merged
merged 1 commit into from Mar 31, 2022

Conversation

kylebaron
Copy link
Collaborator

Summary

  • MASS::mvrnorm will accept covariance matrices with small negative eigenvalues, setting them to zero
  • this PR implements the same behavior
  • note: mrgsolve has historically checked for positive determinant for incoming model matrices and is working toward changing the check to be based on eigen values instead

Example

Showing both MASS and mrgsolve simulating from a matrix with negative eigenvalue

mat <- mrgsolve::bmat(
1.53394, 
1.22232, 0.974014,
2.78342 , 2.211570, 9.86881
)

mat
#>         [,1]     [,2]    [,3]
#> [1,] 1.53394 1.222320 2.78342
#> [2,] 1.22232 0.974014 2.21157
#> [3,] 2.78342 2.211570 9.86881

eigen(mat)$values
#> [1]  1.130541e+01  1.071351e+00 -7.148461e-08
det(mat)
#> [1] -8.658264e-07

set.seed(1010)
a <- MASS::mvrnorm(1000000, c(0,0,0), mat)
b <- mrgsolve::mvgauss(mat,  1000000)

cov(a)
#>          [,1]      [,2]     [,3]
#> [1,] 1.533910 1.2223026 2.778312
#> [2,] 1.222303 0.9740056 2.207535
#> [3,] 2.778312 2.2075348 9.832882
cov(b)
#>          [,1]      [,2]     [,3]
#> [1,] 1.532405 1.2211011 2.777505
#> [2,] 1.221101 0.9730461 2.206862
#> [3,] 2.777505 2.2068619 9.854215

Created on 2022-03-28 by the reprex package (v2.0.1)

@kylebaron kylebaron linked an issue Mar 28, 2022 that may be closed by this pull request
@kylebaron kylebaron merged commit cb94d15 into develop Mar 31, 2022
@kylebaron kylebaron deleted the neg-eigenvalue branch March 31, 2022 02:58
@kylebaron kylebaron mentioned this pull request May 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deal with negative eigenvalue in model matrix
1 participant