RStan crashing R session with RStan 2.16.1 on Windows 7

Session Info:

 Session info ------------------------------------------
 setting  value                       
 version  R version 3.4.1 (2017-06-30)
 system   x86_64, mingw32             
 ui       RStudio (1.0.143)           
 language (EN)                        
 collate  English_United States.1252  
 tz       America/Los_Angeles         
 date     2017-08-10                  

Packages ---------------------------------------------------------
 package      * version   date       source        
 BH             1.62.0-1  2016-11-19 CRAN (R 3.4.0)
 colorspace     1.3-2     2016-12-14 CRAN (R 3.4.1)
 dichromat      2.0-0     2013-01-24 CRAN (R 3.4.0)
 digest         0.6.12    2017-01-27 CRAN (R 3.4.1)
 ggplot2      * 2.2.1     2016-12-30 CRAN (R 3.4.1)
 graphics     * 3.4.1     2017-06-30 local         
 grDevices    * 3.4.1     2017-06-30 local         
 grid           3.4.1     2017-06-30 local         
 gridExtra      2.2.1     2016-02-29 CRAN (R 3.4.1)
 gtable         0.2.0     2016-02-26 CRAN (R 3.4.1)
 inline         0.3.14    2015-04-13 CRAN (R 3.4.1)
 labeling       0.3       2014-08-23 CRAN (R 3.4.0)
 lattice        0.20-35   2017-03-25 CRAN (R 3.4.1)
 lazyeval       0.2.0     2016-06-12 CRAN (R 3.4.1)
 magrittr     * 1.5       2014-11-22 CRAN (R 3.4.1)
 MASS           7.3-47    2017-02-26 CRAN (R 3.4.1)
 Matrix         1.2-10    2017-05-03 CRAN (R 3.4.1)
 methods      * 3.4.1     2017-06-30 local         
 munsell        0.4.3     2016-02-13 CRAN (R 3.4.1)
 plyr           1.8.4     2016-06-08 CRAN (R 3.4.1)
 RColorBrewer   1.1-2     2014-12-07 CRAN (R 3.4.0)
 Rcpp           0.12.12   2017-07-15 CRAN (R 3.4.1)
 RcppEigen      0.3.3.3.0 2017-05-01 CRAN (R 3.4.1)
 reshape2       1.4.2     2016-10-22 CRAN (R 3.4.1)
 rlang          0.1.2     2017-08-09 CRAN (R 3.4.1)
 rstan        * 2.16.2    2017-07-03 CRAN (R 3.4.1)
 scales         0.4.1     2016-11-09 CRAN (R 3.4.1)
 StanHeaders  * 2.16.0-1  2017-07-03 CRAN (R 3.4.1)
 stats        * 3.4.1     2017-06-30 local         
 stats4         3.4.1     2017-06-30 local         
 stringi        1.1.5     2017-04-07 CRAN (R 3.4.0)
 stringr        1.2.0     2017-02-18 CRAN (R 3.4.1)
 tibble       * 1.3.3     2017-05-28 CRAN (R 3.4.1)
 tools          3.4.1     2017-06-30 local         
 utils        * 3.4.1     2017-06-30 local         

Output of writeLines(readLines(file.path(Sys.getenv(“HOME”), “.R/Makevars”))):

CXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function

CXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function

CXXFLAGS += -Wno-ignored-attributes -Wno-deprecated-declarations

I’m troubleshooting a rather complicated model, but I keep running into an issue where Rstan will crash my Rsession. I fairly certain it is not a feature of my model, but rather something about the most recent version of Rstan and my machine. I am able to get the model to successfully run using simulated data. But when I try to apply the same model code to real data, I get a Windows error message saying “R for Windows front-end has stopped working.” If I run using 4 chains, sometimes I will get 4 of these message (one for each R session). Sometimes I get fewer. I’ve been able to figure out using RStudio that I will only get a crash and error message if the model is able to initialize and successfully move on sampling. For example, I just attempted again and only got one error message and crash for four chains. Looking at the Rstudio Viewer, I can see that the three chains that did not throw an error failed to initialize because it couldn’t find initial values that didn’t result in a log probability that evaluates to log(0) and quit after 100 attempts. The one chain that crashed R was the only one that was able to initialize and begin sampling.

I was finally able to run the code on an old version of Rstan (2.15.1). What I discovered is that on chains that successfully sample I would get a small number of warnings about a scale parameter being less than 0.

So here are my three lines of evidence:

  1. Stan successfully runs on simulated data meant to match the model structure. I get no warnings about illegal values of scale parameters or messages about rejecting initial values.
  2. On my real data, I either get chains that either fail to initialize or crash using Rstan 2.16
  3. Using that same data on RStan 2.15.1, I will get warnings about illegal values of a parameter.

Obviously I need to improve my model, but I can’t do that when RStan keeps crashing on me. I can try to go back to an old version of RStan, but there may be other users experiencing this issue and ideally I’d be able to sort this out and stay current. I suspect that what is happening is that Stan is encountering an illegal value for a parameter, but rather than simply rejecting the proposal and giving me a warning it is crashing. This happens rather quickly, so I suspect I have a model that reliably finds these illegal values in the warmup phase.

Rather than getting bogged down in the details of my model, if someone can suggest a simple model that is known to occasionally throw warnings about illegal parameter values, I will use that to see if I can replicate the problem more reliably.

Similar happened to me, at some point stan / rstan has changed and now seems to dislike those lines in the makevars.

Remove that from CXXFLAGS in ~./R/Makevars

@bgoodri

Remove that from CXXFLAGS in ~./R/Makevars

No dice. Still crashing. Here’s my Makevars file now:

> writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars")))

CXXFLAGS=-O3 -Wno-unused-variable -Wno-unused-function

CXXFLAGS=-O3 -Wno-unused-variable -Wno-unused-function

CXXFLAGS += -Wno-ignored-attributes -Wno-deprecated-declarations

Try reinstalling Rcpp from source with that ~/.R/Makevars.

That might have done it. At least the same code and data is not crashing immediately. I will do some more testing, but I think this can be marked as resolved.