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")
4 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?

Hello,
I was having the same problem with Rstan version 2.12.2 not compiling and tried reverting to 2.19.3 with the code you sent but it gives me this error:

packageurl ← “http://cran.r-project.org/src/contrib/Archive/rstan/rstan_2.19.3.tar.gz

install.packages(packageurl, repos = NULL, type = “source”)
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

clang++ -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"…/inst/include" -I"." -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/RcppEigen/include" -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include" -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -fPIC -Wall -g -O2 -c chains.cpp -o chains.o

clang: warning: no such sysroot directory: ‘/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk’ [-Wmissing-sysroot]
In file included from chains.cpp:19:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/mean.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/stan/math/prim/arr/err/check_nonzero_size.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/stan/math/prim/meta.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/stan/math/prim/arr/meta/get.hpp:4:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/…/include/c++/v1/cstdlib:85:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/…/include/c++/v1/stdlib.h:93:15: fatal error: ‘stdlib.h’ file not found
#include_next <stdlib.h>
^~~~~~~~~~

clang++ -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"…/inst/include" -I"." -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/RcppEigen/include" -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include" -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -fPIC -Wall -g -O2 -c init.cpp -o init.o

clang: warning: no such sysroot directory: ‘/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk’ [-Wmissing-sysroot]
In file included from init.cpp:25:
In file included from /Library/Frameworks/R.framework/Resources/include/R.h:47:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/…/include/c++/v1/cstdlib:85:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/…/include/c++/v1/stdlib.h:93:15: fatal error: ‘stdlib.h’ file not found
#include_next <stdlib.h>
^~~~~~~~~~
1 error generated.

make: *** [init.o] Error 1
make: *** Waiting for unfinished jobs…
1 error generated.
make: *** [chains.o] Error 1
ERROR: compilation failed for package ‘rstan’

  • removing ‘/Library/Frameworks/R.framework/Versions/3.6/Resources/library/rstan’
    Warning in install.packages :
    installation of package ‘/var/folders/_v/_dp_x44j2_gb3fg47_bgdrxw0000gn/T//Rtmp0q7TYZ/downloaded_packages/rstan_2.19.3.tar.gz’ had non-zero exit status

I’m using macOS Catalina version 10.15.6
brms version: 2.13.5
R version: 3.6.0

Thanks

Hello, I’m also having problems rolling back:

g++ -m64 -std=c++1y -I"C:/PROGRA~1/R/R-36~1.3/include" -DNDEBUG -I"…/inst/include" -I"." -I"D:/Documents/R/win-library/3.6/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I"D:/Documents/R/win-library/3.6/Rcpp/include" -I"D:/Documents/R/win-library/3.6/RcppEigen/include" -I"D:/Documents/R/win-library/3.6/BH/include" -I"D:/Documents/R/win-library/3.6/StanHeaders/include" -O3 -march=native -c sparse_extractors.cpp -o sparse_extractors.o
sparse_extractors.cpp:1:0: sorry, unimplemented: 64-bit mode not compiled in

^
make: *** [C:/PROGRA~1/R/R-36~1.3/etc/x64/Makeconf:215: sparse_extractors.o] Error 1
ERROR: compilation failed for package ‘rstan’

  • removing ‘D:/Documents/R/win-library/3.6/rstan’
  • restoring previous ‘D:/Documents/R/win-library/3.6/rstan’
    Warning in install.packages :
    installation of package ‘C:/Users/Alasdair/AppData/Local/Temp/RtmpYjZ4zU/downloaded_packages/rstan_2.19.3.tar.gz’ had non-zero exit status

This error implies that the right compiler (64-bit) isn’t being found. To help figure out why, can you post the outputs from:

Sys.getenv("PATH")
Sys.getenv("BINPREF")
readLines("~/.R/Makevars.win")
readLines("~/.Rprofile")
readLines("~/.Renviron")

Thanks for your help! I have everything nicely set up and working on my work Imac (which i’ve been working on all summer) and Win10 laptop. Now trying to get set up for the autumn on my home desktop, and running into all sorts of problems with the new versions of R, RTools, etc.

Sys.getenv(“PATH”)
[1] “C:\rtools\usr\bin;C:\Program Files\R\R-3.6.3\bin\x64;C:\Rtools\bin;c:\Rtools\bin;c:\Rtools\mingw_32\bin;C:\ProgramData\Oracle\Java\javapath;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\GtkSharp\2.12\bin;C:\Windows\System32\OpenSSH\;c:\Users\Alasdair\AppData\Local\enthought\Canopy\User\Scripts;C:\Users\Alasdair\AppData\Local\Enthought\Canopy\User;C:\Users\Alasdair\AppData\Local\Enthought\Canopy\User\Scripts;C:\Users\Alasdair\AppData\Local\Microsoft\WindowsApps;C:\Users\Alasdair\AppData\Local\GitHubDesktop\bin”
Sys.getenv(“BINPREF”)
[1] “”
readLines("~/.R/Makevars.win")
[1] “”
[2] “CXX14FLAGS=-O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2”
[3] “”
[4] “CXX14FLAGS=-O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2”
[5] “”
[6] “CXX14FLAGS=-O3 -march=native”
[7] “CXX14 = g++ -m$(WIN) -std=c++1y”
[8] “CXX11FLAGS=-O3 -march=native”
[9] “”
[10] “CXX14FLAGS=-O3 -march=corei7 -mtune=corei7”
[11] “CXX14 = (BINPREF)g++ -m(WIN) -std=c++1y”
[12] “CXX11FLAGS=-O3 -march=corei7 -mtune=corei7”
readLines("~/.Rprofile")
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘D:/Documents/.Rprofile’: No such file or directory
readLines("~/.Renviron")
[1] “PATH=“C:\rtools\usr\bin;${PATH}””

Alright, the first step is to tell R where to look for the compiler. You can do this by setting the BINPREF environment variable using a .Rprofile file. You can create this through R by running:

cat('Sys.setenv(BINPREF = "C:/Rtools/mingw_$(WIN)/bin/")',
    file = file.path(Sys.getenv("HOME"), ".Rprofile"), 
    sep = "\n", append = FALSE)

Then restart R and test that your toolchain is working by trying to compile a package from source:

install.packages("jsonlite",type="source")

Looks like that worked?

What should I try next?

And, while i have your attention, is there a good, up-to-date guide about how to get things working on Win10, and what versions of are required? (All my problems started when I foolishly decided to update R to the current version!)

I’m going to be teaching Statistical Rethinking this academic term, and providing tech support to students is going to be a nightmare. [Grumble grumble about Covid closing the computer labs…]

Sorry I missed your reply. If that works you should be good to go to install/run RStan.

As for an up-to-date guide, the Install Instructions on the RStan wiki are the closest thing: https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started

But they could do with some coverage of common issues and how to resolve them. It’s been on my to-do-list for a little while, but I’ll try and get them updated over the next few days