I am learning rstan and at the moment I'm solving exercises from Gelmans "Bayesian Data Analysis". For reference this is about the example 5 in chapter 3.
It keeps failing with:
Initialization failed after 100 attempts.
Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
error occurred during calling the sampler; sampling not done
this is my R code:
library(rstan)
scode <- "
transformed data {
real o_data[5];
o_data[1] <- 10;
o_data[2] <- 10;
o_data[3] <- 12;
o_data[4] <- 11;
o_data[5] <- 9;
}
parameters {
real mu;
real<lower=0> sigma;
real tru_val[5];
}
model {
mu ~ uniform(0.0,20.0);
sigma ~ gamma(2,1);
for (i in 1:5) {
tru_val[i] ~ normal(mu,sigma);
tru_val[i] ~ uniform(o_data[i]-0.5, o_data[i]+0.5);
}
}
"
afit <- stan(model_code = scode, verbose=TRUE)
The funny thing is - if I change the second tru_val sampling to tru_val[i] ~ normal(o_data[i],0.5); the model will evaluate just fine.
So far I tried in the stan code:
- rearranging the sampling statements
- introducing helper variables
- explicitely writing
increment_log_pstatements - change variable names in case I had accidentially used a keyword
- add print statements in the stan code
- setting mu to 10
- relaxing/widening the constraints in the uniform distribution
- and combinations of above
I noticed something surprising, as I printed the values of tru_val that - no matter which order the statements - I make it prints values around 0 typically between -2 and +2 - even when I set mu <- 10; sigma <- 1; (in the data section) and the sampling statement tru_val[i] ~ uniform(9.5,10.5). I don't really see how it can get these numbers.
I really hope someone can shine some light onto this.