I’m quite new to Stan and Bayesian statistics in general.

For practice I am trying to make a simple intercept-only model for my data which looks like this :

model{
...
for (i in 1:N) {
mu[i] = a + a_an[idx_an[i]] + a_st[idx_st[i]] + a_gr[idx_gr[i]];
}
Area_s ~ normal(mu, sigma);
}

where N is the number of data points and a_xx are intercepts for random effects and the Area_s the outcome variable.

However, I want to use a truncated normal likelihood because the data can not be lower than 0. Now if I code the model like this:

model{
...
for (i in 1:N) {
mu[i] = a + a_an[idx_an[i]] + a_st[idx_st[i]] + a_gr[idx_gr[i]];
}
// vectorized
Area_s ~ normal(mu, sigma) T[0,]; // truncated normal
}

the compiler throws an error.

Outcomes in truncated distributions must be univariate.
Found outcome expression: Area_s
with non-univariate type: real[]

However when I place the likelihood in a for loop (unvectorized), the code compiles fine and the model samples well:

model{
...
for (i in 1:N) {
mu[i] = a + a_an[idx_an[i]] + a_st[idx_st[i]] + a_gr[idx_gr[i]];
}
for (i in 1:N) {
// unvectorized
Area_s[i] ~ normal(mu[i], sigma) T[0,]; // truncated normal
}
}

In the Stan manual I found that the vectorized version of the likelihood is more efficient. Now I want to know if there is a vectorized version for a truncated likelihood available.

Hum quickly, I do not see anything problematic in your code… I used this formulation for the truncated normal yesterday, but I did not tried to vectorized it and used a loop for other purposes…

We will have to wait for more pertinent people to answer!

At the page 595 of the manual (2.17.0), we can read :

Most distributions have been vectorized, but currently the truncated versions may not exist and may not be vectorized.

I think you will have to accept the loop :) Fortunatly, using loops in stan, which depends upon a low level progamming language, is far less painfull than using loops in R!