# Rstan parser error

**URL:** <https://discourse.mc-stan.org/t/rstan-parser-error/28673>\
**Category:** General\
**Created:** [August 29, 2022, 5:47pm UTC](https://discourse.mc-stan.org/t/rstan-parser-error/28673 "2022-08-29T17:47:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![stan\_beginer](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@stan\_beginer](https://discourse.mc-stan.org/u/stan_beginer)\
**Post date:** [August 29, 2022, 5:47pm UTC](https://discourse.mc-stan.org/t/rstan-parser-error/28673/1 "2022-08-29T17:47:25Z")

</div>

Hi,

Recently when I tried to run one Stan code, the following error messages showed up:

Error in stanc(file = file, model\_code = model\_code, model\_name = model\_name, :  
parser failed badly

May I ask about the possible reasons for that? ( I have tested the code before but no error happened in the past)

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [August 29, 2022, 8:17pm UTC](https://discourse.mc-stan.org/t/rstan-parser-error/28673/2 "2022-08-29T20:17:45Z")

</div>

Hi -

Are you able to share the Stan code which caused this error?

---

<div class="post-metadata">

**Author:** ![eem](https://avatars.discourse-cdn.com/v4/letter/e/ecd19e/32.png) [@eem](https://discourse.mc-stan.org/u/eem)\
**Post date:** [November 16, 2022, 5:15pm UTC](https://discourse.mc-stan.org/t/rstan-parser-error/28673/3 "2022-11-16T17:15:10Z")

</div>

Hello,

I am also experiencing the same problem. My script was working fine until suddenly I started receiving the same error. Any Idea on how to solve the problem?

Error in stanc(file = file, model\_code = model\_code, model\_name = model\_name, :  
parser failed badly

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [November 16, 2022, 5:21pm UTC](https://discourse.mc-stan.org/t/rstan-parser-error/28673/4 "2022-11-16T17:21:49Z")

</div>

> [@eem](#):
>
> Any Idea on how to solve the problem?

Are you able to share the Stan code you are using?

---

<div class="post-metadata">

**Author:** ![eem](https://avatars.discourse-cdn.com/v4/letter/e/ecd19e/32.png) [@eem](https://discourse.mc-stan.org/u/eem)\
**Post date:** [November 16, 2022, 5:27pm UTC](https://discourse.mc-stan.org/t/rstan-parser-error/28673/5 "2022-11-16T17:27:26Z")

</div>

stancode:  
data {  
int\<lower=2\> K;  
int\<lower=0\> N;  
int\<lower=1\> D;  
int\<lower=1,upper=K\> y[N];  
row\_vector[D] x[N];  
}

parameters {  
vector[D] beta;  
ordered[K-1] c;  
}

model {  
beta ~ normal(0,10);  
c ~ normal(0,10);  
for (n in 1:N)  
y[n] ~ ordered\_logistic(x[n] \* beta, c); //only intercept parameters  
}

generated quantities{  
vector[N] log\_lik; //log-likelihood values

for (i in 1:N)  
log\_lik[i] = ordered\_logistic\_lpmf(y[i] | x[i] \* beta, c);

```
#int<lower=1,upper=K> y_pred[N];

#for (i in 1:N)
  #y_pred[i] = ordered_logistic_rng(x[i] * beta, c); 

```

}

r code  
K = length(unique(ytr))  
N = length(ytr)  
x\_data = as.data.frame(tr[,c(-1,-5)])  
x = model.matrix(~ ., x\_data) #design matrix  
D=ncol(x)  
library(rstan) #to fit models with Stan  
my\_data = list( K = K, N = N, y = ytr, x = x,D=D) # data to be passed to Stan

fit1 = stan(file = ‘Trolley22.stan’, data = my\_data) #fit the model

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [November 16, 2022, 6:01pm UTC](https://discourse.mc-stan.org/t/rstan-parser-error/28673/6 "2022-11-16T18:01:47Z")

</div>

> [@eem](#):
>
> `#`

Rstan has a known bug to do with the `#` character, if you replace all comments with the `//` style, does the problem still occur?

---

<div class="post-metadata">

**Author:** ![eem](https://avatars.discourse-cdn.com/v4/letter/e/ecd19e/32.png) [@eem](https://discourse.mc-stan.org/u/eem)\
**Post date:** [November 16, 2022, 6:42pm UTC](https://discourse.mc-stan.org/t/rstan-parser-error/28673/7 "2022-11-16T18:42:15Z")

</div>

Thanks, It worked
