stan-dev / loo

loo R package for approximate leave-one-out cross-validation (LOO-CV) and Pareto smoothed importance sampling (PSIS)
https://mc-stan.org/loo
Other
149 stars 34 forks source link

loo_moment_match.brmsfit crashes R and RStudio #193

Closed hrlai closed 1 year ago

hrlai commented 2 years ago

Hi dev, with reference to this issue from brms raised by @zfarley, I'm attempting to provide a simplified code which will hopefully reproduce the crash when attempting to perform loo_moment_match on a brmsfit object. @paul-buerkner suggested that the error happens inside loo::loo_moment_match.default and that the issue should be raised here.

Thanks in advance. Looking forward to hear your idea.

Minimal code:

library(brms)

# first example in ?brm
# modified to save_pars
prior1 <- prior(normal(0,10), class = b) +
    prior(cauchy(0,2), class = sd)
fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient),
            data = epilepsy, 
            family = poisson(), 
            prior = prior1, 
            save_pars = save_pars(all = TRUE),
            core = 4)

# attempting to do loo_moment_match.brmsfit will crash R and RStudio
fit1 <- add_criterion(fit1, "loo", moment_match = TRUE)

Session info in case it helps:

> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_AU.UTF-8       LC_NUMERIC=C               LC_TIME=en_NZ.UTF-8       
 [4] LC_COLLATE=en_AU.UTF-8     LC_MONETARY=en_NZ.UTF-8    LC_MESSAGES=en_AU.UTF-8   
 [7] LC_PAPER=en_NZ.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_NZ.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] brms_2.16.3 Rcpp_1.0.7 

loaded via a namespace (and not attached):
  [1] nlme_3.1-152         matrixStats_0.61.0   xts_0.12.1           threejs_0.3.3       
  [5] rstan_2.21.2         tensorA_0.36.2       tools_4.1.2          backports_1.4.0     
  [9] utf8_1.2.2           R6_2.5.1             DT_0.20              mgcv_1.8-38         
 [13] DBI_1.1.1            projpred_2.0.2       colorspace_2.0-2     withr_2.4.3         
 [17] tidyselect_1.1.1     gridExtra_2.3        prettyunits_1.1.1    processx_3.5.2      
 [21] Brobdingnag_1.2-6    curl_4.3.2           compiler_4.1.2       cli_3.1.0           
 [25] shinyjs_2.0.0        colourpicker_1.1.1   posterior_1.1.0      scales_1.1.1        
 [29] dygraphs_1.1.1.6     checkmate_2.0.0      mvtnorm_1.1-3        ggridges_0.5.3      
 [33] callr_3.7.0          stringr_1.4.0        digest_0.6.29        StanHeaders_2.21.0-7
 [37] minqa_1.2.4          rmarkdown_2.11       base64enc_0.1-3      pkgconfig_2.0.3     
 [41] htmltools_0.5.2      lme4_1.1-27.1        fastmap_1.1.0        htmlwidgets_1.5.4   
 [45] rlang_0.4.12         shiny_1.7.1          farver_2.1.0         generics_0.1.1      
 [49] zoo_1.8-9            jsonlite_1.7.2       crosstalk_1.2.0      gtools_3.9.2        
 [53] dplyr_1.0.7          distributional_0.2.2 inline_0.3.19        magrittr_2.0.1      
 [57] loo_2.4.1            bayesplot_1.8.1      Matrix_1.3-4         munsell_0.5.0       
 [61] fansi_0.5.0          abind_1.4-5          lifecycle_1.0.1      stringi_1.7.6       
 [65] yaml_2.2.1           MASS_7.3-54          pkgbuild_1.2.1       plyr_1.8.6          
 [69] grid_4.1.2           parallel_4.1.2       promises_1.2.0.1     crayon_1.4.2        
 [73] miniUI_0.1.1.1       lattice_0.20-45      splines_4.1.2        knitr_1.36          
 [77] ps_1.6.0             pillar_1.6.4         igraph_1.2.9         boot_1.3-28         
 [81] markdown_1.1         shinystan_2.5.0      reshape2_1.4.4       codetools_0.2-18    
 [85] stats4_4.1.2         rstantools_2.1.1     glue_1.5.1           evaluate_0.14       
 [89] V8_3.6.0             RcppParallel_5.1.4   nloptr_1.2.2.3       vctrs_0.3.8         
 [93] httpuv_1.6.3         gtable_0.3.0         purrr_0.3.4          reshape_0.8.8       
 [97] assertthat_0.2.1     ggplot2_3.3.5        xfun_0.28            mime_0.12           
[101] xtable_1.8-4         coda_0.19-4          later_1.3.0          rsconnect_0.8.25    
[105] tibble_3.1.6         shinythemes_1.2.0    gamm4_0.2-6          ellipsis_0.3.2      
[109] bridgesampling_1.1-2
topipa commented 2 years ago

I have a very similar system, and the example works for me without crashing. Also the example in the linked brms issue works. Without any example error message and not being able to reproduce the crash, it's unfortunately quite difficult to investigate this further.

In the issue you linked, these is discussion about moment matching crashing when loading a fit object from .rds. This is a known issue without an easy solution (see for example https://github.com/stan-dev/loo/issues/183).

My session info and Rstudio version:

RStudio 2021.09.1+372 "Ghost Orchid" Release (8b9ced188245155642d024aa3630363df611088a, 2021-11-08) for Ubuntu Bionic
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.8 Chrome/69.0.3497.128 Safari/537.36
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=fi_FI.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=fi_FI.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=fi_FI.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=fi_FI.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] brms_2.16.3 Rcpp_1.0.7 

loaded via a namespace (and not attached):
 [1] Brobdingnag_1.2-6    gtools_3.9.2         StanHeaders_2.21.0-7
 [4] RcppParallel_5.1.4   threejs_0.3.3        shiny_1.7.1         
 [7] posterior_1.1.0      distributional_0.2.2 stats4_4.1.2        
[10] tensorA_0.36.2       pillar_1.6.4         backports_1.4.1     
[13] lattice_0.20-45      glue_1.6.0           digest_0.6.29       
[16] promises_1.2.0.1     checkmate_2.0.0      colorspace_2.0-2    
[19] htmltools_0.5.2      httpuv_1.6.4         Matrix_1.4-0        
[22] plyr_1.8.6           dygraphs_1.1.1.6     pkgconfig_2.0.3     
[25] rstan_2.21.3         purrr_0.3.4          xtable_1.8-4        
[28] mvtnorm_1.1-3        scales_1.1.1         processx_3.5.2      
[31] later_1.3.0          tibble_3.1.6         bayesplot_1.8.1     
[34] generics_0.1.1       farver_2.1.0         ggplot2_3.3.5       
[37] ellipsis_0.3.2       DT_0.20              shinyjs_2.0.0       
[40] cli_3.1.0            magrittr_2.0.1       crayon_1.4.2        
[43] mime_0.12            ps_1.6.0             fansi_0.5.0         
[46] nlme_3.1-152         xts_0.12.1           pkgbuild_1.3.1      
[49] colourpicker_1.1.1   prettyunits_1.1.1    rsconnect_0.8.25    
[52] tools_4.1.2          loo_2.4.1            lifecycle_1.0.1     
[55] matrixStats_0.61.0   stringr_1.4.0        munsell_0.5.0       
[58] callr_3.7.0          compiler_4.1.2       rlang_0.4.12        
[61] grid_4.1.2           ggridges_0.5.3       htmlwidgets_1.5.4   
[64] crosstalk_1.2.0      igraph_1.2.10        miniUI_0.1.1.1      
[67] base64enc_0.1-3      codetools_0.2-18     gtable_0.3.0        
[70] inline_0.3.19        abind_1.4-5          markdown_1.1        
[73] reshape2_1.4.4       R6_2.5.1             gridExtra_2.3       
[76] rstantools_2.1.1     zoo_1.8-9            bridgesampling_1.1-2
[79] dplyr_1.0.7          fastmap_1.1.0        utf8_1.2.2          
[82] shinystan_2.5.0      shinythemes_1.2.0    stringi_1.7.6       
[85] parallel_4.1.2       vctrs_0.3.8          tidyselect_1.1.1    
[88] coda_0.19-4