Suddenly unable to compile Stan code using stan_model()

Suddenly I am no longer able to compile a Stan model that I have compiled successfully many times recently. I can sample() from the already previously compiled SDO versions, but stan_model() generates an error if I force it to recompile (e.g., by making a tiny innocuous change to the model).

The problem is not with my model. I also am now unable to compile the dinky little normal model that shows up as the default Stan model that appears in a new stan file under RStudio. Basically the same error message as below (somewhat less wordy but ends the same way).

I suspect this is due to my performing a blanket upgrade to all my R packages yesterday. Perhaps I dug myself a hole. But I need some help climbing out of it :-). I’m pretty much dead in the water now.

Any suggestions would be greatly appreciated so I can get back to work. Thank you!!

Below is the attempt to compile followed by the rather long and (for me) uninterpretable diagnostic message:

*> StabDSO ← stan_model( ‘UC3 Model for Fit exGAUS Stan Y prestudy no plate3-impact of prior.stan’ ) **
hash mismatch so recompiling; make sure Stan code ends with a blank line
make cmd is
make -f “C:/PROGRA~1/R/R-40~1.2/etc/x64/Makeconf” -f “C:/PROGRA~1/R/R-40~1.2/share/make/” CXX=’(CXX14) (CXX14STD)’ CXXFLAGS=’(CXX14FLAGS)' CXXPICFLAGS='(CXX14PICFLAGS)’ SHLIB_LDFLAGS=’(SHLIB_CXX14LDFLAGS)' SHLIB_LD='(SHLIB_CXX14LD)’ SHLIB=“file470135e76c0.dll” WIN=64 TCLBIN=64 OBJECTS=“file470135e76c0.o”
make would use
if test “zfile470135e76c0.o” != “z”; then
if test -e “file470135e76c0-win.def”; then
echo “C:/rtools40/mingw64/bin/“g++ -shared -s -static-libgcc -o file470135e76c0.dll file470135e76c0-win.def file470135e76c0.o “C:/Users/Dave/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a” -L"C:/Users/Dave/Documents/R/win-library/4.0/StanHeaders/libs/x64” -lStanHeaders -L"C:/Users/Dave/Documents/R/win-library/4.0/RcppParallel/lib/x64” -ltbb -L"C:/PROGRA~1/R/R-40~1.2/bin/x64" -lR ;
“C:/rtools40/mingw64/bin/“g++ -shared -s -static-libgcc -o file470135e76c0.dll file470135e76c0-win.def file470135e76c0.o “C:/Users/Dave/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a” -L"C:/Users/Dave/Documents/R/win-library/4.0/StanHeaders/libs/x64” -lStanHeaders -L"C:/Users/Dave/Documents/R/win-library/4.0/RcppParallel/lib/x64” -ltbb -L"C:/PROGRA~1/R/R-40~1.2/bin/x64" -lR ;
echo EXPORTS > tmp.def;
"C:/rtools40/mingw64/bin/"nm file470135e76c0.o | sed -n 's/^.
[BCDRT] / /p’ | sed -e ‘/[.]refptr[.]/d’ -e ‘/[.]weak[.]/d’ | sed ‘s/[^ ][^ ]*/"&"/g’ >> tmp.def;
echo “C:/rtools40/mingw64/bin/“g++ -shared -s -static-libgcc -o file470135e76c0.dll tmp.def file470135e76c0.o “C:/Users/Dave/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a” -L"C:/Users/Dave/Documents/R/win-library/4.0/StanHeaders/libs/x64” -lStanHeaders -L"C:/Users/Dave/Documents/R/win-library/4.0/RcppParallel/lib/x64” -ltbb -L"C:/PROGRA~1/R/R-40~1.2/bin/x64" -lR ;
“C:/rtools40/mingw64/bin/“g++ -shared -s -static-libgcc -o file470135e76c0.dll tmp.def file470135e76c0.o “C:/Users/Dave/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a” -L"C:/Users/Dave/Documents/R/win-library/4.0/StanHeaders/libs/x64” -lStanHeaders -L"C:/Users/Dave/Documents/R/win-library/4.0/RcppParallel/lib/x64” -ltbb -L"C:/PROGRA~1/R/R-40~1.2/bin/x64" -lR ;
rm -f tmp.def;
Error in compileCode(f, code, language = language, verbose = verbose) :
C:/rtools40/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/8.3.0/…/…/…/…/x86_64-w64-mingw32/bin/ld.exe: C:/Users/Dave/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a(stan_fit.o):stan_fit.cpp:(.data$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1[ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1]+0x0): multiple definition of boost::math::detail::bessel_j0<long double>(long double)::P1'; file470135e76c0.o:file470135e76c0.cpp:(.rdata$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1]+0x0): first defined hereC:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/Dave/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a(stan_fit.o):stan_fit.cpp:(.data$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2QS[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2QS]+0x0): multiple definition of boost::math::detail::bessel_j0(long double)::QS’; file470135e76c0.o:file470135e76c0.cpp
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
/bin/g++’ not found
Error in sink(type = “output”) : invalid connection

Below is what I as able to determine about my system configuration…

> sessionInfo()
RStudio version 1.3.1056
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

[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

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

other attached packages:
 [1] gridExtra_2.3        rstan_2.21.2         StanHeaders_2.21.0-7 qqplotr_0.0.5        outliers_0.14       
 [6] EnvStats_2.4.0       gamlss_5.3-4         nlme_3.1-148         gamlss.dist_5.3-2    MASS_7.3-51.6       
[11] gamlss.data_6.0-1    forcats_0.5.1        stringr_1.4.0        dplyr_1.0.7          purrr_0.3.4         
[16] readr_2.0.1          tidyr_1.1.3          tibble_3.1.4         ggplot2_3.3.5        tidyverse_1.3.1     
[21] readxl_1.3.1        

loaded via a namespace (and not attached):
 [1] httr_1.4.2         jsonlite_1.7.2     modelr_0.1.8       RcppParallel_5.1.4 assertthat_0.2.1   stats4_4.0.2      
 [7] cellranger_1.1.0   robustbase_0.93-8  pillar_1.6.2       backports_1.2.1    lattice_0.20-41    glue_1.4.2        
[13] rvest_1.0.1        colorspace_2.0-2   Matrix_1.2-18      pkgconfig_2.0.3    broom_0.7.9        haven_2.4.3       
[19] scales_1.1.1       processx_3.5.2     tzdb_0.1.2         generics_0.1.0     ellipsis_0.3.2     withr_2.4.2       
[25] cli_3.0.1          survival_3.1-12    magrittr_2.0.1     crayon_1.4.1       ps_1.6.0           fs_1.5.0          
[31] fansi_0.5.0        xml2_1.3.2         pkgbuild_1.2.0     loo_2.4.1          tools_4.0.2        prettyunits_1.1.1 
[37] hms_1.1.0          matrixStats_0.60.1 lifecycle_1.0.0    V8_3.4.2           munsell_0.5.0      reprex_2.0.1      
[43] callr_3.7.0        compiler_4.0.2     rlang_0.4.11       grid_4.0.2         rstudioapi_0.13    codetools_0.2-16  
[49] gtable_0.3.0       curl_4.3.2         inline_0.3.19      DBI_1.1.1          R6_2.5.1           lubridate_1.7.10  
[55] utf8_1.2.2         stringi_1.7.4      Rcpp_1.0.7         vctrs_0.3.8        DEoptimR_1.0-9     dbplyr_2.1.1      
[61] tidyselect_1.1.1  

> stan_version()
[1] "2.21.0"

NOTE ADDED: My rubber ducky suggested I take a look at RStan Getting Started · stan-dev/rstan Wiki · GitHub . Scary stuff… but I was desperate. I tried a few things that failed miserably, then saw on the following advice…

Then restart R and reinstall rstan and StanHeaders from source:

Compile packages using all cores Sys.setenv(MAKEFLAGS = paste0("-j",parallel::detectCores())) install.packages(c(“StanHeaders”,“rstan”),type=“source”)

So what the heck… brushed my teeth while it screamed on all 4 cores… now the example code and my own code compiles. I have no idea why this did what it did. What on earth is a “StanHeader”? Any thoughts appreciated. Thank you ducky!!

1 Like

Glad you were able to resolve this (am I right that everything works now, right?)

I also don’t understand what could have been the problem, but most likely something got upgraded in the meantime and your setup was for some reason broken.

The only thing I can say for sure is that StanHeaders is a package that houses some of the C++ math code needed to run Stan (which is then also used by other packages that actually don’t use Stan) and is a requirement of rstan.

Hey thank you Martin. Yes things have gone well after following the full guidance for reloading Stan and Stan headers. I guess the lesson is to follow the instructions even when they strike terror in the hearts on nervous Nelly’s like myself ;-).

Thanks, Dave

1 Like