stan-dev / rstan

RStan, the R interface to Stan
https://mc-stan.org
1.04k stars 267 forks source link

Compile error when calling the function stan_model in an R Jupyter notebook #425

Open paupereira opened 7 years ago

paupereira commented 7 years ago

Summary:

I get a compile error when calling the function stan_model in an R Jupyter notebook. When running the same code from the terminal no error occurs.

Description:

I'm trying to run the following toy model in rstan:

# In R:
# Load necessary libraries and set up multi-core processing for Stan
options(warn=-1, message =-1)
library(dplyr); library(ggplot2); library(rstan); library(reshape2)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())

# In R, or you could save the contents of the string in a file with .stan file type

dgp_string <- "

functions {
    /**
   * Return draws from a linear regression with data matrix X,
   * coefficients beta, and student-t noise with degrees of freedom nu
   * and scale sigma.
   *
   * @param X Data matrix (N x P)
   * @param beta Coefficient vector (P x 1)
   * @param nu Residual distribution degrees of freedom.
   * @param sigma Residual distribution scale.
   * @return Return an N-vector of draws from the model.
   */

    vector dgp_rng(matrix X, vector beta, real nu, real sigma) {
      vector[rows(X)] y; // define the output vector to be as long as the number of rows in X

      // Now fill it in
      for (n in 1:rows(X))
        y[n] <- student_t_rng(nu, X[n] * beta, sigma);
      return y;
   }
}
data {
 // If we were estimating a model, we'd define the data inputs here
}
parameters {
  // ... and the parameters we want to estimate would go in here
}
model {
  // This is where the probability model we want to estimate would go
}
"

# Generate a matrix of random numbers, and values for beta, nu and sigma

set.seed(42) # Set the random number generator seed so that we get the same parameters
N <- 1000 # Number of observations
P <- 10 # Number of covariates
X <- matrix(rnorm(N*P), N, P) # generate an N*P covariate matrix of random data
nu <- 5 # Set degrees of freedom
sigma <- 5 # And scale parameter
beta <- rnorm(10) # Generate some random coefficients that we'll try to recover
# Make sure the first element of beta is positive as in our chosen DGP
beta[1] <- abs(beta[1])

# Compile the script
compiled_function <- stan_model(model_code = dgp_string) # you could use file = "path/to/yourfile.stan" if you have saved it as so

Current Output:

If applicable, any relevant output from RStan.

I get the following error message:

(...)
Error in compileCode(f, code, language = language, verbose = verbose): Compilation ERROR, function(s)/method(s) not created! **********************************************************************
* Please run brazil-build rather than make.  Your build might not work
* correctly without running brazil-build.
**********************************************************************
cc1plus: error: unrecognized command line option "-mbmi-mavx"
make: *** [file3bf5713249a.o] Error 1
Traceback:

1. stan_model(model_code = dgp_string)
2. 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)
3. cxxfunction(sig = sig, body = body, plugin = plugin, includes = includes, 
 .     settings = settings, ..., verbose = verbose)
4. compileCode(f, code, language = language, verbose = verbose)
5. stop(paste("Compilation ERROR, function(s)/method(s) not created!", 
 .     paste(errmsg, collapse = "\n")))

RStan and R version:

sessionInfo()

R version 3.3.2 (2016-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Amazon Linux Bare Metal release 2012.03

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] reshape2_1.4.2       rstan_2.12.1         StanHeaders_2.12.0-1
[4] ggplot2_2.2.0        dplyr_0.5.0         

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.8      magrittr_1.5     munsell_0.4.3    uuid_0.1-2      
 [5] colorspace_1.3-1 R6_2.2.0         stringr_1.1.0    plyr_1.8.4      
 [9] tools_3.3.2      parallel_3.3.2   grid_3.3.2       gtable_0.2.0    
[13] DBI_0.5-1        lazyeval_0.2.0   assertthat_0.1   digest_0.6.10   
[17] tibble_1.2       crayon_1.3.2     gridExtra_2.2.1  IRdisplay_0.4.4 
[21] repr_0.10        IRkernel_0.7.1   inline_0.3.14    evaluate_0.10   
[25] pbdZMQ_0.2-4     stringi_1.1.2    scales_0.4.1     stats4_3.3.2    
[29] jsonlite_1.1    

Operating System:

cat /etc/redhat-release

Red Hat Enterprise Linux Server release 5.3 (Tikanga)
bob-carpenter commented 7 years ago

Looks like you don't have rstan loaded from the sessionInfo().

paupereira commented 7 years ago

I did run library(rstan) if that's what you mean.

Doesn't the fact that rstan_2.12.1 appears in the output of sessionInfo() under "other attached packages" mean that rstan is loaded?

bob-carpenter commented 7 years ago

@ppau1 Never mind, I'm blind. I didn't see the library(rstan) at the end of the library list and somehow missed it in the list of packages.

bgoodri commented 7 years ago

cc1plus: error: unrecognized command line option "-mbmi-mavx"

Your Jupityer seems to be adding an unrecognized compiler flag.

Ben

On Jun 27, 2017 4:48 AM, "Pau" notifications@github.com wrote:

Summary:

I get a compile error when calling the function stan_model in an R Jupyter notebook. When running the same code from the terminal no error occurs. Description:

I'm trying to run the following toy model in rstan:

In R:# Load necessary libraries and set up multi-core processing for Stan

options(warn=-1, message =-1) library(dplyr); library(ggplot2); library(rstan); library(reshape2) rstan_options(auto_write = TRUE) options(mc.cores = parallel::detectCores())

In R, or you could save the contents of the string in a file with

.stan file type dgp_string <- "functions { /* Return draws from a linear regression with data matrix X, coefficients beta, and student-t noise with degrees of freedom nu and scale sigma. @param X Data matrix (N x P) @param beta Coefficient vector (P x 1) @param nu Residual distribution degrees of freedom. @param sigma Residual distribution scale. @return Return an N-vector of draws from the model. / vector dgp_rng(matrix X, vector beta, real nu, real sigma) { vector[rows(X)] y; // define the output vector to be as long as the number of rows in X // Now fill it in for (n in 1:rows(X)) y[n] <- student_t_rng(nu, X[n] beta, sigma); return y; }}data { // If we were estimating a model, we'd define the data inputs here}parameters { // ... and the parameters we want to estimate would go in here}model { // This is where the probability model we want to estimate would go}"

Generate a matrix of random numbers, and values for beta, nu and sigma

set.seed(42) # Set the random number generator seed so that we get the same parametersN <- 1000 # Number of observationsP <- 10 # Number of covariatesX <- matrix(rnorm(NP), N, P) # generate an NP covariate matrix of random datanu <- 5 # Set degrees of freedomsigma <- 5 # And scale parameterbeta <- rnorm(10) # Generate some random coefficients that we'll try to recover# Make sure the first element of beta is positive as in our chosen DGPbeta[1] <- abs(beta[1])

Compile the scriptcompiled_function <- stan_model(model_code =

dgp_string) # you could use file = "path/to/yourfile.stan" if you have saved it as so

Current Output:

If applicable, any relevant output from RStan.

I get the following error message:

(...) Error in compileCode(f, code, language = language, verbose = verbose): Compilation ERROR, function(s)/method(s) not created!



make: *** [file3bf5713249a.o] Error 1 Traceback:

  1. stan_model(model_code = dgp_string)
  2. 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)
  3. cxxfunction(sig = sig, body = body, plugin = plugin, includes = includes, . settings = settings, ..., verbose = verbose)
  4. compileCode(f, code, language = language, verbose = verbose)
  5. stop(paste("Compilation ERROR, function(s)/method(s) not created!", . paste(errmsg, collapse = "\n")))

RStan and R version:

sessionInfo() R version 3.3.2 (2016-10-31)Platform: x86_64-pc-linux-gnu (64-bit)Running under: Amazon Linux Bare Metal release 2012.03 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] reshape2_1.4.2 rstan_2.12.1 StanHeaders_2.12.0-1 [4] ggplot2_2.2.0 dplyr_0.5.0 loaded via a namespace (and not attached): [1] Rcpp_0.12.8 magrittr_1.5 munsell_0.4.3 uuid_0.1-2 [5] colorspace_1.3-1 R6_2.2.0 stringr_1.1.0 plyr_1.8.4 [9] tools_3.3.2 parallel_3.3.2 grid_3.3.2 gtable_0.2.0 [13] DBI_0.5-1 lazyeval_0.2.0 assertthat_0.1 digest_0.6.10 [17] tibble_1.2 crayon_1.3.2 gridExtra_2.2.1 IRdisplay_0.4.4 [21] repr_0.10 IRkernel_0.7.1 inline_0.3.14 evaluate_0.10 [25] pbdZMQ_0.2-4 stringi_1.1.2 scales_0.4.1 stats4_3.3.2 [29] jsonlite_1.1

Operating System:

cat /etc/redhat-release

Red Hat Enterprise Linux Server release 5.3 (Tikanga)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/stan-dev/rstan/issues/425, or mute the thread https://github.com/notifications/unsubscribe-auth/ADOrqo2COHVraonUT9sjkvNYnUvnQ9B6ks5sIG2JgaJpZM4OGFc7 .