I am trying to create a hybrid model with some parameters only applicable to certain model while I want to put everything in one single stan file.
For example

data{
int N;
vector[N] x;
vector[N] y;
int use_beta;
}
parameters {
real alpha;
real beta;
real<lower=0> sigma;
}
model {
if (use_beta)
mu = alpha+beta*x;
else
mu = alpha;
y ~ normal(mu, sigma);
}

So when I call my stan model with use_beta = FALSE, I will exclude “beta” from my pars input. By doing so, would I safely avoid any divergence problem of “beta” ? or do I need to explicitly define beta as a size 0 real when use_beta is FALSE as well?

Also, I notice while calling optimizing, we cannot input pars. In that case, will the algo trying to optimize beta and hence causing problem in the case when use_beta is FALSE?

If you do 1) you should still have a prior for the case when use_beta is true. Not using priors is sometimes OK, but having them is definitely preferable and sometimes crucial. The -Inf, Inf is not really a prior (sometimes called improper prior) as it does not integrate to 1. Improper priors often fail to regularize the posterior in a useful way.

Hope I am making sense and best of luck with your model :-)