Expression ill formed help

Hi again, I am getting an error message from the code below that the expression is ill-formed. I realize that this is due probably to some mismatch between a real and a vector, but I admit I am stumped by it. The error message and code are below. If someone could point me to where I can learn more about how to work with these variable/parameter types for typical regression models, it would be most helpful. Thanks


Expression is ill formed.
error in ‘modele3a3784cab5_bea2581e0976fcdbf559189af41e6a59’ at line 36, column 45

35:      alpha[g] ~ normal(mu_alpha[g], sigma_alpha);
36:      mu_alpha[g] = gamma00+gamma01*ACBG03A[g];
37:      beta1[g] ~ normal(mu_beta1, sigma_beta1);

Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model ‘bea2581e0976fcdbf559189af41e6a59’ due to the above error.

The code is here:

modelString = "
data {
int<lower=0> n; // number of students
int<lower=0> G; // number of schools
int<lower=1,upper=G> schid[n]; // school indices

vector[n] ASRREA01;                   // reading outcome variable
vector[n] ASBG04;
vector[G] ACBG03A;


parameters {
real gamma00[G];
real gamma01[G];
real alpha[G];
real beta1[G];
real mu_beta1[G];
real sigma_read[n];
real sigma_alpha[G];
real sigma_beta1[G];

model {

vector[n] mu;
vector[G] mu_alpha;
for (i in 1:n) {
ASRREA01[i] ~ normal(mu[i], sigma_read);
mu[i] = alpha[schid[i]] + beta1[schid[i]]*ASBG04[i];

for (g in 1: G) {

 alpha[g] ~ normal(mu_alpha[g], sigma_alpha);
 mu_alpha[g] = gamma00+gamma01*ACBG03A[g];
 beta1[g] ~ normal(mu_beta1, sigma_beta1);


// Priors
gamma00 ~ normal(300,100);
gamma01 ~ normal(11, 1);
mu_beta1 ~ normal(20, 2);
sigma_read ~ cauchy(1,5);
sigma_alpha ~ cauchy(1,5);
sigma_beta1 ~ cauchy(1,5);


Hi David,

This is because you’ve declared gamma00 and gamma01 as arrays of size G (i.e., there’s a different one for each of the G schools):

real gamma00[G];
real gamma01[G];

Which means that the expression for mu_alpha:

mu_alpha[g] = gamma00+gamma01*ACBG03A[g];

Resolves to:

real = array + array*real

Did you have issues with the syntax I posted over in this thread?

For more information about constructing different types of regression models (both single-level and hierarchical), you can also see this section of the Stan User’s guide