RStan compiling issue

#1

Hello,

I would like to discuss RStan compiling issue. My code was working before last night. However, when I tried to compile with parallel computing by introducing the command – options(mc.cores = parallel::detectCores()), I found the sampling iteration will stay at the 1/5000 (warm-up) stage. I left my computer on to run the RStan code last night and eventually found out R session quit.

When I tried to run the Stan code again, I start to receive the error message:

stanmodel <- stan_model(file='model.stan')

Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! In file included from C:/Users/Qiangsuper/Documents/R/win-library/3.5/BH/include/boost/config.hpp:39:0,
                 from C:/Users/Qiangsuper/Documents/R/win-library/3.5/BH/include/boost/math/tools/config.hpp:13,
                 from C:/Users/Qiangsuper/Documents/R/win-library/3.5/StanHeaders/include/stan/math/rev/core/var.hpp:7,
                 from C:/Users/Qiangsuper/Documents/R/win-library/3.5/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5,
                 from C:/Users/Qiangsuper/Documents/R/win-library/3.5/StanHeaders/include/stan/math/rev/core.hpp:12,
                 from C:/Users/Qiangsuper/Documents/R/win-library/3.5/StanHeaders/include/stan/math/rev/mat.hpp:4,
                 from C:/Users/Qiangsuper/Documents/R/win-library/3.5/StanHeaders/include/stan/math.hpp:4,
                 from C:/Users/Qiangsuper/Documents/R/win-library/3.5/StanHeaders/include/src/stan/model/model_header.hpp:4,
     
In addition: Warning messages:
1: In readLines(file, warn = TRUE) :
  incomplete final line found on 'C:\Users\Qiangsuper\Dropbox\Spatial Tweedie\CAR code\Complex simulation data - Copy\model.stan'
2: In stan_model(file = "model.stan") :
  StanHeaders version is ahead of rstan version; update to latest rstan
3: In system(cmd, intern = !verbose) :
  running command 'C:/PROGRA~1/R/R-35~1.1/bin/x64/R CMD SHLIB file12244e5665c9.cpp 2> file12244e5665c9.cpp.err.txt' had status 1

In order to solve this problem by myself, I reinstalled RStan and RTool. But nothing changes. So I have to come to ask for help.

The session information is as following:

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
[1] rstan_2.17.4       StanHeaders_2.18.0
[3] ggplot2_3.0.0      dplyr_0.7.6       
[5] data.table_1.11.8  tweedie_2.3.2     
[7] Matrix_1.2-14      MASS_7.3-50       

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.19     rstudioapi_0.8  
 [3] bindr_0.1.1      magrittr_1.5    
 [5] munsell_0.5.0    tidyselect_0.2.4
 [7] colorspace_1.3-2 lattice_0.20-35 
 [9] R6_2.3.0         rlang_0.2.2     
[11] plyr_1.8.4       tools_3.5.1     
[13] grid_3.5.1       gtable_0.2.0    
[15] withr_2.1.2      lazyeval_0.2.1  
[17] yaml_2.2.0       assertthat_0.2.0
[19] tibble_1.4.2     crayon_1.3.4    
[21] bindrcpp_0.2.2   gridExtra_2.3   
[23] purrr_0.2.5      inline_0.3.15   
[25] glue_1.3.0       compiler_3.5.1  
[27] pillar_1.3.0     scales_1.0.0    
[29] stats4_3.5.1     pkgconfig_2.0.2 

Thank you very much for your help.

With appreciation,

Tairan

#2

It looks like this error message is slightly misleading:

The catch is that you can’t update to the latest rstan, since the latest version is what you have: 2.17.4. What you may be able to do is downgrade StanHeaders to its previous version, 2.17.2.

Offhand, it looks like the problem is that the maintainers of the StanHeaders package jumped the gun by putting their latest version on CRAN before a compatible version of rstan was available.

#3

Thank you very much, James. I think I updated the packages too frequently. But I am not quite sure how to downgrade the RStan to the previous version? I think what I need to downgrade is RStan rather than StanHeaders, right?

#4

NO. RStan is at version 2.17.4, and StanHeaders is at version 2.18. The versions of both those packages should start with “2.17.”.

ETA: To be more precise, the first two numbers of the RStan and StanHeaders versions should be the same. If RStan is at version 2.17.x, then the StanHeaders version should start with 2.17. When RStan 2.18 becomes available, then the StanHeaders version should start with 2.18.

ETA: For installing older package versions, try the advice here: https://support.rstudio.com/hc/en-us/articles/219949047-Installing-older-versions-of-packages

1 Like
#5

Thank you very much for your explanation, James. I will downgrade my StanHeaders and keep you update.

#6

It is also possible to use StanHeaders 2.18 with rstan 2.17 if you first call

Sys.setenv(USE_CXX14 = 1)

and you have

CXX14 = g++
CXX14FLAGS = -O3

in your ~/.R/Makevars file

1 Like
#7

Thank you for your help, James. I eventually downgraded my StanHeader to 2.17.2 version. And the code is running now. One interesting fact is that even when I introduced the parallel computing by

options(mc.cores = parallel::detectCores())

The sampling procedure seems to take longer time. Before I tried without the parallel computing and used stan_model and sampling function. Now I am using stan function only.

#8

Hi,

I had the same problem as Tairan - I have installed rstan on a new PC yesterday and I was not able to compile code that would otherwise work on my laptop.
Setting Sys.setenv(USE_CXX14 = 1) and CXX14 = g++ CXX14FLAGS = -O3 in the ~/.R/Makevars file didn’t solve the issue for me. What worked was to remove StanHeaders 2.18 and install 2.17.2.

1 Like
#9

I had the same problem and resolved with installation of StanHeaders 2.17.2.

Great thanks.

#10

When you say “parallel computing”, are you talking about using map_rect or just running multiple chains in parallel?

#11

I am using the options(mc.cores = parallel::detectCores()) function to run the multiply chain at the same time.

#12

I think James is right, we should keep the version of StanHeader and RStan the same. My RStan is functioning now. @ anamartinovici

#13

Interesting, but not particularly helpful. Best solution would be to access StanHeader 2.17.2 and then reinstall from that. So, where/how do I get to do that?

#14

As I said before, for installing older package versions, try the advice here: https://support.rstudio.com/hc/en-us/articles/219949047-Installing-older-versions-of-packages

#15

Much simpler solution that worked:
Go to Archives directory for packages in CRAN
Find StanHeaders directory
Download StanHeaders_2.17.2.tar.gz
Install StanHeaders from this downloaded file
(can do this in R or in RStudio)

#16

I am getting a similar error when running this model (which used to work)…
prior.regzpl.a2 <- c(set_prior(“cauchy(4773, 6703)”, class = “Intercept”),
set_prior(“normal(1807, 903)”, class = “b”, coef = “NAO4yrlag”),
set_prior(“normal(47277, 9455)”, class = “b”, coef = “CCBStrat”),
set_prior(“normal(13753, 6976)”, class = “b”, coef = “SST”))

regzpl.a2 <- brm(RegZpl ~ NAO4yrlag + SST + CCBStrat + (~1|Year),
prior = prior.regzpl.a2, warmup = 1000,
family = “gamma”,
inits = 0, cores = 4,
iter = 5000, chains = 2,
control = list(stepsize = 0.01,
max_treedepth = 15, adapt_delta = 0.99),
data = all.data)

Compiling the C++ model
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from file55c3a09f2be.cpp:8:
In file included from /Users/laura.ganley001/Library/R/3.5/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Users/laura.ganley001/Library/R/3.5/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Users/laura.ganley001/Library/R/3.5/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Users/laura.ganley001/Library/R/3.5/library/StanHeaders/include/stan/math/rev/core.hpp:14:
In file included from /Users/laura.ganley001/Library/R/3.5/library/StanHeaders/include/stan/math/rev/core/matrix_vari.hpp:4:
In file included from /Users/laura.ganley001/Library/R/3.5/library/StanHeaders/include/stan/math/rev/mat/fun/Eigen_NumTraits.hpp:4:
In file included from /Users/laura.ganley001/Library/R/3.5/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Users/laura.ganley001/Library/R/3.5/
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file55c3a09f2be.cpp 2> file55c3a09f2be.cpp.err.txt’ had status 1

My rstan is version 2.7.13 and my StanHeaders is version 2.17.2

I tried putting CXX14 = g++ CXX14FLAGS = -O3 in the Makevars file.
So now it looks like CXX14 = g++ CXX14FLAGS = -O3

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

Any ideas?

#17

Those 2.17.x versions were not exactly intended to work. Upgrade to 2.18.x.

#18

Wonderful, thanks! Updating worked!