Hey everyone,
I am trying to build a Test package which uses RStan. The package only consits of one function. But somehow the package does not build do to the following Error:
installing to library ‘C:/Program Files/R/R-3.6.1/library’
installing source package ‘rstanlm’ …
** using staged installation
** libs
Error in .shlib_internal(args) :
C++14 standard requested but CXX14 is not defined
removing ‘C:/Program Files/R/R-3.6.1/library/rstanlm’
restoring previous ‘C:/Program Files/R/R-3.6.1/library/rstanlm’
Exited with status 1.
The R file consits of the following:
lm_stan ← function(x, y, …) {
standata ← list(x = x, y = y, N = length(y))
out ← rstan::sampling(stanmodels$lm, data = standata, …)
return(out)
}
The Stan file:
// Save this file as inst/stan/lm.stan
data {
int<lower=1> N;
vector[N] x;
vector[N] y;
}
parameters {
real intercept;
real beta;
real<lower=0> sigma;
}
model {
// … priors, etc.
y ~ normal(intercept + beta * x, sigma);
}
In the following thread this issue was discussed but no option did work out. I thought maybe if I have a updated win-build Rstan URL it might work but I dont know where to find one. And I am also not sure if this will solve the issue.
opened 04:45PM - 15 Oct 18 UTC
#### Summary:
Error message relating to C++compiler when running a linear model… fit.
#### Description:
I am new to Stan and RStan and trying out the tutorial by Max Farrell and Isla Myers-Smith on:
<https://ourcodingclub.github.io/2018/04/17/stan-intro.html>
The step by step process is shown below under 'Reproducible Steps'.
When I run the stanc() it compiles fine. But when I fit the linear model I receive the following error message:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! Error in .shlib_internal(commandArgs(TRUE)) :
C++14 standard requested but CXX14 is not defined
Calls: <Anonymous> -> .shlib_internal
Execution halted
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command 'C:/PROGRA~1/R/R-35~1.1/bin/x64/R CMD SHLIB file55c71d73e8e.cpp 2> file55c71d73e8e.cpp.err.txt' had status 1
I've searched everywhere for possible solutions. I reviewed the following issue posted by yanys7
<https://github.com/stan-dev/rstan/issues/565>
As suggested by bgoodri I created a ~/.R/Makevars file and inserted the following:
CXX14 = g++
However the problem still persists.
I don't know if this is a bug or if I'm doing something wrong.
Thanks in advance for any help you can provide.
#### Reproducible Steps:
This follows almost exactly the steps shown in the tutorial.
```
Sys.setenv(USE_CXX14 = 1)
library(rstan)
library(gdata)
library(bayesplot)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
seaice <- read.csv("seaice.csv", stringsAsFactors = F)
colnames(seaice)<-c("year", "extent_north", "extent_south")
x <- I(seaice$year - 1978)
y <- seaice$extent_north
N <- length(seaice$year)
stan_data <- list(N = N, x = x, y = y)
write("// Stan model for simple linear regression
data {
int <lower = 1> N;
vector[N] x;
vector[N] y;
}
parameters {
real alpha;
real beta;
real<lower = 0> sigma;
}
model {
y ~ normal(alpha + x * beta, sigma);
}
generated quantities {
},
"stan_model1.stan")
stanc("stan_model1.stan")
stan_model1 <- "stan_model1.stan"
fit <- stan(file = stan_model1, data = stan_data, warmup = 500, iter = 1000, chains = 4, thin = 1)
```
#### Current Output:
The output from ```stanc("stan_model1.stan")``` is below:
$`status`
[1] TRUE
$model_cppname
[1] "model55c23451a60_stan_model1"
$cppcode
[1] "// Code generated by Stan version 2.17.0\n\n#include <stan/model/model_header.hpp>\n\nnamespace model55c23451a60_stan_model1_namespace {\n\nusing std::istream;\nusing std::string;\nusing std::stringstream;\nusing std::vector;\nusing stan::io::dump;\nusing stan::math::lgamma;\nusing stan::model::prob_grad;\nusing namespace stan::math;\n\ntypedef Eigen::Matrix<double,Eigen::Dynamic,1> vector_d;\ntypedef Eigen::Matrix<double,1,Eigen::Dynamic> row_vector_d;\ntypedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> matrix_d;\n\nstatic int current_statement_begin__;\n\nstan::io::program_reader prog_reader__() {\n stan::io::program_reader reader;\n reader.add_event(0, 0, \"start\", \"model55c23451a60_stan_model1\");\n reader.add_event(19, 19, \"end\", \"model55c23451a60_stan_model1\");\n return reader;\n}\n\nclass model55c23451a60_stan_model1 : public prob_grad {\nprivate:\n int N;\n vector_d x;\n vector_d y;\npublic:\n model55c23451a60_stan_model1(stan::io::va... <truncated>
$model_name
[1] "stan_model1"
$model_code
[1] "// Stan model for simple linear regression\ndata {\n int <lower = 1> N; // Sample size\n vector[N] x; //Predictor\n vector[N] y; // Outcome\n}\n\nparameters {\n real alpha; //Intercept\n real beta; // Slope\n real<lower = 0> sigma; //Error SD\n}\n\nmodel {\n y ~ normal(alpha + x * beta, sigma);\n}\n\ngenerated quantities {\n} // The posterior predictive distribution"
attr(,"model_name2")
[1] "stan_model1"
#### Expected Output:
If applicable, the output you expected from RStan.
#### RStan Version:
RStan version 2.17.3
#### R Version:
R Version 3.5.1
#### Operating System:
Windows 8 64bit
When building R packages that use rstan, make sure you use the rstantools
package: GitHub - stan-dev/rstantools: Tools for Developing R Packages Interfacing with Stan