Stan() function throws "cannot open the connection" error

Hi Team,

Thanks ahead of time for the assistance.

I’m using stan_glmer() within R to run some hierarchical models. I need to implement a floor of 0 on coefficients. To do so, my plan was to extract the Stan code generated, modify it, and then re-run the modified code using stan().

The stan_glmer() piece runs fine:

model <- stan_glmer( <model details> )

I then extract the Stan code and have tried to save both of these outputs as stan_code.stan files in my working directory for the next part:

  • model[[“stanfit”]]@stanmodel@model_code
  • model[[“stanfit”]]@stanmodel@model_cpp[[“model_cppcode”]]

I then run the below command, without modifying the Stan code:

model1 <- stan(
  file = here::here("data/stan_code.stan"),
  data = df_regression,
  chains = 2,
  warmup = 1000,
  iter = 2000,
  cores = 2
)

Here’s the error I get:

Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file '/model/eta_z_no_intercept.stan': No such file or directory

─ Session info
setting value
version R version 4.2.2 (2022-10-31)
os macOS Ventura 13.3.1
system x86_64, darwin17.0
ui RStudio
language (EN)
collate en_US.UTF-8
ctype en_US.UTF-8
tz America/Chicago
date 2023-10-19
rstudio 2023.06.0+421 Mountain Hydrangea (desktop)
pandoc NA

─ Packages
package * version date (UTC) lib source
backports 1.4.1 2021-12-13 [2] CRAN (R 4.2.0)
BH 1.81.0-1 2023-01-22 [2] CRAN (R 4.2.0)
callr 3.7.3 2022-11-02 [2] CRAN (R 4.2.0)
checkmate 2.1.0 2022-04-21 [2] CRAN (R 4.2.0)
cli 3.6.0 2023-01-09 [2] CRAN (R 4.2.0)
colorspace 2.1-0 2023-01-23 [2] CRAN (R 4.2.0)
crayon 1.5.2 2022-09-29 [2] CRAN (R 4.2.0)
desc 1.4.2 2022-09-08 [2] CRAN (R 4.2.0)
fansi 1.0.4 2023-01-22 [2] CRAN (R 4.2.0)
farver 2.1.1 2022-07-06 [2] CRAN (R 4.2.0)
ggplot2 * 3.4.1 2023-02-10 [2] CRAN (R 4.2.0)
glue 1.6.2 2022-02-24 [2] CRAN (R 4.2.0)
gridExtra 2.3 2017-09-09 [2] CRAN (R 4.2.0)
gtable 0.3.1 2022-09-01 [2] CRAN (R 4.2.0)
inline 0.3.19 2021-05-31 [1] CRAN (R 4.2.0)
isoband 0.2.7 2022-12-20 [2] CRAN (R 4.2.0)
jsonlite 1.8.4 2022-12-06 [2] CRAN (R 4.2.0)
labeling 0.4.2 2020-10-20 [2] CRAN (R 4.2.0)
lattice 0.20-45 2021-09-22 [2] CRAN (R 4.2.2)
lifecycle 1.0.3 2022-10-07 [2] CRAN (R 4.2.0)
loo 2.5.1 2022-03-24 [2] CRAN (R 4.2.0)
magrittr 2.0.3 2022-03-30 [2] CRAN (R 4.2.0)
MASS 7.3-58.1 2022-08-03 [2] CRAN (R 4.2.2)
Matrix 1.5-1 2022-09-13 [2] CRAN (R 4.2.2)
matrixStats 1.0.0 2023-06-02 [1] CRAN (R 4.2.0)
mgcv 1.8-41 2022-10-21 [2] CRAN (R 4.2.2)
munsell 0.5.0 2018-06-12 [2] CRAN (R 4.2.0)
nlme 3.1-160 2022-10-10 [2] CRAN (R 4.2.2)
pillar 1.8.1 2022-08-19 [2] CRAN (R 4.2.0)
pkgbuild 1.4.0 2022-11-27 [2] CRAN (R 4.2.0)
pkgconfig 2.0.3 2019-09-22 [2] CRAN (R 4.2.0)
prettyunits 1.1.1 2020-01-24 [2] CRAN (R 4.2.0)
processx 3.8.0 2022-10-26 [2] CRAN (R 4.2.0)
ps 1.7.2 2022-10-26 [2] CRAN (R 4.2.0)
QuickJSR 1.0.7 2023-10-15 [1] CRAN (R 4.2.0)
R6 2.5.1 2021-08-19 [2] CRAN (R 4.2.0)
RColorBrewer 1.1-3 2022-04-03 [2] CRAN (R 4.2.0)
Rcpp * 1.0.10 2023-01-22 [2] CRAN (R 4.2.0)
RcppEigen 0.3.3.9.3 2022-11-05 [2] CRAN (R 4.2.0)
RcppParallel 5.1.6 2023-01-09 [2] CRAN (R 4.2.0)
rlang 1.1.1 2023-04-28 [1] CRAN (R 4.2.0)
rprojroot 2.0.3 2022-04-02 [2] CRAN (R 4.2.0)
rstan * 2.32.3 2023-10-15 [1] CRAN (R 4.2.2)
scales 1.2.1 2022-08-20 [2] CRAN (R 4.2.0)
StanHeaders * 2.26.28 2023-09-07 [1] CRAN (R 4.2.0)
tibble * 3.1.8 2022-07-22 [2] CRAN (R 4.2.0)
utf8 1.2.3 2023-01-31 [2] CRAN (R 4.2.0)
vctrs 0.5.2 2023-01-23 [2] CRAN (R 4.2.0)
viridisLite 0.4.1 2022-08-22 [2] CRAN (R 4.2.0)
withr 2.5.0 2022-03-03 [2] CRAN (R 4.2.0)

This looks like there are includes in the code generated by rstanarm and that those are not where they are expected to be for RStan. Can you inspect the code and see if that’s the case?

If so, then you need to find those include files and put them somewhere that RStan can find them. Easiest if you just put them in the same directory as the .stan file and shorten all the includes to not have any paths. In any case, you don’t want to be trying to open a top level /model file as your OS won’t have a top-level /model directory.