Trouble with initializing multistate model

Problem initializing multistate model.


Dear Stan community,

I am trying to run a multistate model estimating vital rates. The dataset includes recapture and recovery data and we’re estimating survival, movement, recovery, and detection rates. We’re also including age, location of first capture (= cluster), and year as random/fixed covariates. This model works well in JAGS, but I run into an error that I cannot seem to fix while translating it to Stan.

This is the full model:

data {
  int<lower=2> n_occ;
  int<lower=1> n_ind;
  int<lower=1> CH[n_ind,n_occ];
  int<lower=1> AM[n_ind,n_occ];
  int<lower=1> n_age;
  int<lower=1> n_year;
  int<lower=1> n_cluster;
  int<lower=1> year[n_occ];
  int<lower=1> cluster[n_ind];
  int<lower=1> first[n_ind];
  int<lower=1> FR[n_ind];
  int<lower=1> ones[n_ind];
}
parameters {
  real<lower=0,upper=1> s_age[n_age];
  real<lower=0,upper=1> me_age[n_age];
  real<lower=0,upper=1> mi_age[n_age];
  real<lower=0,upper=1> p_age[n_age];
  real<lower=0,upper=1> r_age[n_age];
}
transformed parameters {
  // logit transformation
  real l_s_age[n_age];
  real l_p_age[n_age];
  real l_r_age[n_age];
  l_s_age <- logit(s_age);
  l_p_age <- logit(p_age);
  l_r_age <- logit(r_age);
  // derived parameters
  real<lower=0,upper=1> s_age_year[n_age,n_year];
  real<lower=0,upper=1> p_age_year[n_age,n_year];
  real<lower=0,upper=1> r_age_year[n_age,n_year];
  real<lower=0,upper=1> s_age_year_cluster[n_age,n_year,n_cluster];
  real<lower=0,upper=1> p_age_year_cluster[n_age,n_year,n_cluster];
  real<lower=0,upper=1> s_age_cluster[n_age,n_cluster];
  real<lower=0,upper=1> p_age_cluster[n_age,n_cluster];
  real l_s_age_year[n_age,n_year];
  real l_p_age_year[n_age,n_year];
  real l_r_age_year[n_age,n_year];
  real cluster_s[n_cluster];
  real cluster_p[n_cluster];
  for(a in 1:n_age) {
    for(y in 1:n_year) {
      s_age_year[a,y] <- inv_logit(l_s_age_year[a,y]);
      p_age_year[a,y] <- inv_logit(l_p_age_year[a,y]);
      r_age_year[a,y] <- inv_logit(l_r_age_year[a,y]);
      for(c in 1:n_cluster) {
        s_age_year_cluster[a,y,c] <- inv_logit(l_s_age_year[a,y] + cluster_s[c]);
        p_age_year_cluster[a,y,c] <- inv_logit(l_p_age_year[a,y] + cluster_s[c]);
      }
    }
    for(c in 1:n_cluster) {
      s_age_cluster[a,c] <- inv_logit(sum(l_s_age_year[a,1:n_year])/n_year + cluster_s[c]);
      p_age_cluster[a,c] <- inv_logit(sum(l_p_age_year[a,1:n_year])/n_year + cluster_p[c]);
    }
  }
  // linear predictors
  real<lower=0,upper=1> s[n_ind,n_occ-1];
  real<lower=0,upper=1> me[n_ind,n_occ-1];
  real<lower=0,upper=1> mi[n_ind,n_occ-1];
  real<lower=0,upper=1> p[n_ind,n_occ];
  real<lower=0,upper=1> r[n_ind,n_occ];
  for(i in 1:n_ind) {
    for(t in 1:(first[i]-1)) {
      s[i,t] <- 1;
      me[i,t] <- 1;
      mi[i,t] <- 1;
    }
    for(t in 1:first[i]) {
      p[i,t] <- 1;
      r[i,t] <- 1;
    }
    for(t in first[i]:(n_occ-1)) {
      s[i,t] <- inv_logit(l_s_age_year[AM[i,t],year[t]] + cluster_s[cluster[i]]);
      me[i,t] <- me_age[AM[i,t]];
      mi[i,t] <- mi_age[AM[i,t]];
    }
    for(t in (first[i]+1):n_occ) {
      p[i,t] <- inv_logit(l_p_age_year[AM[i,t],year[t]] + cluster_p[cluster[i]]);
      r[i,t] <- inv_logit(l_r_age_year[AM[i,t],year[t]]);
    }
  }
  // Define state-transition and observation matrices
  real<lower=0,upper=1> ps[5,n_ind,n_occ-1,5];
  real<lower=0,upper=1> po[5,n_ind,n_occ,5];
  for(i in 1:n_ind) {
    for(t in 1:(first[i]-1)) {
      for(ki in 1:5) {
        for(kj in 1:5) {
          ps[ki,i,t,kj] <- 1;
          po[ki,i,t,kj] <- 1;
        }
      }
    }
    for(t in first[i]:(n_occ-1)) {
      // Define probabilities of state S(t+1) given S(t)
      ps[1, i, t, 1] = s[i,t] * (1-me[i,t]);
      ps[1, i, t, 2] = s[i,t] * me[i,t];
      ps[1, i, t, 3] = (1-s[i,t]) * (1-me[i,t]) * r[i,t];
      ps[1, i, t, 4] = (1-s[i,t]) * me[i,t] * r[i,t];
      ps[1, i, t, 5] = (1-s[i,t]) * (1-r[i,t]);
      ps[2, i, t, 1] = s[i,t] * mi[i,t];
      ps[2, i, t, 2] = s[i,t] * (1-mi[i,t]);
      ps[2, i, t, 3] = (1-s[i,t]) * mi[i,t] * r[i,t];
      ps[2, i, t, 4] = (1-s[i,t]) * (1-mi[i,t]) * r[i,t];
      ps[2, i, t, 5] = (1-s[i,t]) * (1-r[i,t]);
      ps[3, i, t, 1] = 0;
      ps[3, i, t, 2] = 0;
      ps[3, i, t, 3] = 0;
      ps[3, i, t, 4] = 0;
      ps[3, i, t, 5] = 1;
      ps[4, i, t, 1] = 0;
      ps[4, i, t, 2] = 0;
      ps[4, i, t, 3] = 0;
      ps[4, i, t, 4] = 0;
      ps[4, i, t, 5] = 1;
      ps[5, i, t, 1] = 0;
      ps[5, i, t, 2] = 0;
      ps[5, i, t, 3] = 0;
      ps[5, i, t, 4] = 0;
      ps[5, i, t, 5] = 1;
      // Define probabilities of O(t) given S(t)
      po[1, i, t, 1] = p[i,t];
      po[1, i, t, 2] = 0;
      po[1, i, t, 3] = 0;
      po[1, i, t, 4] = 0;
      po[1, i, t, 5] = 1 - p[i,t];
      po[2, i, t, 1] = 0;
      po[2, i, t, 2] = p[i,t];
      po[2, i, t, 3] = 0;
      po[2, i, t, 4] = 0;
      po[2, i, t, 5] = 1 - p[i,t];
      po[3, i, t, 1] = 0;
      po[3, i, t, 2] = 0;
      po[3, i, t, 3] = 1;
      po[3, i, t, 4] = 0;
      po[3, i, t, 5] = 0;
      po[4, i, t, 1] = 0;
      po[4, i, t, 2] = 0;
      po[4, i, t, 3] = 0;
      po[4, i, t, 4] = 1;
      po[4, i, t, 5] = 0;
      po[5, i, t, 1] = 0;
      po[5, i, t, 2] = 0;
      po[5, i, t, 3] = 0;
      po[5, i, t, 4] = 0;
      po[5, i, t, 5] = 1;
    }
  }
  // Marginalisation
  real<lower=0,upper=1> zeta[n_ind,n_occ,5];
  for(i in 1:n_ind){
    for(t in 1:(first[i]-1)) {
      for(k in 1:5) {
        zeta[i,t,k] <- 0;
      }
    }
    zeta[i,first[i],1] <- 1;
    zeta[i,first[i],2] <- 0;
    zeta[i,first[i],3] <- 0;
    zeta[i,first[i],4] <- 0;
    zeta[i,first[i],5] <- 0;
    for(t in first[i]:(n_occ-1)) { 
      for(k in 1:5) {  
        zeta[i,(t+1),k] <- dot_product(zeta[i,t,], ps[,i,t,k]) * po[k,i,t,CH[i,(t+1)]];
      }
    }
  }
}
model {
  // Priors
  /// Year random effect
  real sigma_year_s[n_age];
  real sigma_year_p[n_age];
  real sigma_year_r[n_age];
  for(a in 1:n_age) {
    for(y in 1:n_year) {
      l_s_age_year[a,y] ~ normal(l_s_age[a], sigma_year_s[a]);
      l_p_age_year[a,y] ~ normal(l_p_age[a], sigma_year_p[a]);
      l_r_age_year[a,y] ~ normal(l_r_age[a], sigma_year_r[a]);
    }
    sigma_year_s[a] ~ uniform(0.01, 10);
    sigma_year_p[a] ~ uniform(0.01, 10);
    sigma_year_r[a] ~ uniform(0.01, 10);
  }
  /// Cluster random effect
  real sigma_cluster_s;
  real sigma_cluster_p;
  for(c in 1:n_cluster) {
    cluster_s[c] ~ normal(0, sigma_cluster_s);
    cluster_p[c] ~ normal(0, sigma_cluster_p);
  }
  sigma_cluster_s ~ uniform(0.01, 10);
  sigma_cluster_p ~ uniform(0.01, 10);
  // Likelihood 
  vector[n_ind] lik;
  for(i in 1:n_ind) {
    lik[i] <- sum(zeta[i,n_occ,]);
    ones[i] ~ binomial(FR[i], lik[i]);
  }
}

This is the rstan bit of code I use:

parameters.to.keep <- c("s_age", "s_age_year", "s_age_cluster", "s_age_year_cluster", 
                        "me_age", 
                        "mi_age", 
                        "p_age", "p_age_year", "p_age_cluster", "p_age_year_cluster",
                        "r_age", "r_age_year")
ni <- 100
nt <- 2
nb <- ni*0.5
nc <- 1
m <- stan(data = data_c, pars = parameters.to.keep, file = "model.stan",
          chains = nc, warmup = nb, iter = ni, thin = nt, cores = nc)

This is the error I receive:

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: model1d282102245__namespace::log_prob: s_age_year[1][1] is nan, but must be greater than or equal to 0.000000 (in 'string', line 66, column 2 to column 49)
Chain 1: 
Chain 1: Initialization between (-2, 2) failed after 100 attempts. 
Chain 1:  Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] "Error : Initialization failed."
[1] "error occurred during calling the sampler; sampling not done"

The parameter ‘s_age_year’ is the ‘survival per age per year’ (transformed parameters {}) and is simply the inverse logit of ‘l_s_age_year’ which is has its priors set in the ‘model {}’ block. I was thinking that I might be indexing some parameter wrong, or refer to the wrong occasion for some parameters, but I can’t find the mistake.

Any help is greatly appreciated!

Cheers,
Louis



I attached the dataset and stan file used for this model.
data_c.RData (10.0 KB)
model.stan (7.4 KB)

Versions:
rstan 2.32.6
R 4.3.3
Run on LENOVO ThinkPad, Windows 10 Pro

The program looks complicated - you may try to use print to check which variables were not properly initialized.

I would also not recommend to use artificial constraints for sigma such as uniform(0.01,10). You could log-transform the sigma, and put normal prior on log_sigma.

Dear @aakhmetz,

Thanks for your reply. since the model does not yet contains samples (as it failed to initialize) I cannot use print(m) to look at the output. Or is this not what you meant?
Also, as sigma is the standard deviation of the normal distribution and therefore cannot be < 0, I think I cannot use a log-transformws normal distribution as a prior. I can only use distributions that are constrained to > 0, such as uniform(0,1).

Cheers,
Louis

print(..) works even during initialization attempts, at least from CmdStan.

Stan User’s Guide has a section on translating BUGS models, and JAGS is basically the same as BUGS.

The specific error you’re getting says that s_age_year is uninitialized. In your model s_age_year is a simple transform of l_s_age_year which is also uninitialized as you do not assign a value to it before use. Stan will infer only the variables that are declared in the parameters section of the program. I think moving all the “unknown” parameters to the parameters section should fix the initialization problems, but the data file you have provided did not unzip for me so I didn’t test the following model.

data {
  int<lower=2> n_occ;
  int<lower=1> n_ind;
  int<lower=1> CH[n_ind,n_occ];
  int<lower=1> AM[n_ind,n_occ];
  int<lower=1> n_age;
  int<lower=1> n_year;
  int<lower=1> n_cluster;
  int<lower=1> year[n_occ];
  int<lower=1> cluster[n_ind];
  int<lower=1> first[n_ind];
  int<lower=1> FR[n_ind];
  int<lower=1> ones[n_ind];
}
parameters {
  real<lower=0,upper=1> s_age[n_age];
  real<lower=0,upper=1> me_age[n_age];
  real<lower=0,upper=1> mi_age[n_age];
  real<lower=0,upper=1> p_age[n_age];
  real<lower=0,upper=1> r_age[n_age];
  // latent parameters
  real l_s_age_year[n_age,n_year];
  real l_p_age_year[n_age,n_year];
  real l_r_age_year[n_age,n_year];
  real cluster_s[n_cluster];
  real cluster_p[n_cluster];
  real<lower=0.01,upper=1> sigma_year_s[n_age];
  real<lower=0.01,upper=1> sigma_year_p[n_age];
  real<lower=0.01,upper=1> sigma_year_r[n_age];
  real sigma_cluster_s;
  real sigma_cluster_p;
}
transformed parameters {
  // logit transformation
  real l_s_age[n_age];
  real l_p_age[n_age];
  real l_r_age[n_age];
  l_s_age <- logit(s_age);
  l_p_age <- logit(p_age);
  l_r_age <- logit(r_age);
  // derived parameters
  real<lower=0,upper=1> s_age_year[n_age,n_year];
  real<lower=0,upper=1> p_age_year[n_age,n_year];
  real<lower=0,upper=1> r_age_year[n_age,n_year];
  real<lower=0,upper=1> s_age_year_cluster[n_age,n_year,n_cluster];
  real<lower=0,upper=1> p_age_year_cluster[n_age,n_year,n_cluster];
  real<lower=0,upper=1> s_age_cluster[n_age,n_cluster];
  real<lower=0,upper=1> p_age_cluster[n_age,n_cluster];
  for(a in 1:n_age) {
    for(y in 1:n_year) {
      s_age_year[a,y] <- inv_logit(l_s_age_year[a,y]);
      p_age_year[a,y] <- inv_logit(l_p_age_year[a,y]);
      r_age_year[a,y] <- inv_logit(l_r_age_year[a,y]);
      for(c in 1:n_cluster) {
        s_age_year_cluster[a,y,c] <- inv_logit(l_s_age_year[a,y] + cluster_s[c]);
        p_age_year_cluster[a,y,c] <- inv_logit(l_p_age_year[a,y] + cluster_s[c]);
      }
    }
    for(c in 1:n_cluster) {
      s_age_cluster[a,c] <- inv_logit(mean(l_s_age_year[a,1:n_year]) + cluster_s[c]);
      p_age_cluster[a,c] <- inv_logit(mean(l_p_age_year[a,1:n_year]) + cluster_p[c]);
    }
  }
  // linear predictors
  real<lower=0,upper=1> s[n_ind,n_occ-1];
  real<lower=0,upper=1> me[n_ind,n_occ-1];
  real<lower=0,upper=1> mi[n_ind,n_occ-1];
  real<lower=0,upper=1> p[n_ind,n_occ];
  real<lower=0,upper=1> r[n_ind,n_occ];
  for(i in 1:n_ind) {
    for(t in 1:(first[i]-1)) {
      s[i,t] <- 1;
      me[i,t] <- 1;
      mi[i,t] <- 1;
    }
    for(t in 1:first[i]) {
      p[i,t] <- 1;
      r[i,t] <- 1;
    }
    for(t in first[i]:(n_occ-1)) {
      s[i,t] <- inv_logit(l_s_age_year[AM[i,t],year[t]] + cluster_s[cluster[i]]);
      me[i,t] <- me_age[AM[i,t]];
      mi[i,t] <- mi_age[AM[i,t]];
    }
    for(t in (first[i]+1):n_occ) {
      p[i,t] <- inv_logit(l_p_age_year[AM[i,t],year[t]] + cluster_p[cluster[i]]);
      r[i,t] <- inv_logit(l_r_age_year[AM[i,t],year[t]]);
    }
  }
  // Define state-transition and observation matrices
  real<lower=0,upper=1> ps[5,n_ind,n_occ-1,5];
  real<lower=0,upper=1> po[5,n_ind,n_occ,5];
  for(i in 1:n_ind) {
    for(t in 1:(first[i]-1)) {
      for(ki in 1:5) {
        for(kj in 1:5) {
          ps[ki,i,t,kj] <- 1;
          po[ki,i,t,kj] <- 1;
        }
      }
    }
    for(t in first[i]:(n_occ-1)) {
      // Define probabilities of state S(t+1) given S(t)
      ps[1, i, t, 1] = s[i,t] * (1-me[i,t]);
      ps[1, i, t, 2] = s[i,t] * me[i,t];
      ps[1, i, t, 3] = (1-s[i,t]) * (1-me[i,t]) * r[i,t];
      ps[1, i, t, 4] = (1-s[i,t]) * me[i,t] * r[i,t];
      ps[1, i, t, 5] = (1-s[i,t]) * (1-r[i,t]);
      ps[2, i, t, 1] = s[i,t] * mi[i,t];
      ps[2, i, t, 2] = s[i,t] * (1-mi[i,t]);
      ps[2, i, t, 3] = (1-s[i,t]) * mi[i,t] * r[i,t];
      ps[2, i, t, 4] = (1-s[i,t]) * (1-mi[i,t]) * r[i,t];
      ps[2, i, t, 5] = (1-s[i,t]) * (1-r[i,t]);
      ps[3, i, t, 1] = 0;
      ps[3, i, t, 2] = 0;
      ps[3, i, t, 3] = 0;
      ps[3, i, t, 4] = 0;
      ps[3, i, t, 5] = 1;
      ps[4, i, t, 1] = 0;
      ps[4, i, t, 2] = 0;
      ps[4, i, t, 3] = 0;
      ps[4, i, t, 4] = 0;
      ps[4, i, t, 5] = 1;
      ps[5, i, t, 1] = 0;
      ps[5, i, t, 2] = 0;
      ps[5, i, t, 3] = 0;
      ps[5, i, t, 4] = 0;
      ps[5, i, t, 5] = 1;
      // Define probabilities of O(t) given S(t)
      po[1, i, t, 1] = p[i,t];
      po[1, i, t, 2] = 0;
      po[1, i, t, 3] = 0;
      po[1, i, t, 4] = 0;
      po[1, i, t, 5] = 1 - p[i,t];
      po[2, i, t, 1] = 0;
      po[2, i, t, 2] = p[i,t];
      po[2, i, t, 3] = 0;
      po[2, i, t, 4] = 0;
      po[2, i, t, 5] = 1 - p[i,t];
      po[3, i, t, 1] = 0;
      po[3, i, t, 2] = 0;
      po[3, i, t, 3] = 1;
      po[3, i, t, 4] = 0;
      po[3, i, t, 5] = 0;
      po[4, i, t, 1] = 0;
      po[4, i, t, 2] = 0;
      po[4, i, t, 3] = 0;
      po[4, i, t, 4] = 1;
      po[4, i, t, 5] = 0;
      po[5, i, t, 1] = 0;
      po[5, i, t, 2] = 0;
      po[5, i, t, 3] = 0;
      po[5, i, t, 4] = 0;
      po[5, i, t, 5] = 1;
    }
  }
  // Marginalisation
  real<lower=0,upper=1> zeta[n_ind,n_occ,5];
  for(i in 1:n_ind){
    for(t in 1:(first[i]-1)) {
      for(k in 1:5) {
        zeta[i,t,k] <- 0;
      }
    }
    zeta[i,first[i],1] <- 1;
    zeta[i,first[i],2] <- 0;
    zeta[i,first[i],3] <- 0;
    zeta[i,first[i],4] <- 0;
    zeta[i,first[i],5] <- 0;
    for(t in first[i]:(n_occ-1)) { 
      for(k in 1:5) {  
        zeta[i,(t+1),k] <- dot_product(zeta[i,t,], ps[,i,t,k]) * po[k,i,t,CH[i,(t+1)]];
      }
    }
  }
}
model {
  // Priors
  /// Year random effect
  for(a in 1:n_age) {
    for(y in 1:n_year) {
      l_s_age_year[a,y] ~ normal(l_s_age[a], sigma_year_s[a]);
      l_p_age_year[a,y] ~ normal(l_p_age[a], sigma_year_p[a]);
      l_r_age_year[a,y] ~ normal(l_r_age[a], sigma_year_r[a]);
    }
    sigma_year_s[a] ~ uniform(0.01, 10);
    sigma_year_p[a] ~ uniform(0.01, 10);
    sigma_year_r[a] ~ uniform(0.01, 10);
  }
  /// Cluster random effect
  for(c in 1:n_cluster) {
    cluster_s[c] ~ normal(0, sigma_cluster_s);
    cluster_p[c] ~ normal(0, sigma_cluster_p);
  }
  sigma_cluster_s ~ uniform(0.01, 10);
  sigma_cluster_p ~ uniform(0.01, 10);
  // Likelihood 
  vector[n_ind] lik;
  for(i in 1:n_ind) {
    lik[i] <- sum(zeta[i,n_occ,]);
    ones[i] ~ binomial(FR[i], lik[i]);
  }
}

Dear @nhuurre,

Thanks a lot for your reply!
I still can’t use the print() to work via rstan, but I guess that’s not essential.

Thanks for pointing out to use the parameters section for initializing variables. This solved the previous error.

Now I run into another issue. Code below. I also try to attach the data again.
model_data.RData (1.4 MB)

data {
  int<lower=2> n_occ;
  int<lower=1> n_ind;
  int<lower=1> CH[n_ind,n_occ];
  int<lower=1> AM[n_ind,n_occ];
  int<lower=1> n_age;
  int<lower=1> n_year;
  int<lower=1> n_cluster;
  int<lower=1> year[n_occ];
  int<lower=1> cluster[n_ind];
  int<lower=1> first[n_ind];
  int<lower=1> FR[n_ind];
  int<lower=1> ones[n_ind];
}
parameters {
  real<lower=0,upper=1> s_age[n_age];
  real<lower=0,upper=1> me_age[n_age];
  real<lower=0,upper=1> mi_age[n_age];
  real<lower=0,upper=1> p_age[n_age];
  real<lower=0,upper=1> r_age[n_age];
  // latent parameters
  real l_s_age_year[n_age,n_year];
  real l_p_age_year[n_age,n_year];
  real l_r_age_year[n_age,n_year];
  real cluster_s[n_cluster];
  real cluster_p[n_cluster];
  real<lower=0.01,upper=10> sigma_year_s[n_age];
  real<lower=0.01,upper=10> sigma_year_p[n_age];
  real<lower=0.01,upper=10> sigma_year_r[n_age];
  real<lower=0.01,upper=10> sigma_cluster_s[n_cluster];
  real<lower=0.01,upper=10> sigma_cluster_p[n_cluster];
}
transformed parameters {
  // logit transformation
  real l_s_age[n_age];
  real l_p_age[n_age];
  real l_r_age[n_age];
  l_s_age <- logit(s_age);
  l_p_age <- logit(p_age);
  l_r_age <- logit(r_age);
  // derived parameters
  real<lower=0,upper=1> s_age_year[n_age,n_year];
  real<lower=0,upper=1> p_age_year[n_age,n_year];
  real<lower=0,upper=1> r_age_year[n_age,n_year];
  real<lower=0,upper=1> s_age_year_cluster[n_age,n_year,n_cluster];
  real<lower=0,upper=1> p_age_year_cluster[n_age,n_year,n_cluster];
  real<lower=0,upper=1> s_age_cluster[n_age,n_cluster];
  real<lower=0,upper=1> p_age_cluster[n_age,n_cluster];
  for(a in 1:n_age) {
    for(y in 1:n_year) {
      s_age_year[a,y] <- inv_logit(l_s_age_year[a,y]);
      p_age_year[a,y] <- inv_logit(l_p_age_year[a,y]);
      r_age_year[a,y] <- inv_logit(l_r_age_year[a,y]);
      for(c in 1:n_cluster) {
        s_age_year_cluster[a,y,c] <- inv_logit(l_s_age_year[a,y] + cluster_s[c]);
        p_age_year_cluster[a,y,c] <- inv_logit(l_p_age_year[a,y] + cluster_s[c]);
      }
    }
    for(c in 1:n_cluster) {
      s_age_cluster[a,c] <- inv_logit(mean(l_s_age_year[a,1:n_year]) + cluster_s[c]);
      p_age_cluster[a,c] <- inv_logit(mean(l_p_age_year[a,1:n_year]) + cluster_p[c]);
    }
  }
  // linear predictors
  real<lower=0,upper=1> s[n_ind,n_occ-1];
  real<lower=0,upper=1> me[n_ind,n_occ-1];
  real<lower=0,upper=1> mi[n_ind,n_occ-1];
  real<lower=0,upper=1> p[n_ind,n_occ];
  real<lower=0,upper=1> r[n_ind,n_occ];
  for(i in 1:n_ind) {
    for(t in 1:(first[i]-1)) {
      s[i,t] <- 1;
      me[i,t] <- 1;
      mi[i,t] <- 1;
    }
    for(t in 1:first[i]) {
      p[i,t] <- 1;
      r[i,t] <- 1;
    }
    for(t in first[i]:(n_occ-1)) {
      s[i,t] <- inv_logit(l_s_age_year[AM[i,t],year[t]] + cluster_s[cluster[i]]);
      me[i,t] <- me_age[AM[i,t]];
      mi[i,t] <- mi_age[AM[i,t]];
    }
    for(t in (first[i]+1):n_occ) {
      p[i,t] <- inv_logit(l_p_age_year[AM[i,t],year[t]] + cluster_p[cluster[i]]);
      r[i,t] <- inv_logit(l_r_age_year[AM[i,t],year[t]]);
    }
  }
  // Define state-transition and observation matrices
  real<lower=0,upper=1> ps[5,n_ind,n_occ-1,5];
  real<lower=0,upper=1> po[5,n_ind,n_occ-1,5];
  for(i in 1:n_ind) {
    for(t in 1:(first[i]-1)) {
      for(ki in 1:5) {
        for(kj in 1:5) {
          ps[ki,i,t,kj] <- 1;
          po[ki,i,t,kj] <- 1;
        }
      }
    }
    for(t in first[i]:(n_occ-1)) {
      // Define probabilities of state S(t+1) given S(t)
      ps[1, i, t, 1] = s[i,t] * (1-me[i,t]);
      ps[1, i, t, 2] = s[i,t] * me[i,t];
      ps[1, i, t, 3] = (1-s[i,t]) * (1-me[i,t]) * r[i,t];
      ps[1, i, t, 4] = (1-s[i,t]) * me[i,t] * r[i,t];
      ps[1, i, t, 5] = (1-s[i,t]) * (1-r[i,t]);
      ps[2, i, t, 1] = s[i,t] * mi[i,t];
      ps[2, i, t, 2] = s[i,t] * (1-mi[i,t]);
      ps[2, i, t, 3] = (1-s[i,t]) * mi[i,t] * r[i,t];
      ps[2, i, t, 4] = (1-s[i,t]) * (1-mi[i,t]) * r[i,t];
      ps[2, i, t, 5] = (1-s[i,t]) * (1-r[i,t]);
      ps[3, i, t, 1] = 0;
      ps[3, i, t, 2] = 0;
      ps[3, i, t, 3] = 0;
      ps[3, i, t, 4] = 0;
      ps[3, i, t, 5] = 1;
      ps[4, i, t, 1] = 0;
      ps[4, i, t, 2] = 0;
      ps[4, i, t, 3] = 0;
      ps[4, i, t, 4] = 0;
      ps[4, i, t, 5] = 1;
      ps[5, i, t, 1] = 0;
      ps[5, i, t, 2] = 0;
      ps[5, i, t, 3] = 0;
      ps[5, i, t, 4] = 0;
      ps[5, i, t, 5] = 1;
      // Define probabilities of O(t) given S(t)
      po[1, i, t, 1] = p[i,t];
      po[1, i, t, 2] = 0;
      po[1, i, t, 3] = 0;
      po[1, i, t, 4] = 0;
      po[1, i, t, 5] = 1 - p[i,t];
      po[2, i, t, 1] = 0;
      po[2, i, t, 2] = p[i,t];
      po[2, i, t, 3] = 0;
      po[2, i, t, 4] = 0;
      po[2, i, t, 5] = 1 - p[i,t];
      po[3, i, t, 1] = 0;
      po[3, i, t, 2] = 0;
      po[3, i, t, 3] = 1;
      po[3, i, t, 4] = 0;
      po[3, i, t, 5] = 0;
      po[4, i, t, 1] = 0;
      po[4, i, t, 2] = 0;
      po[4, i, t, 3] = 0;
      po[4, i, t, 4] = 1;
      po[4, i, t, 5] = 0;
      po[5, i, t, 1] = 0;
      po[5, i, t, 2] = 0;
      po[5, i, t, 3] = 0;
      po[5, i, t, 4] = 0;
      po[5, i, t, 5] = 1;
    }
  }
  // Marginalisation
  real<lower=0,upper=1> zeta[n_ind,n_occ,5];
  for(i in 1:n_ind){
    for(t in 1:(first[i]-1)) {
      for(k in 1:5) {
        zeta[i,t,k] <- 0;
      }
    }
    zeta[i,first[i],1] <- 1;
    zeta[i,first[i],2] <- 0;
    zeta[i,first[i],3] <- 0;
    zeta[i,first[i],4] <- 0;
    zeta[i,first[i],5] <- 0;
    for(t in first[i]:(n_occ-1)) { 
      for(k in 1:5) {  
        zeta[i,(t+1),k] <- dot_product(zeta[i,t,], ps[,i,t,k]) * po[k,i,t,CH[i,(t+1)]];
      }
    }
  }
}
model {
  // Priors
  /// Year random effect
  for(a in 1:n_age) {
    for(y in 1:n_year) {
      l_s_age_year[a,y] ~ normal(l_s_age[a], sigma_year_s[a]);
      l_p_age_year[a,y] ~ normal(l_p_age[a], sigma_year_p[a]);
      l_r_age_year[a,y] ~ normal(l_r_age[a], sigma_year_r[a]);
    }
    sigma_year_s[a] ~ uniform(0.01, 10);
    sigma_year_p[a] ~ uniform(0.01, 10);
    sigma_year_r[a] ~ uniform(0.01, 10);
  }
  /// Cluster random effect
  for(c in 1:n_cluster) {
    cluster_s[c] ~ normal(0, sigma_cluster_s);
    cluster_p[c] ~ normal(0, sigma_cluster_p);
  }
  sigma_cluster_s ~ uniform(0.01, 10);
  sigma_cluster_p ~ uniform(0.01, 10);
  // Likelihood 
  vector[n_ind] lik;
  for(i in 1:n_ind) {
    lik[i] <- sum(zeta[i,n_occ,]);
    ones[i] ~ binomial(FR[i], lik[i]);
  }
}

The error I get now is the following:

Chain 1: Rejecting initial value:
Chain 1:   Log probability evaluates to log(0), i.e. negative infinity.
Chain 1:   Stan can't start sampling from this initial value.
Chain 1: 
Chain 1: Initialization between (-2, 2) failed after 100 attempts. 
Chain 1:  Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] "Error : Initialization failed."
[1] "error occurred during calling the sampler; sampling not done"

I think all my variables that are not on the logit scale have at least a lower bound of 0. So I can’t really see where this error lies. Unfortunately, no line numbers are given to help with debugging.

Apologies for the potentially obvious questions!

Thank you very much,
Louis

Still says “An error occured while extracting files.”

That log(0) error usually means some intermediate values are too large or small and rounding errors corrupt the result.
The model is a Hidden Markov Model where zeta[i,t,k] is the unnormalized probability of state k at time t, and ones and FR are both just [1,1,1,1,1], right?
My guess would be zeta becomes too small at some point and rounds to zero. If so, normalizing it at every step might help.

    for(t in first[i]:(n_occ-1)) { 
      for(k in 1:5) {  
        zeta[i,(t+1),k] <- dot_product(zeta[i,t,], ps[,i,t,k]) * po[k,i,t,CH[i,(t+1)]];
      }
      normalization[i,t+1] = sum(zeta[i,t+1,:]);
      zeta[i,t+1,:] = zeta[i,t+1,:] / normalization[i,t+1];
    }

Then add the normalization constants to the likelihood at the end

  for(i in 1:n_ind) {
    //lik[i] <- sum(zeta[i,n_occ,]);
    //ones[i] ~ binomial(FR[i], lik[i]); // equivalent to `target += log(lik[i]);`
    for (t in first[i]:n_occ-1) {
      target += log(normalization[i,t+1]);
    }
  }

If that doesn’t work you could try omitting the likelihood from the model to see if it initializes, or use RStan’s expose_stan_functions() to examine the likelihood computation without running the model.

Dear @nhuurre,

That makes sense. Thanks for the information. I got an error using the colon [,,:], so I replaced that with a loop. I also initialized the normalization parameter.

 // Marginalisation
  real<lower=0,upper=1> zeta[n_ind,n_occ,5];
  real<lower=0,upper=5> normalization[n_ind,n_occ];
  for(i in 1:n_ind){
    for(t in 1:(first[i]-1)) {
      for(k in 1:5) {
        zeta[i,t,k] <- 0;
      }
    }
    zeta[i,first[i],1] <- 1;
    zeta[i,first[i],2] <- 0;
    zeta[i,first[i],3] <- 0;
    zeta[i,first[i],4] <- 0;
    zeta[i,first[i],5] <- 0;
    for(t in first[i]:(n_occ-1)) { 
      for(k in 1:5) {  
        zeta[i,(t+1),k] <- dot_product(zeta[i,t,], ps[,i,t,k]) * po[k,i,t,CH[i,(t+1)]];
      }
      normalization[i,t+1] = sum(zeta[i,t+1,:]);
      for(k in 1:5) {
        zeta[i,t+1,k] = zeta[i,t+1,k] / normalization[i,t+1];
      }
    }
  }

This now results in a different error:

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: model1d282a1831e__namespace::log_prob: zeta[1, 4][1] is nan, but must be greater than or equal to 0.000000 (in 'string', line 158, column 2 to column 44)
Chain 1: 
Chain 1: Initialization between (-2, 2) failed after 100 attempts. 
Chain 1:  Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] "Error : Initialization failed."
[1] "error occurred during calling the sampler; sampling not done"

To help troubleshooting, I’ve added the data in this rather messy way. The capture history matrix and the age class matrix were too big, so these are attached as .csv files. I hope this works by just copy-pasting and running the whole code.

AM.csv (22.4 KB)
CH.csv (22.4 KB)

library(rstan)

n_age = 2
n_cluster = 15
n_ind = 739
n_occ = 15
n_year = 15
year = 1:15

AM <- as.matrix(read.csv("AM.csv"))
CH <- as.matrix(read.csv("CH.csv"))

cluster <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10, 10, 10, 10, 
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 
10, 10, 10, 10, 10, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3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, 1, 1, 2, 2, 1, 1, 1, 525, 3, 1, 1, 1, 1, 2, 
1, 2, 513, 1, 9, 1, 1, 3, 1, 1, 1, 412, 1, 10, 1, 1, 2, 1, 2, 
386, 3, 3, 1, 2, 2, 581, 1, 6, 1, 1, 682, 4, 1, 1, 95, 2, 1, 
110, 1, 1, 27, 3, 1, 3, 2, 1, 1, 2, 227, 2, 7, 1, 1, 1, 1, 1, 
265, 2, 1, 118, 8, 2, 1, 1, 2, 276, 4, 1, 1, 1, 1, 139, 2, 1, 
1, 1, 4, 1, 394, 7, 1, 1, 79, 5, 2, 89, 1, 1, 153, 1, 28, 2, 
245, 1, 1, 1, 3, 1, 2, 1, 115, 2, 6, 5, 1, 1, 118, 1, 1, 1, 1, 
1, 163, 1, 2, 1, 1, 228, 1, 1, 82, 1, 1, 116, 1, 1, 1, 1, 1, 
1, 136, 1, 1, 1, 57, 1, 1, 1, 125, 4, 1, 146, 1, 12, 84, 1, 1, 
10, 1, 49, 1, 22, 7, 29, 1, 5, 1, 40, 1, 2, 28, 1, 49, 1, 15, 
51, 1, 22, 1, 1, 2, 1, 1, 1, 1, 3, 1, 2, 1, 2, 1, 1, 1, 137, 
1, 8, 1, 1, 1, 12, 2, 1, 1, 1, 1, 186, 1, 1, 2, 1, 1, 2, 425, 
14, 1, 2, 1, 1, 1, 1, 354, 4, 2, 1, 1, 1, 77, 1, 1, 123, 1, 1, 
98, 42, 1, 1, 1, 2, 163, 1, 2, 97, 2, 1, 2, 168, 2, 209, 2)

data_c <- list(CH = CH, n_ind = dim(CH)[1], n_occ = dim(CH)[2], first = first, # base data
               AM = AM, year = year, cluster = cluster, # additional input data
               n_age = n_age, n_year = n_year, n_cluster = n_cluster, # length of parameters
               FR = FR, ones = ones) # marginalization

# Parameters
parameters.to.keep <- c("s_age", "s_age_year", "s_age_cluster", "s_age_year_cluster", 
                        "me_age", 
                        "mi_age", 
                        "p_age", "p_age_year", "p_age_cluster", "p_age_year_cluster",
                        "r_age", "r_age_year")

ni <- 10
nt <- 2
nb <- ni*0.5
nc <- 1
m <- stan(data = data_c, pars = parameters.to.keep, file = "model.stan",
          chains = nc, warmup = nb, iter = ni, thin = nt, cores = nc)

Thank you very much for you help!

Cheers,
Louis

Forget the normalization idea, I was confused about the model, and with only 15 or less observations in a sequence, zeta should stay large enough for numerical accuracy not to be a problem.

I was able to get the original model run with minimal changes—see the // NOTE: comments below.
I believe that setting p[i,first[i]] <- 1 in the original model caused the model to assume that if an animal is not seen again immediately after the first observation then it must have died, but this contradicts the data as many animals are seen alive later.

data {
  int<lower=2> n_occ;
  int<lower=1> n_ind;
  int<lower=1> CH[n_ind,n_occ];
  int<lower=1> AM[n_ind,n_occ];
  int<lower=1> n_age;
  int<lower=1> n_year;
  int<lower=1> n_cluster;
  int<lower=1> year[n_occ];
  int<lower=1> cluster[n_ind];
  int<lower=1> first[n_ind];
  int<lower=1> FR[n_ind];
  int<lower=1> ones[n_ind];
}
parameters {
  real<lower=0,upper=1> s_age[n_age];
  real<lower=0,upper=1> me_age[n_age];
  real<lower=0,upper=1> mi_age[n_age];
  real<lower=0,upper=1> p_age[n_age];
  real<lower=0,upper=1> r_age[n_age];
  // NOTE: latent parameters declared here
  real l_s_age_year[n_age,n_year];
  real l_p_age_year[n_age,n_year];
  real l_r_age_year[n_age,n_year];
  real cluster_s[n_cluster];
  real cluster_p[n_cluster];
  real<lower=0.01,upper=10> sigma_year_s[n_age];
  real<lower=0.01,upper=10> sigma_year_p[n_age];
  real<lower=0.01,upper=10> sigma_year_r[n_age];
  real<lower=0.01,upper=10> sigma_cluster_s;
  real<lower=0.01,upper=10> sigma_cluster_p;
}
transformed parameters {
  // logit transformation
  real l_s_age[n_age];
  real l_p_age[n_age];
  real l_r_age[n_age];
  l_s_age <- logit(s_age);
  l_p_age <- logit(p_age);
  l_r_age <- logit(r_age);
  // derived parameters
  real<lower=0,upper=1> s_age_year[n_age,n_year];
  real<lower=0,upper=1> p_age_year[n_age,n_year];
  real<lower=0,upper=1> r_age_year[n_age,n_year];
  real<lower=0,upper=1> s_age_year_cluster[n_age,n_year,n_cluster];
  real<lower=0,upper=1> p_age_year_cluster[n_age,n_year,n_cluster];
  real<lower=0,upper=1> s_age_cluster[n_age,n_cluster];
  real<lower=0,upper=1> p_age_cluster[n_age,n_cluster];
  for(a in 1:n_age) {
    for(y in 1:n_year) {
      s_age_year[a,y] <- inv_logit(l_s_age_year[a,y]);
      p_age_year[a,y] <- inv_logit(l_p_age_year[a,y]);
      r_age_year[a,y] <- inv_logit(l_r_age_year[a,y]);
      for(c in 1:n_cluster) {
        s_age_year_cluster[a,y,c] <- inv_logit(l_s_age_year[a,y] + cluster_s[c]);
        p_age_year_cluster[a,y,c] <- inv_logit(l_p_age_year[a,y] + cluster_s[c]);
      }
    }
    for(c in 1:n_cluster) {
      s_age_cluster[a,c] <- inv_logit(sum(l_s_age_year[a,1:n_year])/n_year + cluster_s[c]);
      p_age_cluster[a,c] <- inv_logit(sum(l_p_age_year[a,1:n_year])/n_year + cluster_p[c]);
    }
  }
  // linear predictors
  real<lower=0,upper=1> s[n_ind,n_occ-1];
  real<lower=0,upper=1> me[n_ind,n_occ-1];
  real<lower=0,upper=1> mi[n_ind,n_occ-1];
  real<lower=0,upper=1> p[n_ind,n_occ];
  real<lower=0,upper=1> r[n_ind,n_occ];
  for(i in 1:n_ind) {
    for(t in 1:(first[i]-1)) {
      s[i,t] <- 1;
      me[i,t] <- 1;
      mi[i,t] <- 1;
    }
    for(t in 1:(first[i]-1)) {
      p[i,t] <- 1;
      r[i,t] <- 1;
    }
    // NOTE: `p[i,t]` is the probability for observing `CH[i,t+1]` -- maybe off-by-one indexing error?
    p[i,first[i]] <- inv_logit(l_p_age_year[AM[i,first[i]],year[first[i]]] + cluster_p[cluster[i]]);
    r[i,first[i]] <- 1;
    for(t in first[i]:(n_occ-1)) {
      s[i,t] <- inv_logit(l_s_age_year[AM[i,t],year[t]] + cluster_s[cluster[i]]);
      me[i,t] <- me_age[AM[i,t]];
      mi[i,t] <- mi_age[AM[i,t]];
    }
    for(t in (first[i]+1):n_occ) {
      p[i,t] <- inv_logit(l_p_age_year[AM[i,t],year[t]] + cluster_p[cluster[i]]);
      r[i,t] <- inv_logit(l_r_age_year[AM[i,t],year[t]]);
    }
  }
  // Define state-transition and observation matrices
  real<lower=0,upper=1> ps[5,n_ind,n_occ-1,5];
  real<lower=0,upper=1> po[5,n_ind,n_occ,5];
  for(i in 1:n_ind) {
    for(t in 1:(first[i]-1)) {
      for(ki in 1:5) {
        for(kj in 1:5) {
          ps[ki,i,t,kj] <- 1;
          po[ki,i,t,kj] <- 1;
        }
      }
    }
    for(t in first[i]:(n_occ-1)) {
      // Define probabilities of state S(t+1) given S(t)
      ps[1, i, t, 1] = s[i,t] * (1-me[i,t]);
      ps[1, i, t, 2] = s[i,t] * me[i,t];
      ps[1, i, t, 3] = (1-s[i,t]) * (1-me[i,t]) * r[i,t];
      ps[1, i, t, 4] = (1-s[i,t]) * me[i,t] * r[i,t];
      ps[1, i, t, 5] = (1-s[i,t]) * (1-r[i,t]);
      ps[2, i, t, 1] = s[i,t] * mi[i,t];
      ps[2, i, t, 2] = s[i,t] * (1-mi[i,t]);
      ps[2, i, t, 3] = (1-s[i,t]) * mi[i,t] * r[i,t];
      ps[2, i, t, 4] = (1-s[i,t]) * (1-mi[i,t]) * r[i,t];
      ps[2, i, t, 5] = (1-s[i,t]) * (1-r[i,t]);
      ps[3, i, t, 1] = 0;
      ps[3, i, t, 2] = 0;
      ps[3, i, t, 3] = 0;
      ps[3, i, t, 4] = 0;
      ps[3, i, t, 5] = 1;
      ps[4, i, t, 1] = 0;
      ps[4, i, t, 2] = 0;
      ps[4, i, t, 3] = 0;
      ps[4, i, t, 4] = 0;
      ps[4, i, t, 5] = 1;
      ps[5, i, t, 1] = 0;
      ps[5, i, t, 2] = 0;
      ps[5, i, t, 3] = 0;
      ps[5, i, t, 4] = 0;
      ps[5, i, t, 5] = 1;
      // Define probabilities of O(t) given S(t)
      po[1, i, t, 1] = p[i,t];
      po[1, i, t, 2] = 0;
      po[1, i, t, 3] = 0;
      po[1, i, t, 4] = 0;
      po[1, i, t, 5] = 1 - p[i,t];
      po[2, i, t, 1] = 0;
      po[2, i, t, 2] = p[i,t];
      po[2, i, t, 3] = 0;
      po[2, i, t, 4] = 0;
      po[2, i, t, 5] = 1 - p[i,t];
      po[3, i, t, 1] = 0;
      po[3, i, t, 2] = 0;
      po[3, i, t, 3] = 1;
      po[3, i, t, 4] = 0;
      po[3, i, t, 5] = 0;
      po[4, i, t, 1] = 0;
      po[4, i, t, 2] = 0;
      po[4, i, t, 3] = 0;
      po[4, i, t, 4] = 1;
      po[4, i, t, 5] = 0;
      po[5, i, t, 1] = 0;
      po[5, i, t, 2] = 0;
      po[5, i, t, 3] = 0;
      po[5, i, t, 4] = 0;
      po[5, i, t, 5] = 1;
    }
    // NOTE: `po[,i,n_occ,]` unused but still needs to be initialized
    for(ki in 1:5) {
      for(kj in 1:5) {
        po[ki,i,n_occ,kj] <- 0;
      }
    }
  }
  // Marginalisation
  real<lower=0,upper=1> zeta[n_ind,n_occ,5];
  for(i in 1:n_ind){
    for(t in 1:(first[i]-1)) {
      for(k in 1:5) {
        zeta[i,t,k] <- 0;
      }
    }
    zeta[i,first[i],1] <- 1;
    zeta[i,first[i],2] <- 0;
    zeta[i,first[i],3] <- 0;
    zeta[i,first[i],4] <- 0;
    zeta[i,first[i],5] <- 0;
    for(t in first[i]:(n_occ-1)) { 
      for(k in 1:5) {  
        zeta[i,(t+1),k] <- dot_product(zeta[i,t,], ps[,i,t,k]) * po[k,i,t,CH[i,(t+1)]];
      }
    }
  }
}
model {
  // Priors
  /// Year random effect
  for(a in 1:n_age) {
    for(y in 1:n_year) {
      l_s_age_year[a,y] ~ normal(l_s_age[a], sigma_year_s[a]);
      l_p_age_year[a,y] ~ normal(l_p_age[a], sigma_year_p[a]);
      l_r_age_year[a,y] ~ normal(l_r_age[a], sigma_year_r[a]);
    }
    sigma_year_s[a] ~ uniform(0.01, 10);
    sigma_year_p[a] ~ uniform(0.01, 10);
    sigma_year_r[a] ~ uniform(0.01, 10);
  }
  /// Cluster random effect
  for(c in 1:n_cluster) {
    cluster_s[c] ~ normal(0, sigma_cluster_s);
    cluster_p[c] ~ normal(0, sigma_cluster_p);
  }
  sigma_cluster_s ~ uniform(0.01, 10);
  sigma_cluster_p ~ uniform(0.01, 10);
  // Likelihood 
  vector[n_ind] lik;
  for(i in 1:n_ind) {
    lik[i] <- sum(zeta[i,n_occ,]);
    ones[i] ~ binomial(FR[i], lik[i]);
  }
}

Dear @nhuurre,

You did it! Thank you very much for your help. I adjusted the assignments and linear predictors slightly so it makes sense to me (see below). Not a model/Stan issue after all, just wrong indexing…

Thanks again!

Cheers,
Louis


data {
  int<lower=2> n_occ;
  int<lower=1> n_ind;
  int<lower=1> CH[n_ind,n_occ];
  int<lower=1> AM[n_ind,n_occ];
  int<lower=1> n_age;
  int<lower=1> n_year;
  int<lower=1> n_cluster;
  int<lower=1> year[n_occ];
  int<lower=1> cluster[n_ind];
  int<lower=1> first[n_ind];
  int<lower=1> FR[n_ind];
  int<lower=1> ones[n_ind];
}
parameters {
  real<lower=0,upper=1> s_age[n_age];
  real<lower=0,upper=1> me_age[n_age];
  real<lower=0,upper=1> mi_age[n_age];
  real<lower=0,upper=1> p_age[n_age];
  real<lower=0,upper=1> r_age[n_age];
  // NOTE: latent parameters declared here
  real l_s_age_year[n_age,n_year];
  real l_p_age_year[n_age,n_year];
  real l_r_age_year[n_age,n_year];
  real cluster_s[n_cluster];
  real cluster_p[n_cluster];
  real<lower=0.01,upper=10> sigma_year_s[n_age];
  real<lower=0.01,upper=10> sigma_year_p[n_age];
  real<lower=0.01,upper=10> sigma_year_r[n_age];
  real<lower=0.01,upper=10> sigma_cluster_s;
  real<lower=0.01,upper=10> sigma_cluster_p;
}
transformed parameters {
  // logit transformation
  real l_s_age[n_age];
  real l_p_age[n_age];
  real l_r_age[n_age];
  l_s_age <- logit(s_age);
  l_p_age <- logit(p_age);
  l_r_age <- logit(r_age);
  // derived parameters
  real<lower=0,upper=1> s_age_year[n_age,n_year];
  real<lower=0,upper=1> p_age_year[n_age,n_year];
  real<lower=0,upper=1> r_age_year[n_age,n_year];
  real<lower=0,upper=1> s_age_year_cluster[n_age,n_year,n_cluster];
  real<lower=0,upper=1> p_age_year_cluster[n_age,n_year,n_cluster];
  real<lower=0,upper=1> s_age_cluster[n_age,n_cluster];
  real<lower=0,upper=1> p_age_cluster[n_age,n_cluster];
  for(a in 1:n_age) {
    for(y in 1:n_year) {
      s_age_year[a,y] <- inv_logit(l_s_age_year[a,y]);
      p_age_year[a,y] <- inv_logit(l_p_age_year[a,y]);
      r_age_year[a,y] <- inv_logit(l_r_age_year[a,y]);
      for(c in 1:n_cluster) {
        s_age_year_cluster[a,y,c] <- inv_logit(l_s_age_year[a,y] + cluster_s[c]);
        p_age_year_cluster[a,y,c] <- inv_logit(l_p_age_year[a,y] + cluster_s[c]);
      }
    }
    for(c in 1:n_cluster) {
      s_age_cluster[a,c] <- inv_logit(sum(l_s_age_year[a,1:n_year])/n_year + cluster_s[c]);
      p_age_cluster[a,c] <- inv_logit(sum(l_p_age_year[a,1:n_year])/n_year + cluster_p[c]);
    }
  }
  // linear predictors
  real<lower=0,upper=1> s[n_ind,n_occ-1];
  real<lower=0,upper=1> me[n_ind,n_occ-1];
  real<lower=0,upper=1> mi[n_ind,n_occ-1];
  real<lower=0,upper=1> p[n_ind,n_occ-1];
  real<lower=0,upper=1> r[n_ind,n_occ-1];
  for(i in 1:n_ind) {
    for(t in 1:(first[i]-1)) {
      s[i,t] <- 1;
      me[i,t] <- 1;
      mi[i,t] <- 1;
    }
    for(t in 1:first[i]) {
      p[i,t] <- .5;
      r[i,t] <- .5;
    }
    for(t in first[i]:(n_occ-1)) {
      s[i,t] <- inv_logit(l_s_age_year[AM[i,t],year[t]] + cluster_s[cluster[i]]);
      me[i,t] <- me_age[AM[i,t]];
      mi[i,t] <- mi_age[AM[i,t]];
    }
    for(t in (first[i]+1):(n_occ-1)) {
      p[i,t] <- inv_logit(l_p_age_year[AM[i,t],year[t]] + cluster_p[cluster[i]]);
      r[i,t] <- inv_logit(l_r_age_year[AM[i,t],year[t]]);
    }
  }
  // Define state-transition and observation matrices
  real<lower=0,upper=1> ps[5,n_ind,n_occ-1,5];
  real<lower=0,upper=1> po[5,n_ind,n_occ-1,5];
  for(i in 1:n_ind) {
    for(t in 1:(first[i]-1)) {
      for(ki in 1:5) {
        for(kj in 1:5) {
          ps[ki,i,t,kj] <- 1;
          po[ki,i,t,kj] <- 1;
        }
      }
    }
    for(t in first[i]:(n_occ-1)) {
      // Define probabilities of state S(t+1) given S(t)
      ps[1, i, t, 1] = s[i,t] * (1-me[i,t]);
      ps[1, i, t, 2] = s[i,t] * me[i,t];
      ps[1, i, t, 3] = (1-s[i,t]) * (1-me[i,t]) * r[i,t];
      ps[1, i, t, 4] = (1-s[i,t]) * me[i,t] * r[i,t];
      ps[1, i, t, 5] = (1-s[i,t]) * (1-r[i,t]);
      ps[2, i, t, 1] = s[i,t] * mi[i,t];
      ps[2, i, t, 2] = s[i,t] * (1-mi[i,t]);
      ps[2, i, t, 3] = (1-s[i,t]) * mi[i,t] * r[i,t];
      ps[2, i, t, 4] = (1-s[i,t]) * (1-mi[i,t]) * r[i,t];
      ps[2, i, t, 5] = (1-s[i,t]) * (1-r[i,t]);
      ps[3, i, t, 1] = 0;
      ps[3, i, t, 2] = 0;
      ps[3, i, t, 3] = 0;
      ps[3, i, t, 4] = 0;
      ps[3, i, t, 5] = 1;
      ps[4, i, t, 1] = 0;
      ps[4, i, t, 2] = 0;
      ps[4, i, t, 3] = 0;
      ps[4, i, t, 4] = 0;
      ps[4, i, t, 5] = 1;
      ps[5, i, t, 1] = 0;
      ps[5, i, t, 2] = 0;
      ps[5, i, t, 3] = 0;
      ps[5, i, t, 4] = 0;
      ps[5, i, t, 5] = 1;
      // Define probabilities of O(t) given S(t)
      po[1, i, t, 1] = p[i,t];
      po[1, i, t, 2] = 0;
      po[1, i, t, 3] = 0;
      po[1, i, t, 4] = 0;
      po[1, i, t, 5] = 1 - p[i,t];
      po[2, i, t, 1] = 0;
      po[2, i, t, 2] = p[i,t];
      po[2, i, t, 3] = 0;
      po[2, i, t, 4] = 0;
      po[2, i, t, 5] = 1 - p[i,t];
      po[3, i, t, 1] = 0;
      po[3, i, t, 2] = 0;
      po[3, i, t, 3] = 1;
      po[3, i, t, 4] = 0;
      po[3, i, t, 5] = 0;
      po[4, i, t, 1] = 0;
      po[4, i, t, 2] = 0;
      po[4, i, t, 3] = 0;
      po[4, i, t, 4] = 1;
      po[4, i, t, 5] = 0;
      po[5, i, t, 1] = 0;
      po[5, i, t, 2] = 0;
      po[5, i, t, 3] = 0;
      po[5, i, t, 4] = 0;
      po[5, i, t, 5] = 1;
    }
  }
  // Marginalisation
  real<lower=0,upper=1> zeta[n_ind,n_occ,5];
  for(i in 1:n_ind){
    for(t in 1:(first[i]-1)) {
      for(k in 1:5) {
        zeta[i,t,k] <- 0;
      }
    }
    zeta[i,first[i],1] <- 1;
    zeta[i,first[i],2] <- 0;
    zeta[i,first[i],3] <- 0;
    zeta[i,first[i],4] <- 0;
    zeta[i,first[i],5] <- 0;
    for(t in first[i]:(n_occ-1)) { 
      for(k in 1:5) {  
        zeta[i,(t+1),k] <- dot_product(zeta[i,t,], ps[,i,t,k]) * po[k,i,t,CH[i,(t+1)]];
      }
    }
  }
}
model {
  // Priors
  /// Year random effect
  for(a in 1:n_age) {
    for(y in 1:n_year) {
      l_s_age_year[a,y] ~ normal(l_s_age[a], sigma_year_s[a]);
      l_p_age_year[a,y] ~ normal(l_p_age[a], sigma_year_p[a]);
      l_r_age_year[a,y] ~ normal(l_r_age[a], sigma_year_r[a]);
    }
    sigma_year_s[a] ~ uniform(0.01, 10);
    sigma_year_p[a] ~ uniform(0.01, 10);
    sigma_year_r[a] ~ uniform(0.01, 10);
  }
  /// Cluster random effect
  for(c in 1:n_cluster) {
    cluster_s[c] ~ normal(0, sigma_cluster_s);
    cluster_p[c] ~ normal(0, sigma_cluster_p);
  }
  sigma_cluster_s ~ uniform(0.01, 10);
  sigma_cluster_p ~ uniform(0.01, 10);
  // Likelihood 
  vector[n_ind] lik;
  for(i in 1:n_ind) {
    lik[i] <- sum(zeta[i,n_occ,]);
    ones[i] ~ binomial(FR[i], lik[i]);
  }
}