Potential Problems with rstan and /or brms packages in R 4.0.2

I ran the following models in R studio with brms:

> m3 <-brm(weight ~ Time, ChickWeight)
Compiling the C++ model
> m3
Error: object 'm3' not found

>m2 <- brm(f2 + f3 + set_rescor(FALSE), data = dat, chains= 1, cores = 1)
Compiling the C++ model
> m2
Error: object 'm2' not found

The brms variables containing the models are not in the R studio environment. Also, the variables are not found when typing:

traceback(m3)
Error in .is.positive.intlike(x) : object ‘m3’ not found

Operating System: Windows 10
brms Version: 2.13.0
R version: 4.02

Thanks in advance

which version of rstan and stanheaders have you installed?

I just updated StanHeaders and rstan from CRAN and ran into the same problem. Tagging @bgoodri

This seems to be an rstan / stanheaders issue. When I run the rstan example:

stanmodelcode <- "
data {
  int<lower=0> N;
  real y[N];
} 

parameters {
  real mu;
} 

model {
  target += normal_lpdf(mu | 0, 10);
  target += normal_lpdf(y  | mu, 1);
} 
"

y <- rnorm(20) 
dat <- list(N = 20, y = y); 
fit <- stan(model_code = stanmodelcode, model_name = "example", 
            data = dat, iter = 2012, chains = 3, verbose = TRUE,
            sample_file = file.path(tempdir(), 'norm.csv')) 
print(fit)

I get

Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: file10042d81dfb.o:file10042d81dfb.cpp:(.text+0x125c): undefined reference to `rstan::stan_fit::stan_fit(SEXPREC*, int)'
C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: file10042d81dfb.o:file10042d81dfb.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D1Ev[_ZN3tbb8internal26task_scheduler_observer_v3D1Ev]+0x14): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'
C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: file10042d81dfb.o:file10042d81dfb.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D0Ev[_ZN3tbb8internal26task_scheduler_observer_v3D0Ev]+0x1c): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'
C:/rtools40/mingw64/bin/../lib/gcc/x86_6
Zusätzlich: Warnmeldung:
In file.remove(c(unprocessed, processed)) :
  cannot erase file 'C:\Users\paulb\AppData\Local\Temp\Rtmpw5IjU2\file10041c494813.stan'. Reason: 'No such file or directory'

Here is the sessionInfo:

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    

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

other attached packages:
 [1] rstan_2.21.1         StanHeaders_2.21.0-5 TAM_3.5-19           CDM_7.5-15          
 [5] mvtnorm_1.1-1        lme4_1.1-23          Matrix_1.2-18        brms_2.13.0         
 [9] Rcpp_1.0.5           forcats_0.5.0        stringr_1.4.0        dplyr_1.0.0         
[13] purrr_0.3.4          readr_1.3.1          tidyr_1.1.0          tibble_3.0.2        
[17] ggplot2_3.3.2        tidyverse_1.3.0     

loaded via a namespace (and not attached):
  [1] minqa_1.2.4          colorspace_1.4-1     ellipsis_0.3.1       ggridges_0.5.2      
  [5] rsconnect_0.8.16     markdown_1.1         base64enc_0.1-3      fs_1.4.2            
  [9] rstudioapi_0.11      DT_0.14              fansi_0.4.1          lubridate_1.7.9     
 [13] xml2_1.3.2           bridgesampling_1.0-0 codetools_0.2-16     splines_4.0.2       
 [17] shinythemes_1.1.2    bayesplot_1.7.2      jsonlite_1.7.0       nloptr_1.2.2.2      
 [21] packrat_0.5.0        broom_0.5.6          dbplyr_1.4.4         shiny_1.5.0         
 [25] compiler_4.0.2       httr_1.4.1           backports_1.1.8      assertthat_0.2.1    
 [29] fastmap_1.0.1        cli_2.0.2            later_1.1.0.1        htmltools_0.5.0     
 [33] prettyunits_1.1.1    tools_4.0.2          igraph_1.2.5         coda_0.19-3         
 [37] gtable_0.3.0         glue_1.4.1           reshape2_1.4.4       V8_3.2.0            
 [41] cellranger_1.1.0     vctrs_0.3.1          nlme_3.1-148         crosstalk_1.1.0.1   
 [45] ps_1.3.3             rvest_0.3.5          mime_0.9             miniUI_0.1.1.1      
 [49] lifecycle_0.2.0      gtools_3.8.2         statmod_1.4.34       MASS_7.3-51.6       
 [53] zoo_1.8-8            scales_1.1.1         colourpicker_1.0     hms_0.5.3           
 [57] promises_1.1.1       Brobdingnag_1.2-6    parallel_4.0.2       inline_0.3.15       
 [61] shinystan_2.5.0      yaml_2.2.1           curl_4.3             gridExtra_2.3       
 [65] loo_2.3.0            stringi_1.4.6        dygraphs_1.1.1.6     polycor_0.7-10      
 [69] boot_1.3-25          pkgbuild_1.0.8       rlang_0.4.6          pkgconfig_2.0.3     
 [73] matrixStats_0.56.0   lattice_0.20-41      rstantools_2.1.1     htmlwidgets_1.5.1   
 [77] processx_3.4.3       tidyselect_1.1.0     plyr_1.8.6           magrittr_1.5        
 [81] R6_2.4.1             generics_0.0.2       DBI_1.1.0            pillar_1.4.4        
 [85] haven_2.3.1          withr_2.2.0          xts_0.12-0           abind_1.4-5         
 [89] modelr_0.1.8         crayon_1.3.4         utf8_1.1.4           grid_4.0.2          
 [93] readxl_1.3.1         blob_1.2.1           callr_3.4.3          threejs_0.3.3       
 [97] reprex_0.3.0         digest_0.6.25        xtable_1.8-4         httpuv_1.5.4        
[101] RcppParallel_5.0.2   stats4_4.0.2         munsell_0.5.0        shinyjs_1.1 

EDIT: I can get the Stan running again in the above environment by using rstan 2.19.3 instead of the latest rstan 2.21.1. So there indeed seems to be a problem in the latest rstan.

This is the same as the issue over in this thread: Error in compileCode(f, code, language = language, verbose = verbose) : Compilation ERROR, function(s)/method(s) not created! Again

It looks like there’s an issue with the call to the linker statement missing the TBB and StanHeaders flags

Thanks. Yes it indeed seems to be the same issue.

Thanks Paul and Andrjohns for replying. So, in the mean time, the solution is to use rstan version 2.19.3? Indeed I have been using version 2.21.1

Yes, reverting back to rstan 2.19.3 will fix the problem. As of now, rstan 2.21.1 is only available as source on CRAN so when you just uninstall rstan and install it again but then refuse to compile from source, it will give you rstan 2.19.3 directly. In a few days rstan 2.21.1 will also be available as compiled package in which case the approach above does not work anymore and you have to go for:

packageurl <- "http://cran.r-project.org/src/contrib/Archive/rstan/rstan_2.19.3.tar.gz"
install.packages(packageurl, repos = NULL, type = "source")
3 Likes

I do confirm you that reverting back to rstan 2.19.3 solved the issue. Thank you very much Paul. And have a good day.

1 Like

It would be nice to be notified when rstan is fixed, but I don’t know where to look for it.
Apparently, it seems that lots of topics have been opened, all of them pointing to the same problem, which makes it very difficult to follow.

2 Likes

This is where to look at:

Thank you for the suggestion. I have similar issues with brms after updating r and want to try reverting back to an earlier version of rstan. However, when I run the code you suggested, I get the following error. Any ideas of how to fix it? I tried googling but haven’t found a solution. Thanks!

Installing package into ‘C:/Users/Daphne Liu/Documents/R/win-library/4.0’
(as ‘lib’ is unspecified)
trying URL ‘http://cran.r-project.org/src/contrib/Archive/rstan/rstan_2.19.3.tar.gz
Content type ‘application/x-gzip’ length 922337 bytes (900 KB)
downloaded 900 KB

  • installing source package ‘rstan’ …
    ** package ‘rstan’ successfully unpacked and MD5 sums checked
    ** using staged installation
    ** libs

*** arch - i386

C:/Rtools/mingw_64/bin/g++.exe -m32 -std=c++1y -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.2/include" -DNDEBUG -I"…/inst/include" -I"." -I"C:/Users/Daphne Liu/Documents/R/win-library/4.0/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I’C:/Users/Daphne Liu/Documents/R/win-library/4.0/Rcpp/include’ -I’C:/Users/Daphne Liu/Documents/R/win-library/4.0/RcppEigen/include’ -I’C:/Users/Daphne Liu/Documents/R/win-library/4.0/BH/include’ -I’C:/Users/Daphne Liu/Documents/R/win-library/4.0/StanHeaders/include’ -O3 -march=native -c sparse_extractors.cpp -o sparse_extractors.o
sh: C:/Rtools/mingw_64/bin/g++.exe: No such file or directory
make: *** [C:/PROGRA~1/R/R-40~1.2/etc/i386/Makeconf:229: sparse_extractors.o] Error 127
ERROR: compilation failed for package ‘rstan’

  • removing ‘C:/Users/Daphne Liu/Documents/R/win-library/4.0/rstan’
  • restoring previous ‘C:/Users/Daphne Liu/Documents/R/win-library/4.0/rstan’
    Warning in install.packages :
    installation of package ‘C:/Users/DAPHNE~1/AppData/Local/Temp/RtmpCmzG1g/downloaded_packages/rstan_2.19.3.tar.gz’ had non-zero exit status

sh: C:/Rtools/mingw_64/bin/g++.exe: No such file or directory

It means that your file g++.exe doesn’t exist in this folder, you need to find where it was installed in your system.

I use Rtools40
https://cran.r-project.org/bin/windows/Rtools/
and the g++.exe file can be found in the directory

C:\rtools40\mingw64\bin

Maybe you could try to install Rtools40?