Dear Stan Users and Experts,
I am trying to estimate a simple linear two-level with varying intercepts and slopes models with a covariance matrix for random effects (u_{0g}, u_{1g}) , or they are correlated.
y_{ig} = \beta_{0g} + x_{1ig}\beta_{1g}+\varepsilon_{ig}
\beta_{0g}=\gamma_{00}+u_{0g}
\beta_{1g}=\gamma_{01}+u_{1g}
data {
int N; // number of observations
int P; // number of groups
int K; // number of predictors + intercept
int prov[N]; // group vector
matrix[N, K] x; // matrix of predictors
real y[N]; // y vector
}
parameters {
vector[K] betaP[P]; // intercept and slope coefficients by group
vector<lower=0>[K] sigmaP; // sd for intercept and slope
vector[K] beta; // intercept and slope hyper-priors
corr_matrix[K] Omega; // correlation matrix
real<lower=0> sigma; // population sigma
}
model {
vector[N] mu;
// priors
beta ~ normal(0, 1);
Omega ~ lkj_corr(2);
sigmaP ~ exponential(1);
sigma ~ exponential(1);
betaP ~ multi_normal(beta, quad_form_diag(Omega, sigmaP));
// likelihood
for(i in 1:N) {
mu[i] = x[i] * (betaP[prov[i]]); // * is matrix multiplication in this context
}
y ~ normal(mu, sigma);
}
I closely followed the codes on the following blog,
which was suggested on Andrew’s blog,
https://statmodeling.stat.columbia.edu/2024/02/27/tutorial-on-varying-intercept-varying-slope-multilevel-models-in-stan-from-will-hipson/
I feel that I only made cosmetic changes to the original codes to suit my model and data naming preference. I estimated the same model using lmer and stan_glmer without any problem. So there shouldn’t be issues with the data structure. But I am getting the following error message,
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 1).
Chain 1: Unrecoverable error evaluating the log probability at the initial value.
Chain 1: Exception: multiply: Columns of rv (2) and Rows of v (7305519684788891252) must match in size (in 'string', line 29, column 4 to column 36)
[1] "Error : Exception: multiply: Columns of rv (2) and Rows of v (7305519684788891252) must match in size (in 'string', line 29, column 4 to column 36)"
[1] "error occurred during calling the sampler; sampling not done"
Anyone knows how to remedy this issue. I suspect it might be related to how some of the data/parameter vector/matrix is declared. Any help would be very much appreciated. What I am trying to do is to present a simple HLM model using the LKJ prior for my students.
Jun Xu, PhD
Professor
Department of Sociology
Data Science & Analytics Program
Ball State University
Web: Jun Xu