Hi all,

I’m encountering what seems as if it should be a surmountable error that I have not at all been able to wrap my head around. The general model is a mixture model with two binary states and transitions between these states (all spatially structured), and some zero-inflation observation error. The descriptions of nearest neighbor Gaussian processes and iCAR formulations have been really helpful for describing the initial state, but I’ve had trouble trying to formulate transition parameters as a function of the state at neighboring sites during the previous time-period. From what I’ve been able to isolate, the offending code is this:

```
for (i in 1:ncells) {
psi[i, 1]=inv_logit(a0_psi+dot_product(a_for, BForest[,i])+rho[i]); //a1_psi*Forest[i];
for (t in 2:nyears){
phi[i, t-1]=inv_logit(b0_phi+b1_phi*Forest[i]+b2_phi*mean(psi[NN[i,], t-1]));
gamma[i, t-1]=inv_logit(b0_gam+b1_gam*Forest[i]+b2_gam*mean(psi[NN[i,], t-1]));
psi[i, t]=psi[i, t-1]*phi[i, t-1]+(1-psi[i, t-1])*gamma[i, t-1];
}
}
```

My goal is that phi or gamma (and by extension, psi) vary based upon values of psi in neighboring regions during a previous time step. NN is here a matrix with ncell rows and a number of columns based upon the maximum number of neighbors for any cell. I’ve tried more recursive indexing (mean(psi[NN[i, 1:numn[i]], t-1]), where numn is a vector of the number of neighboring sites, and have fixed psi at non-existent neighbors as 0, and have not been able to get around the exception error that psi[x] is nan, but must be greater than or equal to zero (yes, psi is defined as having limits at [0, 1]). Anyway, happy to share more code, initial values, etc. My sense from looking around is that the segment function could work here, but I haven’t been able to get this to work, either.

Thanks,

John