# Expression ill formed help

**URL:** <https://discourse.mc-stan.org/t/expression-ill-formed-help/17788>\
**Category:** Modeling\
**Created:** [September 2, 2020, 1:05am UTC](https://discourse.mc-stan.org/t/expression-ill-formed-help/17788 "2020-09-02T01:05:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkaplan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dkaplan/32/13835_2.png) [@dkaplan](https://discourse.mc-stan.org/u/dkaplan)\
**Post date:** [September 2, 2020, 1:05am UTC](https://discourse.mc-stan.org/t/expression-ill-formed-help/17788/1 "2020-09-02T01:05:24Z")

</div>

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

David

## Expression is ill formed. error in ‘modele3a3784cab5\_bea2581e0976fcdbf559189af41e6a59’ at line 36, column 45

```
34:   
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);

}  
"

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [September 2, 2020, 1:55am UTC](https://discourse.mc-stan.org/t/expression-ill-formed-help/17788/2 "2020-09-02T01:55:40Z")

</div>

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?](https://discourse.mc-stan.org/t/error-evaluating-the-log-probability-at-the-initial-value/17752/6)

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [September 2, 2020, 2:00am UTC](https://discourse.mc-stan.org/t/expression-ill-formed-help/17788/3 "2020-09-02T02:00:22Z")

</div>

For more information about constructing different types of regression models (both single-level and hierarchical), you can also see [this section](https://mc-stan.org/docs/2_24/stan-users-guide/regression-models.html) of the Stan User’s guide
