I’m running a hierarchical model in rstan (v. 2.15.1, R version 3.4.0), and frequently – but not consistently – I get an error right after compilation:

Iteration: 1 / 500 [ 0%] (Warmup)

[1] "Error in sampler$call_sampler(args_list[[i]]) : "

[2] " c++ exception (unknown reason)"

error occurred during calling the sampler; sampling not done

I’ve tried to strip it down to the minimum that produces this error:

gmodel='data {

int<lower=0> n; // number of subjects

int<lower=0> k; // number of observations per subject

real r;

real y[k,n];

}

parameters {

real<lower=0> tau[n];

real<lower=0> beta;

}

transformed parameters {

real<lower=0> sigma[n];

real<lower=0> betas;

betas=beta+r;

for (i in 1:n){

sigma[i]=pow(tau[i],-.5);

}

}

//

model {

beta ~ exponential(1);

for(i in 1:n){

tau[i] ~ exponential(betas);

for (j in 1:k){

y[j,i] ~ normal(0,sigma[i]);

}

}

}’

nn=200

tau=rexp(nn,2)

kk=3

yy=sapply(tau,function(t) rnorm(kk,0,t^(-.5)))

gamdat=list(n=nn,y=yy,k=kk,r=.1)

gamstan=stan(model_code = gmodel,data = gamdat,iter=500,chains=1)

The transformed parameter betas is there because I thought maybe there was a problem with sampling negative values of beta. Indeed, setting r=1 – so betas=beta+1 – makes it run consistently, while r = .1 or anything lower pretty consistently crashes. But the code might run several times in a row, then crash on the fourth try, though it’s the same data each time. Once it crashes, it crashes immediately each time I run it again, until I force it to recompile by changing the model.

I was originally running it on RStan 2.16.2, but I reinstalled the older version because I thought that might be causing the problem, because it was compiled on R 3.4.1. But no change.

To make it even more confusing, I tried running exactly the same code on a Linux server, also with R 3.4.0 and RStan 2.16.2, and there it runs without a hitch. Obviously I could run everything on the server, and I guess I will, but I’d appreciate hearing if anyone has any insight into this.