Error when running MCMC with Rstan

Im getting a new error which im not sure the source of, this is with script that had previously worked fine, but now i’ve installed Rstan on a new device and am not sure the parameters or source of this error message, the MCMC does converge and all chains finish successfully but before saving the output it gives this error.

#parameters
 fit<-model$sample(
    data = data,
    seed = 123,
    chains = 4,
    parallel_chains = 4,
    refresh = 500)
Error in if (any(efbmi_per_chain < threshold)) { : 
  missing value where TRUE/FALSE needed

UPDATE: Im able to have it run and save the output without an error when I tinker with the model parameter in a specific way.

  #only way it works!!!
  fit<-model$sample(
    data = data,
    seed = 123456,
    chains = 1,
    refresh = 500)

Anyone help or advice would be really appreciated

im using
Windows 11
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)

and

cmdstan_version()
[1] β€œ2.30.0”

and

writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars.win")))
CXX14=$(BINPREF)g++
CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=native -mtune=native

…and…

> devtools::session_info('rstan')
─ Session info ─────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.1 (2022-06-23 ucrt)
 os       Windows 10 x64 (build 22000)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  English_United Kingdom.utf8
 ctype    English_United Kingdom.utf8
 tz       Asia/Taipei
 date     2022-07-21
 rstudio  2022.02.3+492 Prairie Trillium (desktop)
 pandoc   NA

─ Packages ─────────────────────────────────────────────────────────────────────────────────────────────────
 package      * version   date (UTC) lib source
 backports      1.4.1     2021-12-13 [1] CRAN (R 4.2.0)
 BH             1.78.0-0  2021-12-15 [1] CRAN (R 4.2.0)
 callr          3.7.0     2021-04-20 [1] CRAN (R 4.2.0)
 checkmate      2.1.0     2022-04-21 [1] CRAN (R 4.2.1)
 cli            3.3.0     2022-04-25 [1] CRAN (R 4.2.0)
 colorspace     2.0-3     2022-02-21 [1] CRAN (R 4.2.0)
 crayon         1.5.1     2022-03-26 [1] CRAN (R 4.2.0)
 desc           1.4.1     2022-03-06 [1] CRAN (R 4.2.0)
 digest         0.6.29    2021-12-01 [1] CRAN (R 4.2.0)
 ellipsis       0.3.2     2021-04-29 [1] CRAN (R 4.2.0)
 fansi          1.0.3     2022-03-24 [1] CRAN (R 4.2.0)
 farver         2.1.0     2021-02-28 [1] CRAN (R 4.2.0)
 ggplot2        3.3.6     2022-05-03 [1] CRAN (R 4.2.0)
 glue           1.6.2     2022-02-24 [1] CRAN (R 4.2.0)
 gridExtra      2.3       2017-09-09 [1] CRAN (R 4.2.0)
 gtable         0.3.0     2019-03-25 [1] CRAN (R 4.2.0)
 inline         0.3.19    2021-05-31 [1] CRAN (R 4.2.1)
 isoband        0.2.5     2021-07-13 [1] CRAN (R 4.2.0)
 labeling       0.4.2     2020-10-20 [1] CRAN (R 4.2.0)
 lattice        0.20-45   2021-09-22 [2] CRAN (R 4.2.1)
 lifecycle      1.0.1     2021-09-24 [1] CRAN (R 4.2.0)
 loo            2.5.1     2022-03-24 [1] CRAN (R 4.2.1)
 magrittr       2.0.3     2022-03-30 [1] CRAN (R 4.2.0)
 MASS           7.3-57    2022-04-22 [2] CRAN (R 4.2.1)
 Matrix         1.4-1     2022-03-23 [2] CRAN (R 4.2.1)
 matrixStats    0.62.0    2022-04-19 [1] CRAN (R 4.2.1)
 mgcv           1.8-40    2022-03-29 [2] CRAN (R 4.2.1)
 munsell        0.5.0     2018-06-12 [1] CRAN (R 4.2.0)
 nlme           3.1-157   2022-03-25 [2] CRAN (R 4.2.1)
 pillar         1.7.0     2022-02-01 [1] CRAN (R 4.2.0)
 pkgbuild       1.3.1     2021-12-20 [1] CRAN (R 4.2.1)
 pkgconfig      2.0.3     2019-09-22 [1] CRAN (R 4.2.0)
 prettyunits    1.1.1     2020-01-24 [1] CRAN (R 4.2.0)
 processx       3.6.1     2022-06-17 [1] CRAN (R 4.2.0)
 ps             1.7.1     2022-06-18 [1] CRAN (R 4.2.0)
 R6             2.5.1     2021-08-19 [1] CRAN (R 4.2.0)
 RColorBrewer   1.1-3     2022-04-03 [1] CRAN (R 4.2.0)
 Rcpp           1.0.8.3   2022-03-17 [1] CRAN (R 4.2.0)
 RcppEigen      0.3.3.9.2 2022-04-08 [1] CRAN (R 4.2.0)
 RcppParallel   5.1.5     2022-01-05 [1] CRAN (R 4.2.1)
 rlang          1.0.3     2022-06-27 [1] CRAN (R 4.2.1)
 rprojroot      2.0.3     2022-04-02 [1] CRAN (R 4.2.0)
 rstan          2.21.5    2022-04-11 [1] CRAN (R 4.2.1)
 scales         1.2.0     2022-04-13 [1] CRAN (R 4.2.0)
 StanHeaders    2.21.0-7  2020-12-17 [1] CRAN (R 4.2.1)
 tibble         3.1.7     2022-05-03 [1] CRAN (R 4.2.0)
 utf8           1.2.2     2021-07-24 [1] CRAN (R 4.2.0)
 vctrs          0.4.1     2022-04-13 [1] CRAN (R 4.2.0)
 viridisLite    0.4.0     2021-04-13 [1] CRAN (R 4.2.0)
 withr          2.5.0     2022-03-03 [1] CRAN (R 4.2.0)

 [1] C:/Users/Users/AppData/Local/R/win-library/4.2
 [2] C:/Program Files/R/R-4.2.1/library
1 Like

RStan shouldn’t be failing like this. It’s a bug.

I’m pinging @bgoodri, but the best place to report is in the RStan issue tracker:

1 Like

Was this ever fixed? Or is it tracked somewhere in an issue? I get the same error (Error in if (any(efbmi_per_chain < threshold)) { : missing value where TRUE/FALSE needed) randomly. That is, if I run the sampler with the same data & model, it may happen. I suppose this happens because of the changes in R 4.2.0:

  • Calling if() or while() with a condition of length greater than one gives an error rather than a warning. Consequently, environment variable R_CHECK_LENGTH_1_CONDITION no longer has any effect.

https://cran.r-project.org/doc/manuals/r-release/NEWS.html

No I still havent found a reliable solution to it

Can share a dataset and model that reproduces the error?

I ran into this error, and an example that produces it can be found here https://drive.google.com/file/d/1JvJv6wTbadyhyPfIHZpdIVnzss5E0SoS/view?usp=drive_link. I took the model from here http://srmart.in/informative-priors-for-correlation-matrices-an-easy-approach/, and simulated some data with a different covariance matrix and higher dimensions. I’m playing around a bit with the model, and the error comes upt sometimes. In the attached zp file are output files generated via cmdstan. When I try to read them into R via as_cmdstan_fit, I get the error

Error in if (any(efbmi_per_chain < threshold)) { :
missing value where TRUE/FALSE needed
(I also get treedepth warnings). Depending on the simulated data/seed, I also sometimes get the same error when I run directly in R with cmdstanR. But I hope the output files are sufficient te see what’s going on (t takes a long time to run).

I can’t replicate your error locally. Can you try with the Github cmdstanr:

# install.packages("remotes")
remotes::install_github("stan-dev/cmdstanr")

Thanks for looking into it so quickly. Using the gtihub cmdstanr fixes it. Should have done that earlier. Thanks.

1 Like

No worries, great to hear that it’s all working!