Failed to Parse Stan Model, no line information

Short summary of the problem

I am trying to run a multilevel model (Bayesian hierarchical model with non-informative priors) in rstan and have used previously successful code as an example for the creation of this model, but keep getting the following error:

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

There is no line information or other information to help me solve the issue.

The data for this model is not currently publically available, but a simulation of the data is available in the next chunk.

The code I am using is below:

BhLm_Code <- "
  int<lower=0> n; // number of data points
  int<lower=0> ngr1; // number of groups in group 1
  real y[n]; // withdrawal values
  real x[n]; // year
  int<lower=0, upper=ngr1> group1[n]; // grouping IDs for 1st grouping

  real alpha; // intercept
  real beta; // slope

  vector[ngr1] alpha_g1; // group-level influence on alpha for group 1
  vector[ngr1] beta_g1; // group-level influence on beta for group 1

  real<lower=0,upper=100> sigmay;

  real<lower=0> sigmaa1;
  real<lower=0> sigmab1;

transformed parameters{
vector[n] mu;

  for (i in 1:n){
    mu[i]=(alpha_g1[group1[i]]) + ((beta_g1[group1[i]]) * (x[i])));

      alpha_g1~normal(alpha, sigmaa1);
      beta_g1~normal(beta, sigmab1);
     y[i]~normal(mu[i], sigmay);


y <- infile[,response]
temp <- !
##getting rows with a non n/a response

infile <- infile[temp,]
##removing n/a rows from dataframe

y <- y[temp]
##getting responses from updated dataframe

x <- infile[,Year]
##getting year/predictor from updated dataframe

group1O <- ordered(Group1o)
group1 <- as.numeric(group1O)
##getting numeric list for group 1
n <- length(y)
n.gr1 <- max(group1)

BhLm_data <- list(n=n, ngr1=n.gr1, y=y, x=x, group1=group1)

BhLm_inits <- list()
for (i in 1:4)
BhLm_inits[[i]]<- list(alpha=rnorm(1), beta=runif(1), alpha_g1=rep(0, n.gr1),
                       beta_g1=rep(0, n.gr1),
                       sigmay=runif(1, 0, 3), sigmaa1=runif(1), sigmab1=runif(1) )

BhLm_pars <- c("alpha", "beta", "alpha_g1", "beta_g1", "sigmay", "sigmaa1","sigmab1")

fit_b <- stan(model_code = BhLm_Code, data = BhLm_data, iter = 1, chains = 4, init = BhLm_inits, pars = BhLm_pars, control = list(adapt_delta = 0.91, max_treedepth=12))

A simulation of the dataset for this code can be made with:

ic_site_id<-c(rep(1, 20), rep(2,20), rep(3,20), rep(4,20), rep(5,20))
Year<-rep(1991:2010, 5)
With_AQU<-data.frame(Year=Year, ic_site_id=ic_site_id)

Thank you for any and all help!


When compiling your code with cmdstanr, I get the following error:

Syntax error in '/var/folders/1d/rjvd0h_n0yq20j13h4gdfytw0000gn/T/RtmpcA3ooU/model-94bb1d91e6cb.stan', line 27, column 65 to column 66, parsing error:
    26:    for (i in 1:n){
    27:      mu[i]=(alpha_g1[group1[i]]) + ((beta_g1[group1[i]]) * (x[i])));
    28:  }
    29:  }

Ill-formed phrase. Found L-value "=" expression. There are many ways in which this can be completed to a valid phrase.

Which is happening because you have extra unmatched parentheses at the end of the line

Thanks! I haven’t used cmdstanr before. Small syntax errors are so easy to miss regardless of how many times you look at them. I appreciate your help and will be looking into cmdstanr too.