For within a vector of indexes

Hello all,

I would like to declare the distribution only for a few observations, something like

for (i in myindexes) {
y[i] ~ normal( normal(mu,sigma2)
}

where myindexes is a vector of integers (e.g. myindexes=c(8,20,33,45,60,55))

“Error: Loop must be over container or range.”

I declared myindexes as “int myindexes;”

does anybody know how to declare myindexes (it’s in the data list) so that it does what I mean?

Thanks.

Welcome to the forums!

You need to declare it as an array of integers. If you pass it in as data, it would like:

data {
  int myindexes[6];
}

If you wanted to declare it in the model, it would look like:

transformed data {
  int myindexes[6] = {8,20,33,45,60,55};
}

Also, when it comes to running your model, you don’t need to loop over the indexes in the array. Instead, you can just pass the entire array:

y[myindexes] ~ normal(mu, sigma);

Also notice above that I specified sigma, and not sigma2. This is because Stan parameterises the normal distribution using the standard deviation, not the variance.

Thanks, if I have already the myindexes in the data list, I don’t need the “transformation data” command, right?

Also, The command " ```
y[myindexes] ~ normal(mu, sigma);


cheers

Yep, if the array is already passed as data then you don’t need the transformed data section