Dear all Stanians,
Hope this message finds you all well. I am writing to ask a question about using array in Stan code blocks. I am trying to estimate a two-level logit model with intercept and slopes as outcomes. Thus I need to have a multivariate prior for the random components at level-2. But I am running into problems with lines beginning with the array function. I pretty much followed the codes at the following Stan project site for manuals:
And my Stan codes are as follow with slight revision (logit instead of OLS) to the codes in the Stan manual. It appears that it doesnât like this part (Line 5 in the data block): array[N] int<lower=1, upper=J> jj;
Somehow it doesnât recognize array to begin this line. I also tried to turn the jj part to vector. It runs through this line, but when it hits any line that begins with array, the program always gives me error. I tried turning some of the lines beginning with array (e.g., âarray[J] row_vector[L] u;â) to matrix, but it still didnât work. Any pointer will be great appreciated.
Jun Xu, PhD
Professor
Department of Sociology
Data Analytics Program
Ball State University
Muncie, IN 47306
data {
int<lower=0> N; // num individuals
int<lower=1> K; // num ind predictors
int<lower=1> J; // num groups
int<lower=1> L; // num group predictors
array[N] int<lower=1, upper=J> jj; // group for individual
matrix[N, K] x; // individual predictors
array[J] row_vector[L] u; // group predictors
vector[N] y; // outcomes
}
parameters {
corr_matrix[K] Omega; // prior correlation
vector<lower=0>[K] tau; // prior scale
matrix[L, K] gamma; // group coeffs
array[J] vector[K] beta; // indiv coeffs by group
real<lower=0> sigma; // prediction error scale
}
model {
tau ~ cauchy(0, 2.5);
Omega ~ lkj_corr(2);
to_vector(gamma) ~ normal(0, 5);
{
array[J] row_vector[K] u_gamma;
for (j in 1:J) {
u_gamma[j] = u[j] * gamma;
}
beta ~ multi_normal(u_gamma, quad_form_diag(Omega, tau));
}
for (n in 1:N) {
y[n] ~ bernoulli(inv_logit(x[n] * beta[jj[n]]));
}
}
and the error message is
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
error in âmodel2a4410413ab4_60f1e9e0a72498a2488a08151e849344â at line 7, column 2
5: int<lower=1> J; // num groups
6: int<lower=1> L; // num group predictors
7: array[N] int<lower=1, upper=J> jj; // group for individual
^
8: matrix[N, K] x; // individual predictors
PARSER EXPECTED: <one of the following:
a variable declaration, beginning with type,
(int, real, vector, row_vector, matrix, unit_vector,
simplex, ordered, positive_ordered,
corr_matrix, cov_matrix,
cholesky_corr, cholesky_cov
or â}â to close variable declarations>
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model â60f1e9e0a72498a2488a08151e849344â due to the above error.