A general question about variable declarations in model block

I’m still feeling uncomfortable about which block variable declarations should be put. For example, I want to create a vector to do element-wise operation in a later loop:

vector[N] Z = A*B

where A is a matrix containing data and parameter and B is a vector containing data. Then I use Z to do element-wise operations in a loop, e.g.

for (i in 1:N){
    D[i] = (Y[i] <= Z[i] ?  1:0);
}

Then I received a message that “Variable “vector” does not exist”.
So I wonder where should I put the declaration?

Can you paste your model? Nothing is wrong with how you did it here, so something must be wrong elsewhere.

what interface are you using and what version?

I don’t know what version of RStan started using the new compiler - the old compiler required all variable declarations to come at the beginning of a block - this rather looks like the error message that would have been generated if that were the case.

also note: the variables in the model block are local variables and are not written as part of the output.

1 Like

Thanks for pointing it out! After moving those declarations on the top of model block, the issue resolved.

1 Like