Error running stan with linux

Hey,

Rstan is again not working on the Linux server I am using :(

Here the output, any suggestions? Thanks!

> library(rstan)
Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.21.2, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
> # options(mc.cores = parallel::detectCores())
> # rstan_options(auto_write = TRUE)
> 
> utils::sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS:   /software/R/lib64/R/lib/libRblas.so
LAPACK: /software/R/lib64/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] rstan_2.21.2         ggplot2_3.3.2        StanHeaders_2.21.0-7

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5         pillar_1.4.7       compiler_4.0.3     prettyunits_1.1.1 
 [5] tools_4.0.3        pkgbuild_1.1.0     jsonlite_1.7.1     lifecycle_0.2.0   
 [9] tibble_3.0.4       gtable_0.3.0       pkgconfig_2.0.3    rlang_0.4.9       
[13] cli_2.2.0          parallel_4.0.3     curl_4.3           loo_2.3.1         
[17] gridExtra_2.3      withr_2.3.0        dplyr_1.0.2        generics_0.1.0    
[21] vctrs_0.3.5        stats4_4.0.3       grid_4.0.3         tidyselect_1.1.0  
[25] glue_1.4.2         inline_0.3.16      R6_2.5.0           processx_3.4.4    
[29] fansi_0.4.1        callr_3.5.1        purrr_0.3.4        magrittr_2.0.1    
[33] codetools_0.2-16   scales_1.1.1       ps_1.4.0           ellipsis_0.3.1    
[37] matrixStats_0.57.0 assertthat_0.2.1   colorspace_2.0-0   V8_3.4.0          
[41] RcppParallel_5.0.2 munsell_0.5.0      crayon_1.3.4      
> 
> system("g++ --version")
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

> 
> Sys.which("g++")
       g++ 
"/bin/g++" 
> 
> readLines(file.path(getwd(), ".R", "Makevars"))
[1] "# created by rstan"                              
[2] "CXX14FLAGS=-O3 -march=native -mtune=native -fPIC"
[3] "CXX14=/opt/rh/devtoolset-8/root/usr/bin/g++"     
[4] ""                                                
> 
> modelscript <- "
+ // saved as schools.stan
+ data {
+   int<lower=0> J;         // number of schools
+   real y[J];              // estimated treatment effects
+   real<lower=0> sigma[J]; // standard error of effect estimates
+ }
+ parameters {
+   real mu;                // population treatment effect
+   real<lower=0> tau;      // standard deviation in treatment effects
+   vector[J] eta;          // unscaled deviation from mu by school
+ }
+ transformed parameters {
+   vector[J] theta = mu + tau * eta;        // school treatment effects
+ }
+ model {
+   target += normal_lpdf(eta | 0, 1);       // prior log-density
+   target += normal_lpdf(y | theta, sigma); // log-likelihood
+ }
+ 
+ "
> 
> schools_dat <- list(J = 8,
+   y = c(28,  8, -3,  7, -1,  1, 18, 12),
+   sigma = c(15, 10, 16, 11,  9, 11, 10, 18))
> 
> fit <- stan(model_code = modelscript, data = schools_dat)

 *** caught segfault ***
address (nil), cause 'unknown'

Traceback:
 1: dyn.load(libLFile)
 2: cxxfunction(sig = sig, body = body, plugin = plugin, includes = includes,     settings = settings, ..., verbose = verbose)
 3: pkgbuild::with_build_tools(cxxfunction(sig = sig, body = body,     plugin = plugin, includes = includes, settings = settings,     ..., verbose = verbose), required = rstan_options("required") &&     !identical(Sys.getenv("WINDOWS"), "TRUE") && !identical(Sys.getenv("R_PACKAGE_SOURCE"),     ""))
 4: cxxfunctionplus(signature(), body = paste(" return Rcpp::wrap(\"",     model_name, "\");", sep = ""), includes = inc, plugin = "rstan",     save_dso = save_dso | auto_write, module_name = paste("stan_fit4",         model_cppname, "_mod", sep = ""), verbose = verbose)
 5: stan_model(file, model_name = model_name, model_code = model_code,     stanc_ret = NULL, boost_lib = boost_lib, eigen_lib = eigen_lib,     save_dso = save_dso, verbose = verbose)
 6: stan(model_code = modelscript, data = schools_dat)
An irrecoverable exception occurred. R is aborting now ...

Hi,
some of those issues can be caused by updating parts of the system rstan is compiled against. Could you try reinstalling the packages from source?

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

Additionally, you may try using latest (currently not on CRAN for stupid reasons) rstan via Repository for distributing (some) stan-dev R packages | r-packages or use cmdstanr which is less tightly coupled to your R session and will not crash your R even if bad stuff happens.

Hope you can get this resolved quickly.

using cmdstan solved the issue, thanks!

2 Likes