I am getting the following message when fitting my Stan model (an ecological metapopulation dynamics model) that suggests there is an issue with an object tmp_proj
in my generated quantities block:
The following variables have undefined values: tmp_proj[1,2,2],The following variables have undefined values: tmp_proj[2,2,2],The following variables have undefined values: tmp_proj[3,2,2],The following variables have undefined values: tmp_proj[4,2,2]
… And so on. I am filling in this object with a lot of nested conditional statements, and I suspect I just made a mistake somewhere, but for the life of me I can’t find it! Here’s the part of the generated quantities block where I create it (I can post the whole block, but it’s pretty long). Can anyone spot a mistake? It’s defined as real tmp_proj[np,n_ages, ny_proj];
. Based on the message above, I think the issue starts after the if(a==1){}
condition (because the first issue happens for tmp_proj[1,2,2]
which is patch 1, age 2, and year 2).
d
, sigma_r
, mean_recruits
, rec_dev_proj
, T_adjust_proj
, proj_init
, and z
are all estimated or defined earlier. np
, ny_proj
, and n_ages
are the dimensions.
for(p in 1:np){
for(y in 1:ny_proj){
if(y==1){
// initiate projection with fixed observation
for(a in 1:n_ages){
tmp_proj[p,a, 1] = proj_init[p,a];
}
} else { // add case for all other years
for(a in 1:n_ages){
// project age 1
if(a==1){
tmp_proj[p,1, y] = mean_recruits * exp(rec_dev_proj[y-1] - pow(sigma_r,2)/2) * T_adjust_proj[p,y];
// these values are all estimated or defined earlier, and I know this part is working at least
}
// project non-reproductive ages
else if(a < age_at_maturity){
tmp_proj[p,a,y] = tmp_proj[p,a-1,y-1] * (1-z);// these just grow and die in the patch
}
// project reproductive ages
else{
if(p==1){
tmp_proj[p,a,y] = tmp_proj[p, a-1, y-1] * (1-z) * (1-d) + tmp_proj[p+1, a-1, y-1] * (1-z) * d;
} // close patch 1 case
else if(p==np){
tmp_proj[p,a,y] = tmp_proj[p, a-1, y-1] * (1-z) * (1-d) + tmp_proj[p-1, a-1, y-1] * (1-z) * d;
} // close highest patch
else{
tmp_proj[p,a,y] = tmp_proj[p, a-1, y-1] * (1-z) * 2*(1-d) + tmp_proj[p-1, a-1, y-1] * (1-z) * d + tmp_proj[p+1, a-1, y-1] * (1-z) * d;
} // close if/else for main patches
} // close else for reproductive age group
} // close age loop
} // close else for all years except the initial one
} // close year loop
} // close patch loop