Search code examples
c++rdata-modelingstanrstan

Compilation ERROR, function(s)/method(s) not created! for brms model


I am having this error again. But unlike before, now the test below works:

fx <- inline::cxxfunction( signature(x = "integer", y = "numeric" ) , '
return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
' )
fx( 2L, 5 ) # returns 10 as should be

But when I try any model based on stan (eg. sample code and data below) I get the same error as below.

Here are sample data and code:

DT<-structure(list(c = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("a", "b"), class = "factor"), 
x = c(2004L, 2005L, 2006L, 2007L, 2008L, 2010L, 2012L, 2010L, 
2012L, 2010L, 2005L, 2007L, 2012L, 2005L), y = c(11.05826397, 
15.56776557, 49.50980392, 64.53900709, 75, 68.18181818, 99.86357435, 
100, 99.26739927, 98.52941176, 100, 95.94594595, 99.64157706, 
100)), class = "data.frame", row.names = c(NA, -14L))
library(brms)
fit <- brm(y~ x, data = DT)

It says "function(s)/method(s) not created!":

`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 C:/Users/.../Documents/R/win-library/3.5/BH/include/boost/config.hpp:39:0,
                 from C:/Users/.../Documents/R/win-library/3.5/BH/include/boost/math/tools/config.hpp:13,
                 from C:/Users/.../Documents/R/win-library/3.5/StanHeaders/include/stan/math/rev/core/var.hpp:7,
                 from C:/Users/.../Documents/R/win-library/3.5/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5,
                 from C:/Users/.../Documents/R/win-library/3.5/StanHeaders/include/stan/math/rev/core.hpp:12,
                 from C:/Users/.../Documents/R/win-library/3.5/StanHeaders/include/stan/math/rev/mat.hpp:4,
                 from C:/Users/.../Documents/R/win-library/3.5/StanHeaders/include/stan/math.hpp:4,
                 from C:/Users/.../Documents/R/win-library/3.5/StanHeaders/include/src/stan
In addition: Warning messages:
1: In rstan::stan_model(model_code = x$model, save_dso = save_dso) :
  StanHeaders version is ahead of rstan version; update to latest rstan
2: In system(cmd, intern = !verbose) :
  running command 'C:/PROGRA~1/R/R-3.5.1/bin/x64/R CMD SHLIB file110c1cb656fa.cpp 2> file110c1cb656fa.cpp.err.txt' had status 1 `

Here is my session info:

sessionInfo()
R version 3.5.0 (2018-04-23)
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     datasets 
[6] methods   base     

other attached packages:
[1] rstudioapi_0.8.0.9000 brms_2.5.0           
[3] ggplot2_3.0.0         Rcpp_0.12.19         

loaded via a namespace (and not attached):
 [1] mvtnorm_1.0-8        lattice_0.20-35     
 [3] gtools_3.8.1         zoo_1.8-4           
 [5] assertthat_0.2.0     digest_0.6.18       
 [7] mime_0.6             R6_2.3.0            
 [9] plyr_1.8.4           backports_1.1.2     
[11] ggridges_0.5.1       stats4_3.5.0        
[13] coda_0.19-2          colourpicker_1.0    
[15] pillar_1.3.0         rlang_0.2.2         
[17] lazyeval_0.2.1       miniUI_0.1.1.1      
[19] Matrix_1.2-14        DT_0.4              
[21] shinythemes_1.1.1    shinyjs_1.0         
[23] stringr_1.3.1        htmlwidgets_1.3     
[25] loo_2.0.0            igraph_1.2.2        
[27] munsell_0.5.0        shiny_1.1.0         
[29] rstan_2.17.4         compiler_3.5.0      
[31] httpuv_1.4.5         pkgconfig_2.0.2     
[33] base64enc_0.1-3      rstantools_1.5.1    
[35] htmltools_0.3.6      tidyselect_0.2.5    
[37] tibble_1.4.2         gridExtra_2.3       
[39] threejs_0.3.1        matrixStats_0.54.0  
[41] crayon_1.3.4         dplyr_0.7.6         
[43] withr_2.1.2          later_0.7.5         
[45] grid_3.5.0           nlme_3.1-137        
[47] xtable_1.8-3         gtable_0.2.0        
[49] magrittr_1.5         StanHeaders_2.18.0  
[51] scales_1.0.0         stringi_1.1.7       
[53] reshape2_1.4.3       promises_1.0.1      
[55] bindrcpp_0.2.2       dygraphs_1.1.1.6    
[57] xts_0.11-1           tools_3.5.0         
[59] glue_1.3.0           markdown_0.8        
[61] shinystan_2.5.0      purrr_0.2.5         
[63] crosstalk_1.0.0      rsconnect_0.8.8     
[65] abind_1.4-5          parallel_3.5.0      
[67] inline_0.3.15        colorspace_1.3-2    
[69] bridgesampling_0.5-2 bayesplot_1.6.0     
[71] bindr_0.1.1          Brobdingnag_1.2-6 

I would appreciate any ideas to address this.


Solution

  • brms provides routines to fit Bayesian models using Stan; Stan in turn is a C++ library that provides a probabilistic language for defining Bayesian models. These models are then parsed and expressed in the Stan language to C++ code, which is then compiled to an executable program.

    So in a nutshell, one of the key requirements to use brms is a C++ compiler, that compiles your models into an executable program. Your error indicates that the latter is missing.

    From the brms GitHub FAQ:

    Because brms is based on Stan, a C++ compiler is required. The program Rtools (available on https://cran.r-project.org/bin/windows/Rtools/) comes with a C++ compiler for Windows. On Mac, you should install Xcode. For further instructions on how to get the compilers running, see the prerequisites section on https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started.

    You can install Rtools directly via this link.