Hi - new stan
user here.
I’m trying to build a hierarchical linear regression model similar to the one described in section 1.13 of the Stan manual.
Unlike the multivar regression example presented there, my model does not have group-level predictors. Instead, I have a mean \mu for the regression coefficients \beta. In math-speak:
// adapted from
// https://mc-stan.org/docs/2_23/stan-users-guide/multivariate-hierarchical-priors-section.html
data {
int<lower=0> N; // num samples
int<lower=1> K; // num predictors
int<lower=1> J; // num groups (stations)
int<lower=1,upper=J> jj[N]; // group for individual samples
matrix[N, K] x; // individual predictors
vector[N] y; // outcomes
}
parameters {
matrix[J, K] mu;
matrix[K, J] z;
cholesky_factor_corr[K] L_Omega;
vector<lower=0,upper=pi()/2>[K] tau_unif;
real<lower=0> sigma; // prediction error scale
}
transformed parameters {
matrix[J, K] beta;
vector<lower=0>[K] tau; // prior scale
for (k in 1:K) tau[k] = 2.5 * tan(tau_unif[k]);
beta = mu + (diag_pre_multiply(tau, L_Omega) * z)';
}
model {
sigma ~ cauchy(0, 2.5);
to_vector(z) ~ std_normal();
L_Omega ~ lkj_corr_cholesky(3);
to_vector(mu) ~ normal(0, 2.);
y ~ normal(rows_dot_product(beta[jj], x), sigma);
}
The data for the predictors and target look OK - I’ve cleaned these up and standardized everything before inference:
count mean std min 25% 50% 75% max
PREDICTOR1 7300.0 6.793297e-09 1.000068 -4.911924 -0.649995 -0.061916 0.573672 5.153406
PREDICTOR2 7300.0 1.254147e-08 1.000068 -4.760993 -0.647560 -0.059847 0.578347 4.476428
(...etc...)
At runtime, I get a bunch of
Rejecting initial value:
Log probability evaluates to log(0), i.e. negative infinity.
Stan can't start sampling from this initial value.
Initialization between (-2, 2) failed after 100 attempts.
Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
The model eventually crashes with:
RuntimeError: Initialization failed.
Clearly my model is mis-specified somehow, but how exactly? And what exactly is initialized between (-2,2)
? Thank you!
Full disclosure: I’m using pystan 2.19.1.1
.