If else statements with array of integers passed to data

rstan version 2.21.2

I’m working on beast (maybe it wouldn’t be if I was better programmer?) of a model that involves serial dilutions with data that is sometimes censored. I would like to model each dilution separately based on conditions that I pass to data. For example in the model block:

  for (n in 1:N1) {

    if (dilution_plate==1) {
      if (censored==0) {
target += poisson_lpmf( y[n] | 0.1 * lambda[J_1[n]] );
      }
      else if (censored==1) {
target += poisson_lccdf(U | lambda[J_1[n]]);
      }      
    }

    else if (dilution_plate==2) {
      if (censored==0) {
target += poisson_lpmf( y[n] | 0.01 * lambda[J_1[n]] + error_1[J_1[n]] );
      }
      else if (censored==1) {
target += poisson_lccdf(U | 0.01 * lambda[J_1[n]] + error_1[J_1[n]] );
      }
    }
//.....continue on for 4 more of these nested conditional statements + 4 others with different data but you get the idea
  }

where dilution_plate is an array of integers that is an index for the dilution plate and censored is a dummy variable indicating censoring. In other words, each row of data will have a value for y, dilution_plate, and censored.
I would like to include these nested conditional statements in both the model and generated quantities block.
However, the above throws the following error:


I tried removing == and replacing with = and that did not help. I tried declaring dilution_plate and censored as vectors and replacing 1 with 1.0 and 2 with 2.0, etc. That did not help. I also tried adding if (dilution_plate[n] = 1) and it throws a different error. I thought adding [n] would do the trick since it’s inside a for loop, but nope.

I’m sure I’m missing something obvious. I did read the manual on conditional statements… is there no way to pass a vector or array to data and for each iteration in a for loop evaluate the condition?

(for now I am using for loops and incrementing the target just to keep everything explicit as to what I am doing, so currently not looking for speed gains)

Thanks!

dilution_plate[n] is a step in the right direction. What’s the new error? Just to be explicit, did you try dilution_plate[n] == 1? If so, what’s the error then?

1 Like

ahh dang, I forgot to add == back in when I tried dilution_plate[n]. Doing dilution_plate[n] == 1 does the trick. thanks for looking!!