Bridge sampling with the latest rstan (yet again not working)

Hi, yet again I’m trying my luck with the latest rstan and bridge sampling and no success.
I have Linux, I’ve installed rstan like this, which seems to work so far.

remove.packages(c("StanHeaders", "rstan"))
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")), type = "source")
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")), type = "source")

When I try to use bridgesampling I get. (I just tried to compare dumb model with itself very few data points with brms)

> bayes_factor(m,m)

 *** caught segfault ***
address (nil), cause 'memory not mapped'

Traceback:
 1: .External(list(name = "CppMethod__invoke_notvoid", address = <pointer: 0x55de94c5fe10>,     dll = list(name = "Rcpp", path = "/home/bruno/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/libs/Rcpp.so",         dynamicLookup = TRUE, handle = <pointer: 0x55de969abab0>,         info = <pointer: 0x55de949427b0>), numParameters = -1L),     <pointer: 0x55dea30262e0>, <pointer: 0x55de9e57ae10>, .pointer,     ...)
 2: object@.MISC$stan_fit_instance$unconstrain_pars(pars)
 3: .local(object, ...)
 4: rstan::unconstrain_pars(stanfit_model, .rstan_relist(theta, skeleton))
 5: rstan::unconstrain_pars(stanfit_model, .rstan_relist(theta, skeleton))
 6: FUN(newX[, i], ...)
 7: apply(ex, 1:2, FUN = function(theta) {    rstan::unconstrain_pars(stanfit_model, .rstan_relist(theta,         skeleton))})
 8: bridge_sampler.stanfit(samples$fit, ...)
 9: bridge_sampler(samples$fit, ...)
10: doTryCatch(return(expr), name, parentenv, handler)
11: tryCatchOne(expr, names, parentenv, handlers[[1L]])
12: tryCatchList(expr, classes, parentenv, handlers)
13: tryCatch(expr, error = function(e) {    call <- conditionCall(e)    if (!is.null(call)) {        if (identical(call[[1L]], quote(doTryCatch)))             call <- sys.call(-4L)        dcall <- deparse(call, nlines = 1L)        prefix <- paste("Error in", dcall, ": ")        LONG <- 75L        sm <- strsplit(conditionMessage(e), "\n")[[1L]]        w <- 14L + nchar(dcall, type = "w") + nchar(sm[1L], type = "w")        if (is.na(w))             w <- 14L + nchar(dcall, type = "b") + nchar(sm[1L],                 type = "b")        if (w > LONG)             prefix <- paste0(prefix, "\n  ")    }    else prefix <- "Error : "    msg <- paste0(prefix, conditionMessage(e), "\n")    .Internal(seterrmessage(msg[1L]))    if (!silent && isTRUE(getOption("show.error.messages"))) {        cat(msg, file = outFile)        .Internal(printDeferredWarnings())    }    invisible(structure(msg, class = "try-error", condition = e))})
14: try(bridge_sampler(samples$fit, ...))
15: bridge_sampler.brmsfit(x1, ...)
16: bridge_sampler(x1, ...)
17: bayes_factor.brmsfit(m, m)
18: bayes_factor(m, m)

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace

Bridge sampling was working fine with the last rstan from CRAN, and I think that at some point was working with one dev version, but then it stop working with newer dev versions, and I had to roll back to the CRAN version. Now I really want to use the latest rstan… Is there any way to fix this? Is there any special way to install rstan so that bridge sampling works?

@Henrik_Singmann , any experience with this?

  • Operating System: Ubuntu 20.04.4 LTS

  • RStan Version:
    rstan_2.26.9 StanHeaders_2.26.9 brms_2.16.3 Rcpp_1.0.8.3

  • Output of writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars"))):

CXX14FLAGS=-O3 -march=native -mtune=native -fPIC
CXX14=g++
  • Output of devtools::session_info("rstan"):
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.1.2 (2021-11-01)
 os       Ubuntu 20.04.4 LTS
 system   x86_64, linux-gnu
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       Europe/Amsterdam
 date     2022-04-08
 pandoc   2.2.3.2 @ /home/bruno/anaconda3/bin/pandoc

─ Packages ───────────────────────────────────────────────────────────────────
 package      * version   date (UTC) lib source
 backports      1.4.1     2021-12-13 [1] CRAN (R 4.1.1)
 BH             1.78.0-0  2021-12-15 [3] CRAN (R 4.1.2)
 callr          3.7.0     2021-04-20 [3] CRAN (R 4.0.5)
 checkmate      2.0.0     2020-02-06 [1] CRAN (R 4.1.1)
 cli            3.2.0     2022-02-14 [1] CRAN (R 4.1.1)
 colorspace     2.0-3     2022-02-21 [1] CRAN (R 4.1.1)
 crayon         1.5.0     2022-02-14 [1] CRAN (R 4.1.1)
 curl           4.3.2     2021-06-23 [3] CRAN (R 4.1.0)
 desc           1.4.0     2021-09-28 [3] CRAN (R 4.1.1)
 digest         0.6.29    2021-12-01 [1] CRAN (R 4.1.1)
 ellipsis       0.3.2     2021-04-29 [3] CRAN (R 4.0.5)
 fansi          1.0.2     2022-01-14 [1] CRAN (R 4.1.1)
 farver         2.1.0     2021-02-28 [3] CRAN (R 4.0.4)
 ggplot2        3.3.5     2021-06-25 [1] CRAN (R 4.1.1)
 glue           1.6.2     2022-02-24 [1] CRAN (R 4.1.1)
 gridExtra      2.3       2017-09-09 [1] CRAN (R 4.1.1)
 gtable         0.3.0     2019-03-25 [3] CRAN (R 4.0.0)
 inline         0.3.19    2021-05-31 [1] CRAN (R 4.1.1)
 isoband        0.2.5     2021-07-13 [3] CRAN (R 4.1.0)
 jsonlite       1.8.0     2022-02-22 [3] CRAN (R 4.1.2)
 labeling       0.4.2     2020-10-20 [3] CRAN (R 4.0.3)
 lattice        0.20-45   2021-09-22 [4] CRAN (R 4.1.1)
 lifecycle      1.0.1     2021-09-24 [1] CRAN (R 4.1.1)
 loo            2.4.1     2020-12-09 [1] CRAN (R 4.1.1)
 magrittr       2.0.2     2022-01-26 [1] CRAN (R 4.1.1)
 MASS           7.3-55    2022-01-13 [4] CRAN (R 4.1.2)
 Matrix         1.4-0     2021-12-08 [4] CRAN (R 4.1.2)
 matrixStats    0.61.0    2021-09-17 [1] CRAN (R 4.1.1)
 mgcv           1.8-39    2022-02-24 [4] CRAN (R 4.1.2)
 munsell        0.5.0     2018-06-12 [3] CRAN (R 4.0.0)
 nlme           3.1-155   2022-01-13 [4] CRAN (R 4.1.2)
 pillar         1.7.0     2022-02-01 [1] CRAN (R 4.1.1)
 pkgbuild       1.3.1     2021-12-20 [3] CRAN (R 4.1.2)
 pkgconfig      2.0.3     2019-09-22 [3] CRAN (R 4.0.0)
 prettyunits    1.1.1     2020-01-24 [3] CRAN (R 4.0.0)
 processx       3.5.2     2021-04-30 [3] CRAN (R 4.0.5)
 ps             1.6.0     2021-02-28 [3] CRAN (R 4.0.4)
 R6             2.5.1     2021-08-19 [3] CRAN (R 4.1.1)
 RColorBrewer   1.1-2     2014-12-07 [3] CRAN (R 4.0.0)
 Rcpp           1.0.8.3   2022-03-17 [1] CRAN (R 4.1.2)
 RcppEigen      0.3.3.9.1 2020-12-17 [1] CRAN (R 4.1.1)
 RcppParallel   5.1.4     2021-05-04 [1] CRAN (R 4.1.1)
 rlang          1.0.2     2022-03-04 [1] CRAN (R 4.1.2)
 rprojroot      2.0.2     2020-11-15 [3] CRAN (R 4.0.3)
 rstan          2.26.9    2022-04-07 [1] local
 scales         1.1.1     2020-05-11 [3] CRAN (R 4.0.0)
 StanHeaders    2.26.9    2022-04-07 [1] local
 tibble         3.1.6     2021-11-07 [1] CRAN (R 4.1.1)
 utf8           1.2.2     2021-07-24 [3] CRAN (R 4.1.0)
 V8             4.1.0     2022-02-06 [1] CRAN (R 4.1.2)
 vctrs          0.3.8     2021-04-29 [3] CRAN (R 4.0.5)
 viridisLite    0.4.0     2021-04-13 [3] CRAN (R 4.0.5)
 withr          2.5.0     2022-03-03 [1] CRAN (R 4.1.2)

 [1] /home/bruno/R/x86_64-pc-linux-gnu-library/4.1
 [2] /usr/local/lib/R/site-library
 [3] /usr/lib/R/site-library
 [4] /usr/lib/R/library

Tagging @hsbadr @andrjohns that might be able to help or at least probably know more.

I cannot replicate this on my machine with the following simple model:

library("brms")
fit1 <- brm(
  count ~ zAge + zBase + Trt,
  data = epilepsy, family = negbinomial(), 
  prior = prior(normal(0, 1), class = b),
  save_pars = save_pars(all = TRUE)
)
fit2 <- brm(
  count ~ zAge + Trt,
  data = epilepsy, family = negbinomial(), 
  prior = prior(normal(0, 1), class = b),
  save_pars = save_pars(all = TRUE)
)
bayes_factor(fit1, fit2)

System (I also re-installed stan with your code beforehand):

> sessionInfo()
R version 4.1.3 (2022-03-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.4 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_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8       
 [4] LC_COLLATE=en_GB.UTF-8     LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.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.8 

loaded via a namespace (and not attached):
  [1] nlme_3.1-155         matrixStats_0.61.0   xts_0.12.1          
  [4] threejs_0.3.3        rstan_2.26.9         tensorA_0.36.2      
  [7] tools_4.1.3          backports_1.4.1      utf8_1.2.2          
 [10] R6_2.5.1             DT_0.20              mgcv_1.8-39         
 [13] projpred_2.0.2       DBI_1.1.2            colorspace_2.0-2    
 [16] tidyselect_1.1.1     gridExtra_2.3        prettyunits_1.1.1   
 [19] processx_3.5.2       Brobdingnag_1.2-7    emmeans_1.7.2       
 [22] curl_4.3.2           compiler_4.1.3       cli_3.2.0           
 [25] shinyjs_2.1.0        sandwich_3.0-1       colourpicker_1.1.1  
 [28] posterior_1.2.0      scales_1.1.1         dygraphs_1.1.1.6    
 [31] checkmate_2.0.0      mvtnorm_1.1-3        ggridges_0.5.3      
 [34] callr_3.7.0          stringr_1.4.0        digest_0.6.29       
 [37] StanHeaders_2.26.9   minqa_1.2.4          base64enc_0.1-3     
 [40] pkgconfig_2.0.3      htmltools_0.5.2      lme4_1.1-28         
 [43] fastmap_1.1.0        htmlwidgets_1.5.4    rlang_1.0.2         
 [46] shiny_1.7.1          farver_2.1.0         generics_0.1.2      
 [49] zoo_1.8-9            jsonlite_1.7.3       crosstalk_1.2.0     
 [52] gtools_3.9.2         dplyr_1.0.8          distributional_0.3.0
 [55] inline_0.3.19        magrittr_2.0.1       loo_2.4.1           
 [58] bayesplot_1.8.1      Matrix_1.4-0         munsell_0.5.0       
 [61] fansi_1.0.2          abind_1.4-5          lifecycle_1.0.1     
 [64] stringi_1.7.6        multcomp_1.4-18      MASS_7.3-55         
 [67] pkgbuild_1.3.1       plyr_1.8.6           grid_4.1.3          
 [70] parallel_4.1.3       promises_1.2.0.1     crayon_1.5.0        
 [73] miniUI_0.1.1.1       lattice_0.20-45      splines_4.1.3       
 [76] ps_1.6.0             pillar_1.7.0         igraph_1.2.11       
 [79] boot_1.3-28          markdown_1.1         estimability_1.3    
 [82] shinystan_2.5.0      reshape2_1.4.4       codetools_0.2-18    
 [85] stats4_4.1.3         rstantools_2.1.1     glue_1.6.1          
 [88] V8_4.1.0             RcppParallel_5.1.5   nloptr_2.0.0        
 [91] vctrs_0.3.8          httpuv_1.6.5         gtable_0.3.0        
 [94] purrr_0.3.4          assertthat_0.2.1     ggplot2_3.3.5       
 [97] mime_0.12            xtable_1.8-4         coda_0.19-4         
[100] later_1.3.0          rsconnect_0.8.25     survival_3.2-13     
[103] tibble_3.1.6         shinythemes_1.2.0    gamm4_0.2-6         
[106] TH.data_1.1-0        ellipsis_0.3.2       bridgesampling_1.1-2

However, I have no Makevars file on that machine

ohh, it seems I forgot this!

It still strange to get a segfault, right?

The API access point to sample a Stan model from within R, rstan::log_prob(), which the bridgesampler needs to use, seems to not handle exceptions very well. And an input of wrong dimensionality which arises when parameters are missing, seems to be such a case.

2 Likes

I can’t replicate this with the experimental version too:

> packageVersion("rstan")
[1] ‘2.29.2.9000’
> bayes_factor(fit1, fit2)
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Estimated Bayes factor in favor of fit1 over fit2: 0.53900