Failed initialization--New to STAN, not sure how to debug

I am trying to fit the following model using rstan:

wiener_model <- "

data {
  int<lower=0> N;
  int<lower=0> S;
  real y[N,S];
  int<lower=0, upper=1> resp[N,S];
  int<lower=0, upper=1> type[N,S];
  int<lower=1> time[N,S];

parameters {
   real<lower=0, upper=.5> g;
   real<lower=0, upper=10> a; 
   real<lower=0, upper=1> b_Con;
   real<lower=0, upper=1> b_Inc;
   real v;

model {
  g ~ gamma(2, 30);
  a ~ gamma(3, 1);
  b_Con ~ beta(2, 2);
  b_Inc ~ beta(2, 2);  
  v ~ normal(0, 1);

  for (j in 1:S) {
    for (i in 1:N) {
      if (type[i,j]==1){
        if (resp[i,j] == 1) {
          y[i,j] ~ wiener(a, time[i,j]^g, b_Con, v);
      else {
          y[i] ~ wiener(a, time[i,j]^g, 1-b_Con, -v);

      if (type[i,j]==0){
        if (resp[i,j] == 1) {
          y[i,j] ~ wiener(a, time[i,j]^g, b_Inc, v);
        else {
          y[i,j] ~ wiener(a, time[i,j]^g, 1-b_Inc, -v);

fit <- stan(model_code = wiener_model, data = standata, chains=1)

It works out all right for some subjects, but fails for a subset with the following error:

Initialization between (-2, 2) failed after 100 attempts. 
 Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] "Error in sampler$call_sampler(args_list[[i]]) : Initialization failed."
error occurred during calling the sampler; sampling not done

I’ve attached the data from two subjects for whom the model produces the error.
checking_data.csv (61.4 KB)

I organize these data as follows:

dat = read.csv("checking_data.csv")
standata <- list(y = cbind(dat$y.1,dat$y.2),
                 resp = cbind(dat$resp.1,dat$resp.2),
                 N = dat$N[1],
                 S = dat$S[1],
                 type = cbind(dat$type.1,dat$type.2),
                 time = cbind(dat$time.1,dat$time.2))

In general, I suspect that I’m misunderstanding how to write the model block correctly. I’m just not sure how to trouble shoot this.

Any help would be greatly appreciated!

I’d recommend updating to the 2.16 version, where you should get some indication in the way of a warning message as to what goes wrong. It’s rather hard to debug other people’s big programs by eye.

There’s a big problem in what you’re doing in that if you write y[i] ~ ... in the loop under j, you wind up giving y[i, j] a total of S times too much prior, because the sampling statement is already vectorized over the j.

You’re also confused about indentation. that first else goes with the lower if, not where it’s indented.

This’d be much more efficient if you grouped the data ahead of time rather than branching—then you could vectorize and the code would also be much simpler.

P.S. Indentation really helps with debugging.

Hi, Bob.

Thanks for the quick and helpful reply! Things are working much more smoothly now that I have expressive errors messages and have vectorized everything.


Hi Evan,

Would you be willing to share your updated code?

I’m attempting to do something similar (apply diffusion model to 2x4 within-subjects design) and am having issues getting Stan to work (also used a bunch of loops). I am curious to see how you vectorized your data.