Clang.hpp WARNING (RStan)

I was going to do a simple poisson GLM for my class, but got a warning (and a c++ error…later perhaps). So I tried the simplest poisson model to investigate. No c++ error, but I get a warning. Can you help me to get rid of this?

Thanks,

Jay

First, some info:

I’m using RStan 2.16.2 (stan 2.16.0) on an iMac running Sierra (10.12.6).

My Makevars file contains the following:

CC=clang
CXX=clang++ -arch x86_64 -ftemplate-depth-256
CXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function

I also have cmdstan-2.12.0 and cmdstan-2.14.0 installed (but am only using RStan 2.16.2 (stan 2.16.0))

My RStan code/output:

> set.seed(26401)
> N<- 20
> mu<- 10
> y<- rpois(n=N, lambda=mu)
> my.data <- list(
+   N = N,
+   y = y)
> my.init<- list(list(mu=10))
> model.stan<- "
+ data {
+   int<lower=0> N;
+   int<lower=0> y[N];
+ }
+ parameters {
+   real<lower=0> mu; 
+ }
+ model {
+ y ~ poisson(mu);
+ }
+ "
> library(rstan)
Loading required package: ggplot2
Loading required package: StanHeaders
rstan (Version 2.16.2, packaged: 2017-07-03 09:24:58 UTC, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
> rstan_options(auto_write = TRUE)
> options(mc.cores = parallel::detectCores())
> fit1 <- stan(
+     model_code = model.stan,
+     data = my.data,         
+     init = my.init,
+     chains = 1,             
    warmup = 1000,          
+ +     iter = 2000,            
+     refresh = 1000
+ )
In file included from file4cbe82b6700.cpp:8:
In file included from /Users/jay/Library/R/librarymac/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Users/jay/Library/R/librarymac/StanHeaders/include/stan/math.hpp:4:
In file included from /Users/jay/Library/R/librarymac/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Users/jay/Library/R/librarymac/StanHeaders/include/stan/math/rev/core.hpp:12:
In file included from /Users/jay/Library/R/librarymac/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5:
In file included from /Users/jay/Library/R/librarymac/StanHeaders/include/stan/math/rev/core/var.hpp:7:
In file included from /Users/jay/Library/R/librarymac/BH/include/boost/math/tools/config.hpp:13:
In file included from /Users/jay/Library/R/librarymac/BH/include/boost/config.hpp:39:
/Users/jay/Library/R/librarymac/BH/include/boost/config/compiler/clang.hpp:196:11: warning: 'BOOST_NO_CXX11_RVALUE_REFERENCES' macro redefined [-Wmacro-redefined]
#  define BOOST_NO_CXX11_RVALUE_REFERENCES
          ^
<command line>:6:9: note: previous definition is here
#define BOOST_NO_CXX11_RVALUE_REFERENCES 1
        ^
1 warning generated.

SAMPLING FOR MODEL 'c280e2c55fcf0a882eab452b53738873' NOW (CHAIN 1).

Gradient evaluation took 8e-06 seconds
1000 transitions using 10 leapfrog steps per transition would take 0.08 seconds.
Adjust your expectations accordingly!


Iteration:    1 / 2000 [  0%]  (Warmup)
Iteration: 1000 / 2000 [ 50%]  (Warmup)
Iteration: 1001 / 2000 [ 50%]  (Sampling)
Iteration: 2000 / 2000 [100%]  (Sampling)

 Elapsed Time: 0.007756 seconds (Warm-up)
               0.007064 seconds (Sampling)
               0.01482 seconds (Total)

> stan_version()
[1] "2.16.0"
>

[edit: escaped code blocks]

Don’t worry about that compiler warning. What was the error?

To be sure, I don’t get the c++ error with this simple code. But, below is a very simple Poisson GLM that does give the c++ error. (I checked the data (lung cancer and radon data) …okay…runs in R’s glm fine…)

> poisGLM.data1.list <- list(
+     N = length(y),
+     y=y,
+     offsetlnE=log(E),
+     x=x
+ )
> poisGLM.init1.list <- list(
+ list(
+     b0 = 0.171, ## MLEs
+     b1 = -0.036),
+ list(
+     b0 = 0.171, ## MLEs
+     b1 = -0.036),
+ list(
    b0 = 0.171, ## MLEs
    b1 = -0.036),
+ + + list(
+     b0 = 0.171, ## MLEs
+     b1 = -0.036)
+ )
> poisGLM.model1.stan <-
+     "
+ data
+ {
int<lower=1> N;
+ + int<lower=0> y[N];
+ real<lower=0> offsetlnE[N];
+ real<lower=0> x[N];
+ }
+ parameters
+ {
+ real b0; real b1;
+ }
+ model
+ {
+ real<lower=0> lnmu [N];
lnmu = offsetlnE + b0 + b1 * x;
y ~ poisson_log(lnmu);
+ + + }
+ "
> library(rstan)
Loading required package: ggplot2
Loading required package: StanHeaders
rstan (Version 2.16.2, packaged: 2017-07-03 09:24:58 UTC, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
> rstan_options(auto_write = TRUE)
> options(mc.cores = parallel::detectCores())
> fit1 <- stan(
+     model_code = poisGLM.model1.stan,
+     data = poisGLM.data1.list,
+     init = poisGLM.init1.list,
+     chains = 4,
+     warmup = 1000,
+     iter = 2000,
+     seed = 24601,
+     refresh = 1000
+   )
Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  c++ exception (unknown reason)

[edit: escaped code blocks]

1 Like

…i omitted the lower bound on the linear predictor variable (I was trying y ~ poisson and got the same c++ exception and thought y~ poisson_log might help but forgot to change lower bound…still get the same c++ exception

real<lower=0> lnmu[N]
real lnmu[N}

[edit: escape code blocks]

This is a bug in the Mac binary for rstan on CRAN. It doesn’t give you the full output from Stan’s parser. I believe this is fixed for the next release (coming soon). It’s definitely a problem when trying to debug models! For the time being you can get the full parser output on Mac by installing rstan from source instead of binary (you can add type="source" when calling install.packages).

In this particular case the full error message you received should have been

SYNTAX ERROR, MESSAGE(S) FROM PARSER:

require unconstrained. found range constraint.
  error in 'model_test' at line 12, column 25
  -------------------------------------------------
    10: }
    11: model {
    12:   real<lower=0> lnmu [N];
                                ^
    13:   lnmu = offsetlnE + b0 + b1 * x;
  -------------------------------------------------

which is telling you that it wants you to remove the lower=0 constraint in the model block. You can put constraints on parameters but not on intermediate quantities computed from those parameters in the model block. But in this case I don’t think lnmu even needs to be positive since you’re using poisson_log (instead of just poisson). poisson_log accepts in input on the log scale and exponentiates for you so you don’t need the argument to poisson_log to be positive.

I would try changing your data block to

data {
  int<lower=1> N;
  int<lower=0> y[N];
  vector<lower=0>[N] offsetlnE;  // changed from real array to vector
  vector<lower=0>[N] x;  // changed from real array to vector
}

and then your model block to

model {
  vector[N] lnmu = offsetlnE + b0 + b1 * x;
  y ~ poisson_log(lnmu);
}

Using vectors instead of real arrays for offsetlnE and x lets you do multiplication and addition the way you want. If you kept the arrays you’d have to loop.