Failed to parse/fit Stan model error rstan

Hi, I’m new to Stan and just finished figuring out how to get rstan working on my Macbook M1 Pro ARM computer with Monterey (downloaded latest versions of r and rstudio). I tried running the first example “Eight Schools” from here: RStan Getting Started · stan-dev/rstan Wiki · GitHub and I get an error that Stan failed to parse. Could anyone help me try to figure out what’s going on? I wasn’t sure if there was something wrong with my rstan installation on my mac as it was quite a challening process, but I actually also tried this on a windows laptop I have, and got the same error. Here’s the code and error:


fit ← stan(file = ‘schools.stan’, data = schools_dat)


data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // standard error of effect estimates
parameters {
real mu; // population treatment effect
real<lower=0> tau; // standard deviation in treatment effects
vector[J] eta; // unscaled deviation from mu by school
transformed parameters {
vector[J] theta = mu + tau * eta; // school treatment effects
model {
target += normal_lpdf(eta | 0, 1); // prior log-density
target += normal_lpdf(y | theta, sigma); // log-likelihood

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

The stan model file is correct. You can help us diagnose by posting the complete script so the error can be reproduced.

Welcome to Stan.

Thank you! Sorry for the late reply–this is the script:


schools_dat ← list(J = 8,
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18))

schools_model ← stan(file = “/Users/jordanrogan/Dropbox/Mac/Documents/R/schools.stan”, ##where the stan script is saved
data = schools_dat)

pairs(fit, pars = c(“mu”, “tau”, “lp__”))

la ← extract(fit, permuted = TRUE) # return a list of arrays
mu ← la$mu

return an array of three dimensions: iterations, chains, parameters

a ← extract(fit, permuted = FALSE)

use S3 functions on stanfit objects

a2 ← as.array(fit)
m ← as.matrix(fit)
d ←

This is unfortunately a known issue with rstan 2.26, try restarting your R session. More information in this post: Error: Expected end of file after end of generated quantities block - #5 by andrjohns

Thanks for your response. Unfortunately, restarting my r session did not fix the problem. I don’t know enough about coding to understand what would need to be done in the solution provided within another post from the thread you posted. If anyone has a means of explaining the solution, or another possible workaround (if available) I would appreciate it, thank you!

What I will say is I have run other code in stan successfully in a recent prstatistics course I took–so I’m not sure why this stan modile file/code in particular is giving me issues.