# PARSER EXPECTED error

**URL:** <https://discourse.mc-stan.org/t/parser-expected-error/979>\
**Category:** Modeling\
**Created:** [June 20, 2017, 7:46pm UTC](https://discourse.mc-stan.org/t/parser-expected-error/979 "2017-06-20T19:46:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nitaimukh](https://avatars.discourse-cdn.com/v4/letter/n/e495f1/32.png) [@nitaimukh](https://discourse.mc-stan.org/u/nitaimukh)\
**Post date:** [June 20, 2017, 7:46pm UTC](https://discourse.mc-stan.org/t/parser-expected-error/979/1 "2017-06-20T19:46:09Z")

</div>

I am writing my first stan program, with Rstan interface. in the model below, if I declare the betavar variable as a local variable in the model block, the code runs, but the values of betavar is not saved. If I declare the betavar in the transformed parameters block, I get an error :

52: n4= abs(l-ll);  
53: sumn = n1+n2+n3+n4;  
54: betavar = sigbeta2\*(gam^sumn);  
^  
55: if (sumn == 0) beta[n1,n2,n3,n4] =0

PARSER EXPECTED:   
Error in stanc(file = file, model\_code = model\_code, model\_name = model\_name, :  
failed to parse Stan model ‘b88c4f5b4184fcf2b7d34fb060c58a97’ due to the above error.

Here is the model part. Any help would be much appreciated.  
Nitai

data {  
int\<lower=1\> xdim;  
int\<lower=1\> ydim;  
int\<lower=1\> zdim;  
int\<lower=1\> tdim;  
real YData[64,64,33,210];  
}  
parameters {  
real beta[6,6,6,6];  
real gam;  
}  
transformed parameters {  
real\<lower=0\> sig2;  
real\<lower=0\> sigbeta2;  
real betavar;  
}

model {  
real Ymean;  
int xliml;  
int yliml;  
int zliml;  
int tliml;  
int xlimu;  
int ylimu;  
int zlimu;  
int tlimu;  
int n1;  
int n2;  
int n3;  
int n4;  
int sumn;

for( i in 1:xdim) {  
for( j in 1:ydim) {  
for( k in 1:zdim) {  
for( l in 1:tdim) {  
Ymean = 0.0;  
xliml =max(1, i-5);xlimu = min(i+5, xdim);  
yliml =max(1, j-5); ylimu = min(j+5, ydim);  
zliml =max(1, k-5); zlimu = min(k+5, zdim);  
tliml =max(1, l-5); tlimu = min(l+5, tdim);  
for(ii in xliml:xlimu){  
for(jj in yliml:ylimu){  
for(kk in zliml:zlimu){  
for(ll in tliml:tlimu){  
n1\<- abs(i-ii);  
n2= abs(j-jj);  
n3= abs(k-kk);  
n4= abs(l-ll);  
sumn = n1+n2+n3+n4;  
betavar = sigbeta2\*(gam^sumn);  
if (sumn == 0) beta[n1,n2,n3,n4] =0  
else  
beta[n1,n2,n3,n4] ~ normal(0.0, betavar);  
Ymean = Ymean + YData[ii,jj,kk,ll]\*beta[n1,n2,n3,n4];  
} } } }

YData[i,j,k,l] ~ normal(Ymean, sig2);

}}}}  
gam ~ uniform(0,1);  
sig2 ~ inv\_gamma(1,1);  
sigbeta2 ~ inv\_gamma(1,1);  
}

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [June 20, 2017, 8:23pm UTC](https://discourse.mc-stan.org/t/parser-expected-error/979/2 "2017-06-20T20:23:33Z")

</div>

You can’t assign to variables defined in the transformed parameters block outside of that block.

For instance, if you had a parameter sigma and wanted also the parameter sigma^2, you could do this:

```stan
parameters {
  real<lower=0.0> sigma;
}
transformed parameters {
  real sigma_squared;

  sigma_squared = sigma^2;
}

```

But you can’t modify sigma\_squared in the model or generated quantities blocks after that.

(the error should be something like: `attempt to assign variable in wrong block. left-hand-side variable origin=transformed parameter`)

In your case you’d need to define a betavar in the transformed data block big enough to hold all the intermediate betavars you compute, fill up that array of betavars, and then you use that in the model block.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [June 21, 2017, 6:11am UTC](https://discourse.mc-stan.org/t/parser-expected-error/979/3 "2017-06-21T06:11:54Z")

</div>

agreed. you should get a better error message - will check this one out.  
cheers,  
Mitzi

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [June 21, 2017, 1:31pm UTC](https://discourse.mc-stan.org/t/parser-expected-error/979/4 "2017-06-21T13:31:41Z")

</div>

@mitzimorris , nono, the error is actually that. I just ran some code with that problem and copy-pasted the error here :D.

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [June 21, 2017, 1:44pm UTC](https://discourse.mc-stan.org/t/parser-expected-error/979/5 "2017-06-21T13:44:51Z")

</div>

right - the parser doesn’t give a proper error message - will check out the parser.  
what version(s) did you try this on?

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [June 21, 2017, 2:27pm UTC](https://discourse.mc-stan.org/t/parser-expected-error/979/6 "2017-06-21T14:27:39Z")

</div>

From R’s sessionInfo():

rstan\_2.15.1  
StanHeaders\_2.15.0-1

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [June 21, 2017, 5:33pm UTC](https://discourse.mc-stan.org/t/parser-expected-error/979/7 "2017-06-21T17:33:34Z")

</div>

continuing to agree with @bbbales2 - ran the model in Rstan -

```
> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X Yosemite 10.10.5
 rstan_2.15.1 
StanHeaders_2.15.0-1 
....

```

and got the error message:

```
ERROR at line 52

 50: n4= abs(l-ll); 
 51: sumn = n1+n2+n3+n4;
 52: betavar = sigbeta2*(gam^sumn);
                                   ^
 53: if (sumn == 0) beta[n1,n2,n3,n4] =0

PARSER EXPECTED: <expression assignable to left-hand side>

```

there are two problems with the line 53:

```
 if (sumn == 0) beta[n1,n2,n3,n4] =0

```

1. need semicolon after both stmts in an if-else construct
2. you can’t assign to parameter `beta` - there is no way to set a parameter, although you can init params via Rstan. so you can’t really do exactly what your trying to do - although a simple `if` stmt condition  
`if (sumn != 0)` or `(sumn > 0)` or something you could still do the sampling in the `then` clause.

if for some reason you’re not seeing the full error message, please send details on Rstan and Stan versions.  
cheers,  
Mitzi
