Problems getting Stan to run after upgrading to R 4.0.2

Thanks @andrjohns, there is now other errors and the model finish running.

Hi Andrew! :)
How are you? How are things in Aussie?

I am pretty new to R and have hardly any experience with coding.
I wanted to run a brm model through the package “brms” it uses Stan. I got the same message like Chris at first. Then I copy pasted everything code you suggested and also created the folder with markevars.win (I created a text file, or what sort of file is this meant to be?)
and after running > cat(“CXX14FLAGS += -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2”,file = “~/.R/Makevars.win”, sep = “\n”, append = FALSE)

library(rstan)
example(stan_model,run.dontrun=TRUE,verbose=TRUE)

I get
Error in cleanup_makevar(old) :
argument “RMU” is missing, with no default
In addition: Warning messages:
1: In find.package(package, lib.loc, verbose = verbose) :
package ‘base’ found more than once, using the first from
“C:/PROGRA~1/R/R-40~1.4/library/base”,
“C:/Program Files/R/R-4.0.4/library/base”
2: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘-E’ not found

would you be able to help here, please?
best wishes,
Adele

This error occurs when your withr package is at a version greater than 2.2.0. You just need to downgrade your package using:

devtools::install_version("withr", version="2.2.0")

Then make sure not to update. It’s a bit annoying but will be fixed in the next release

Hey andrjohns,
I have a very similar problem and can’t solve it.

Executing
example(stan_model, run.dontrun = TRUE, verbose=TRUE)
returns
Error in prep_call_sampler(object) : could not find function "prep_call_sampler" In addition: Warning messages: 1: In find.package(package, lib.loc, verbose = verbose) : package ‘base’ found more than once, using the first from “C:/PROGRA~1/R/R-40~1.4/library/base”, “C:/Program Files/R/R-4.0.4/library/base” 2: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) : '-E' not found

So what is happening here? I tried installing rstan on different ways (with install.rstan and from source), doublechecked those installations, removed them and reinstalled them, with and without loading library.
devtools::install_version("withr", version="2.2.0")
returns
Error in loadNamespace(name) : there is no package called ‘devtools’

What can I do to finally get Stan working?

Btw: there is no .R folder in my documents and I cannot create one of such name as Windows prohibits the dot in folder names. I also tried adding the makevars file in /Documents/R/ but did not quite grasp of what type this file should be. Creating a .txt and just naming it Makevars.win was not the solution.

I need a bit more information about your system. Can you post the outputs from:

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

Output is:

Sys.getenv(“BINPREF”)
[1] “”
Sys.getenv(“PATH”)
[1] “C:\Program Files\R\R-4.0.4\bin\x64;C:\ProgramData\Oracle\Java\javapath;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Users\Anekdoteles\AppData\Local\Programs\Python\;C:\Users\Anekdoteles\AppData\Local\Programs\Python\Python38-32\Scripts;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Users\Anekdoteles\AppData\Local\Programs\Python\Launcher\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Users\Anekdoteles\AppData\Local\Programs\MiKTeX\miktex\bin\x64\;C:\Users\Anekdoteles\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64\”
readLines(“~/.R/Makevars.win”)
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘C:/Users/Anekdoteles/Documents/.R/Makevars.win’: No such file or directory
readLines(“~/.Renviron”)
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘C:/Users/Anekdoteles/Documents/.Renviron’: No such file or directory
readLines(“~/.Rprofile”)
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘C:/Users/Anekdoteles/Documents/.Rprofile’: No such file or directory

It looks like you haven’t completed the Getting Started instructions in the RStan wiki to prepare your system.

Can you complete the Instructions for Windows and then install RStan from source via:

# Compile packages using all cores
Sys.setenv(MAKEFLAGS = paste0("-j",parallel::detectCores()))

install.packages(c("StanHeaders","rstan"),type="source")

I followed complete instructions and ignored that I didn’t get the error messages that the link you provided suggested would make the following steps necessary. Sys.which("make") also seems to work yet executing install.packages("jsonlite", type="source") sends me on an endless cycle of a warning asking me to resart R prior to installation. I negated that to break the circle. I executed library("rstan") and library(rstan) just to be sure which didn’t produce an error.

I executed example(stan_model, package = "rstan", run.dontrun = TRUE)
and return was
Error in prep_call_sampler(object) :
could not find function “prep_call_sampler”
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/rtools40/usr/mingw_/bin/g++’ not found

Let’s debug the jsonlite install first, since we need to make sure that your RTools installation is accessible from R.

In a clean R session (with RStan not loaded) can you try to remove any existing RStan install via

remove.packages("rstan")
if (file.exists(".RData")) file.remove(".RData")

Then, restart R and try the jsonlite install again

I executed the code and reinstalled jsonlite via "install.packages("jsonlite", type = "source"). But now what?

Great, if that reinstalled without issue, then things are looking good. Next, can you try the rstan source install again:

# Compile packages using all cores
Sys.setenv(MAKEFLAGS = paste0("-j",parallel::detectCores()))

install.packages(c("StanHeaders","rstan"),type="source")

Executing the model again returns
Error in prep_call_sampler(object) :
could not find function “prep_call_sampler”
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/rtools40/usr/mingw_/bin/g++’ not found

Executing the model after library(rstan) commands returns
Error in prep_call_sampler(object) :
could not find function “prep_call_sampler”
In addition: Warning messages:
1: In find.package(package, lib.loc, verbose = verbose) :
package ‘base’ found more than once, using the first from
“C:/PROGRA~1/R/R-40~1.4/library/base”,
“C:/Program Files/R/R-4.0.4/library/base”
2: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/rtools40/usr/mingw_/bin/g++’ not found

Can you post the output from:

install.packages("devtools")
devtools::session_info("rstan")
package ‘devtools’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\Anekdoteles\AppData\Local\Temp\RtmpIzpUj5\downloaded_packages

executing devtools command:

- Session info --------------------------------------------------------------
 setting  value                       
 version  R version 4.0.4 (2021-02-15)
 os       Windows 7 x64 SP 1          
 system   x86_64, mingw32             
 ui       RStudio                     
 language (EN)                        
 collate  German_Germany.1252         
 ctype    German_Germany.1252         
 tz       Europe/Berlin               
 date     2021-03-15                  

- Packages ------------------------------------------------------------------
 ! package      * version   date       lib source        
   assertthat     0.2.1     2019-03-21 [1] CRAN (R 4.0.0)
   backports      1.2.1     2020-12-09 [1] CRAN (R 4.0.3)
   BH             1.75.0-0  2021-01-11 [1] CRAN (R 4.0.3)
   callr          3.5.1     2020-10-13 [1] CRAN (R 4.0.4)
   checkmate      2.0.0     2020-02-06 [1] CRAN (R 4.0.0)
   cli            2.3.1     2021-02-23 [1] CRAN (R 4.0.4)
   colorspace     2.0-0     2020-11-11 [1] CRAN (R 4.0.4)
   crayon         1.4.1     2021-02-08 [1] CRAN (R 4.0.4)
   curl           4.3       2019-12-02 [1] CRAN (R 4.0.0)
   desc           1.3.0     2021-03-05 [1] CRAN (R 4.0.4)
   digest         0.6.27    2020-10-24 [1] CRAN (R 4.0.4)
   ellipsis       0.3.1     2020-05-15 [1] CRAN (R 4.0.4)
   fansi          0.4.2     2021-01-15 [1] CRAN (R 4.0.4)
   farver         2.1.0     2021-02-28 [1] CRAN (R 4.0.4)
   ggplot2      * 3.3.3     2020-12-30 [1] CRAN (R 4.0.4)
   glue           1.4.2     2020-08-27 [1] CRAN (R 4.0.4)
   gridExtra      2.3       2017-09-09 [1] CRAN (R 4.0.0)
   gtable         0.3.0     2019-03-25 [1] CRAN (R 4.0.0)
   inline         0.3.17    2020-12-01 [1] CRAN (R 4.0.4)
   isoband        0.2.4     2021-03-03 [1] CRAN (R 4.0.4)
   jsonlite       1.7.2     2020-12-09 [1] CRAN (R 4.0.4)
   labeling       0.4.2     2020-10-20 [1] CRAN (R 4.0.3)
   lattice        0.20-41   2020-04-02 [2] CRAN (R 4.0.4)
   lifecycle      1.0.0     2021-02-15 [1] CRAN (R 4.0.4)
   loo            2.4.1     2020-12-09 [1] CRAN (R 4.0.4)
   magrittr       2.0.1     2020-11-17 [1] CRAN (R 4.0.4)
   MASS           7.3-53.1  2021-02-12 [1] CRAN (R 4.0.4)
   Matrix         1.2-18    2019-11-27 [1] CRAN (R 4.0.3)
   matrixStats    0.58.0    2021-01-29 [1] CRAN (R 4.0.4)
   mgcv           1.8-34    2021-02-16 [1] CRAN (R 4.0.4)
   munsell        0.5.0     2018-06-12 [1] CRAN (R 4.0.0)
   nlme           3.1-152   2021-02-04 [1] CRAN (R 4.0.4)
   pillar         1.5.1     2021-03-05 [1] CRAN (R 4.0.4)
   pkgbuild       1.2.0     2020-12-15 [1] CRAN (R 4.0.4)
   pkgconfig      2.0.3     2019-09-22 [1] CRAN (R 4.0.0)
   prettyunits    1.1.1     2020-01-24 [1] CRAN (R 4.0.0)
   processx       3.4.5     2020-11-30 [1] CRAN (R 4.0.4)
   ps             1.6.0     2021-02-28 [1] CRAN (R 4.0.4)
   R6             2.5.0     2020-10-28 [1] CRAN (R 4.0.4)
   RColorBrewer   1.1-2     2014-12-07 [1] CRAN (R 4.0.0)
   Rcpp           1.0.6     2021-01-15 [1] CRAN (R 4.0.4)
   RcppEigen      0.3.3.9.1 2020-12-17 [1] CRAN (R 4.0.4)
 D RcppParallel   5.0.3     2021-02-24 [1] CRAN (R 4.0.4)
   rlang          0.4.10    2020-12-30 [1] CRAN (R 4.0.4)
   rprojroot      2.0.2     2020-11-15 [1] CRAN (R 4.0.4)
   rstan        * 2.21.2    2020-07-27 [1] CRAN (R 4.0.4)
   scales         1.1.1     2020-05-11 [1] CRAN (R 4.0.4)
   StanHeaders  * 2.21.0-7  2020-12-17 [1] CRAN (R 4.0.4)
   tibble         3.1.0     2021-02-25 [1] CRAN (R 4.0.4)
   utf8           1.1.4     2018-05-24 [1] CRAN (R 4.0.4)
   V8             3.4.0     2020-11-04 [1] CRAN (R 4.0.4)
   vctrs          0.3.6     2020-12-17 [1] CRAN (R 4.0.4)
   viridisLite    0.3.0     2018-02-01 [1] CRAN (R 4.0.0)
   withr          2.4.1     2021-01-26 [1] CRAN (R 4.0.4)

[1] C:/Users/Anekdoteles/Documents/R/win-library/4.0
[2] C:/Program Files/R/R-4.0.4/library

 D -- DLL MD5 mismatch, broken installation.

I can’t see anything obviously wrong there, let’s try a basic model to see if that will work.

First you’ll need to downgrade your withr package or you’ll run into issues:

devtools::install_version("withr",version="2.2.0")

Next, restart R and then run:

modcode = "data{real y_mean;} parameters{real y;} model{y~normal(y_mean,1);}"

library(rstan)
rstan_options(auto_write=FALSE)
stan(model_code=modcode,data=list(y_mean=0))

Does that error?

It does show a warning

    > stan(model_code=modcode,data=list(y_mean=0))
recompiling to avoid crashing R session

SAMPLING FOR MODEL 'efb77423d7adb44255913eac3d9377e8' NOW (CHAIN 1).
Chain 1: 
Chain 1: Gradient evaluation took 0 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 1: Adjust your expectations accordingly!
Chain 1: 
Chain 1: 
Chain 1: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 1: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 1: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 1: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 1: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 1: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 1: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 1: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 1: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 1: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 1: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 1: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 1: 
Chain 1:  Elapsed Time: 0.011 seconds (Warm-up)
Chain 1:                0.013 seconds (Sampling)
Chain 1:                0.024 seconds (Total)
Chain 1: 

SAMPLING FOR MODEL 'efb77423d7adb44255913eac3d9377e8' NOW (CHAIN 2).
Chain 2: 
Chain 2: Gradient evaluation took 0 seconds
Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 2: Adjust your expectations accordingly!
Chain 2: 
Chain 2: 
Chain 2: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 2: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 2: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 2: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 2: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 2: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 2: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 2: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 2: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 2: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 2: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 2: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 2: 
Chain 2:  Elapsed Time: 0.014 seconds (Warm-up)
Chain 2:                0.016 seconds (Sampling)
Chain 2:                0.03 seconds (Total)
Chain 2: 

SAMPLING FOR MODEL 'efb77423d7adb44255913eac3d9377e8' NOW (CHAIN 3).
Chain 3: 
Chain 3: Gradient evaluation took 0 seconds
Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 3: Adjust your expectations accordingly!
Chain 3: 
Chain 3: 
Chain 3: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 3: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 3: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 3: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 3: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 3: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 3: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 3: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 3: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 3: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 3: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 3: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 3: 
Chain 3:  Elapsed Time: 0.015 seconds (Warm-up)
Chain 3:                0.015 seconds (Sampling)
Chain 3:                0.03 seconds (Total)
Chain 3: 

SAMPLING FOR MODEL 'efb77423d7adb44255913eac3d9377e8' NOW (CHAIN 4).
Chain 4: 
Chain 4: Gradient evaluation took 0 seconds
Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 4: Adjust your expectations accordingly!
Chain 4: 
Chain 4: 
Chain 4: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 4: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 4: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 4: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 4: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 4: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 4: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 4: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 4: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 4: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 4: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 4: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 4: 
Chain 4:  Elapsed Time: 0.013 seconds (Warm-up)
Chain 4:                0.014 seconds (Sampling)
Chain 4:                0.027 seconds (Total)
Chain 4: 
Inference for Stan model: efb77423d7adb44255913eac3d9377e8.
4 chains, each with iter=2000; warmup=1000; thin=1; 
post-warmup draws per chain=1000, total post-warmup draws=4000.

      mean se_mean   sd  2.5%   25%   50%   75% 97.5% n_eff Rhat
y    -0.05    0.03 0.99 -2.00 -0.70 -0.04  0.60  1.92  1364    1
lp__ -0.49    0.02 0.70 -2.56 -0.64 -0.21 -0.05  0.00  1461    1

Samples were drawn using NUTS(diag_e) at Mon Mar 15 11:54:56 2021.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at 
convergence, Rhat=1).
Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found

That warning is safe to ignore, it’s fixed in the next release.

That means that your installation is completely setup and working fine. Is it one of your models that is giving the prep_sampler error or just the example model?

Hi Andrew!
Thank you so much for your help especially considering it was the weekend!

I tried it. then I got another warning message:
Error in cleanup_makevar(old) :
argument “RMU” is missing, with no default

I looked it up and someone suggested this:

file.rename(“~/.R/Makevars.win”, “~/.R/Makevars.win.bak”)

Now its working! YAYY!! Thank you!! :)

Hey @andrjohns just came back to tell you how thankful I am for you taking the time to help me. I really appreciate it. Had some serious deadline to meet at the time. You helped a lot to get things done. Thanks and keep up the good work.

1 Like

No worries at all, glad to hear that it was helpful!