JenniNiku / gllvm

Generalized Linear Latent Variable Models
https://jenniniku.github.io/gllvm/
49 stars 20 forks source link

Runtime error with certain number of latent dimensions #9

Closed gtikhonov closed 1 year ago

gtikhonov commented 5 years ago

Hi, Thanks for the package, it seems to be a really nice and convenient tool! Recently, I've been trying to fit some models using it and I encountered the following runtime error:

> fit <- gllvm(Y, X, formula=~., family=gaussian(), num.lv=5)
Error in solve.default(D.mat) : 
  Lapack routine dgesv: system is exactly singular: U[2001,2001] = 0
Error in abs(cov.mat.mod) : non-numeric argument to mathematical function
Standard errors for parameters could not be calculated.

Same error happens also with num.lv=6 but not with num.lv=4. I managed to replicate the bug with the following minimal working example (MWE):

library(gllvm)
set.seed(0)
ny = 200
ns = 50
nc = 3
Y = matrix(rnorm(ny*ns),ny,ns)
X = matrix(rnorm(ny*nc),ny,nc)
fit <- gllvm(Y, X, formula=~., family=gaussian(), num.lv=5)

Could you provide any insight, whether this is a "critical" error, so that the returned results in the fit object are completely screwed, or just some of them? What do I not take into account or what I shall adjust in the MWE to avoid such misbehaviour? Here is my R session information if this can help in any way:

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gllvm_1.1.7   mvabund_4.0.1 TMB_1.7.15   

loaded via a namespace (and not attached):
 [1] MASS_7.3-51.4   compiler_3.6.1  Matrix_1.2-17   parallel_3.6.1  tools_3.6.1     mgcv_1.8-29     Rcpp_1.0.3     
 [8] mvtnorm_1.0-11  splines_3.6.1   nlme_3.1-141    grid_3.6.1      tweedie_2.3.2   statmod_1.4.32  lattice_0.20-38
BertvanderVeen commented 5 years ago

Hello Gleb,

I believe your error message originates from the calculation of the standard errors. In your example, if you add sd.errors = FALSE, this will get rid of the error (as the standard errors won't be calculated). As such, it doesn't affect the estimates, though it does indicate there are some problems with the fit, it's possible the optimizer didn't converge to a good solution.

You can check the gradient of your fit withfit$TMBfn$gr(fit$TMBfn$par). If there are some values that are far away from zero, you might want to try some different starting values (see the starting.val and n.init options in the documentation), or reconsider the number of latent variables.