Hi am after some advice. I often find that when setting up a model I want to fix a parameters during model exploration. An example of this is would be exploring if incorporating auto correlation in a Covariance matrix is better than the independence assumption \rho = 0 vs \rho \neq 0. I have used three methods

move the parameter declaration from the parameter section to the data section and recompile. However this ends up creating many models and messy directory.

have data values for bounds of a parameter, and set the upper and lower bound to the value you want to fix the parameter at.

Have a ridiculous prior that basically constrains a parameter to a fixed value

I worry that 2 & 3 will effect sampling of the posterior. Are there better implementation than these? Apologies if I have missed an obvious solution.

I think you can use zero size parameter arrays to do this. It can be a bit awkward, but check this as a model to optionally estimate a standard deviation:

data {
int N;
real y[N];
int<lower = 0, upper = 1> include_param; // 1 for include, 0 for not
real sigma_fixed;
}
params {
real mu;
real<lower = 0.0> sigma[include_param];
}
model {
mu ~ normal(0, 1);
if(include_param == 0) {
y ~ normal(mu, sigma_fixed);
} else {
sigma ~ normal(0, 1);
y ~ normal(mu, sigma);
}
}

Hey! You can also use #include and then let the various models share the same data{...} declaration You can actually use #includes everywhere in your model to keep parts consistent across models. Maybe thatâ€™ll help with the â€śmessâ€ť. I tend to treat each approach as the same model (so basically what you mentioned in 1.). You can obviously combine this with Benâ€™s suggestion. :)

Can I get some detail about how to implement the includes solution? I am only familiar with using the #include for including additional functions. My latest attempt was the one below, but no cigar

//sigma.stan
real<lower = 0.0> sigma;

//model.stan
data {
int N;
real y[N];
//#include sigma.stan
}
parameters {
real mu;
#include sigma.stan
}
model {
mu ~ normal(0, 1);
sigma ~ normal(0, 1);
y ~ normal(mu, sigma);
}