stan-dev / rstan

RStan, the R interface to Stan
https://mc-stan.org
1.04k stars 265 forks source link

Error in sampler$call_sampler #994

Open gowerc opened 2 years ago

gowerc commented 2 years ago

When running rstan::sampling() I have just started getting the following error message:

[1] "Error in sampler$call_sampler(args_list[[i]]) : "
error occurred during calling the sampler; sampling not done

Please see the comment below for more details

~Our stan code hasn't changed in the last 6 months so assume this is related to an updated version of stan / rstan / local compiler change ? The following below is a minimal example code that I was able to use to create the problem, that being said it is super inconsistant, 80% of the time it works fine and 20% of the time it kicks up the above error. Once its kicked up the error it won't fix itself until I restart R. The issue seems to be related to loading our R package via rstantools but I've not been able to consistently reproduce the problem nor diagnose whats triggering it.~

~Any help / advice would be appreciated.~

Version Information

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

[1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library

gowerc commented 2 years ago

Potentially linked to #986 & #973

gowerc commented 2 years ago

~Made some small progress on this, realised that if the model is compiled before loading our package via devtools::load_all() then it works if the model is compiled after loading our package via devtools::load_all() then we get the aforementioned error. For reference this isn't just for the stan model within our package, all models compiled by stan_model() fail after having loaded our package. Also for reference our package just uses the default process from rstantools for loading the stan model (we haven't added any customisations), our package also doesn't set any global options nor run any onload() / zzz code.~

gowerc commented 2 years ago

Ok, the issue was due to a dependency package of ours called glmmTMB. This package had been incorrectly installed and for some reason when loading it it would cause all STAN compiled models to fail.

That is to say:

library(rstan)

stancode <- "
data { real y[100]; }
parameters { real mu; real<lower=0> sigma; }
model { y ~ normal(mu,sigma);  }
"

model <- stan_model(model_code = stancode)

fit <- sampling(
    model,
    list(y = rnorm(100, 1.6, 0.2)),
    iter = 200,
    chains = 2,
    refresh = 0
)

Would work fine, whilst:

library(rstan)

stancode <- "
data { real y[100]; }
parameters { real mu; real<lower=0> sigma; }
model { y ~ normal(mu,sigma);  }
"

library(glmmTMB)

model <- stan_model(model_code = stancode)

fit <- sampling(
    model,
    list(y = rnorm(100, 1.6, 0.2)),
    iter = 200,
    chains = 2,
    refresh = 0
)

Would fail with the aforementioned error message. Re-compiling glmmTMB from source resolved the problem.

I guess the issue is solved so feel free to close it however I will leave it open for now just encase you wish to look into it as it seems strange to me that loading an unrelated package would cause all STAN models to fail.