Hi,
I am a R user and most of time I do vector/matrix computation in R, there is something called ‘broadcasting’ happening to make the dimension match well (eg, one number + one vector). I noticed Stan has similar topics in user manula but I am still confused about it. For example in the following example:
vector[n] logit_p = beta[1] + beta[2]*x + random_intercept1[indicator] + x.* random_intercept1[indicator];
int<lower = 0> y = bernoulli_logit_rng(logit_p);
Here beta is a vector of length two, x is a vector of length n, indicator is a vector of length n and random_intercept1[indicator] is a corresponding scaler (eg, random_intercept1 is a vector of length four, and each element of indicator is a number from 1-4 to choose the corresponding random intercept). The above expression works well since Stan will match the dimension correctly for me. However, if I rewrite the code in the following way, it will not work:
vector[n] logit_p;
vector[n] y;
logit_p = beta[1] + beta[2]*x + random_intercept1[indicator] + x.* random_intercept1[indicator]];
y = bernoulli_logit_rng(logit_p);
So I have to use the for loop to go through each position of vector y:
vector[n] logit_p;
vector[n] y;
logit_p = beta[1] + beta[2]*x + random_intercept1[indicator] + x.* random_intercept1[indicator]];
for (i in 1:n){
y[i] = bernoulli_logit_rng(logit_p[i]);
}
I don’t know why the above situation happens and I have encountered similar problems multiple times, so once encountering the problem I have to use the for loop to define vectors in Stan, which is quite confusing. May I ask that why this happens and is there any solution for that?
Thanks!