If-else statement inside parameter block?

Hi Everyone,

Is it possible to use conditional statements inside of parameter block?

I have several parameters in my model and I want to test how different combinations affect the final posterior. The idea is for me to be able to choose which parameters to run from data passed to the the stan file from R and using that to evaluate conditional statements inside the parameter block, model block and the generated quantities block.

will this be possible?

not possible.
the parameter block only allows variable declarations; no conditional statements or expressions allowed.

see 8.5 Program Block: parameters | Stan Reference Manual

Variables declared as parameters cannot be directly assigned values. So there is no block of statements in the parameters program block.

What you want to do is sort of possible if you make parameter vectors conditionally of size zero like

data {
  int<lower = 0, upper = 1> include_theta;
  int<lower = 1> K;
}
parameters {
  vector[include_theta ? K : 0] theta;
}

We do that a lot in rstanarm: see for example

4 Likes

Hi @bgoodri

Thanks for the tip!

Hi @bgoodri is there a way to do something similar using cmdstanr? It doesn’t seem to initialise if I use zero length arrays. Thank you!!