Error message point to wrong syntax

Dear all,
My apologies for sending so many messages. I’m relatively new to Stan and I’m encountering errors that I am not sure how to handle. I’m running a simple one way anova with a hierarchical structure. The code is here

title: “rstan one way random effects ANOVA example”
subtitle: “EP 965”
author: “David Kaplan”
date: “8/29/2020”
html_document: default
pdf_document: default

knitr::opts_chunk$set(echo = TRUE)

Read in data

Canada <-read.csv("~/Desktop/Canada.csv",header=TRUE) 
Canada1 <- na.omit(Canada)
Canada1$schid <- as.numeric(as.factor(Canada1$IDSCHOOL))  
Canada1 <- subset(Canada1, select=c(ASRREA01,schid))
data.list <- with(Canada1, list(ASRREA01=ASRREA01,schid=schid, G = length(unique(Canada1$schid)), n = nrow(Canada1)))

Begin Stan Code. Notice that Stan uses // for comments

modelstring = "

// Comparison of G groups with common variance and
// hierarchical prior for the mean
data {
  int<lower=0> n;                   // number of observations
  int<lower=0> G;                   // number of groups
  int<lower=1,upper=G> schid[n];    // discrete group indicators
  vector[n] ASSREA01;               // real valued observations
parameters {
  real mu0;             // prior mean
  real<lower=0> sigma0; // prior std constrained to be positive
  vector[G] mu;         // group means
  real<lower=0> sigma;  // common std constrained to be positive
model {
  mu0 ~ normal(10, 10);             // weakly informative prior
  sigma0 ~ normal(0, 10);           // weakly informative prior
  mu ~ normal(mu0, sigma0);         // population prior with unknown parameters
  sigma ~ lognormal(0, .5);         // weakly informative prior
  ASRREA01 ~ normal(mu[schid], sigma);  // observation model / likelihood

Start estimation

nChains = 1
nIter= 10000
thinSteps = 1
burnInSteps = floor(nIter/2)

onewayAnova = stan(data=data.list,model_code=modelString,chains=nChains,

Stan plots

stan_plot(onewayAnova, pars=c("mu0","sigma0", "mu","sigma"))

Stan output


I get the following error

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

When I then look at what it is telling me, I see the following

32:   for (g in 1: G) {
33:      mu_alpha[g] = gamma00 + gamma01*ACBG03A[g];
34:      alpha[g] ~ normal(mu_alpha, sigma_alpha);

Notice that this code does not appear in my syntax at all. It’s as though it is picking up a problem with another program I’m running. Seems weird to me.

Thanks for your assistance,


Is it the capitalization difference? Do you have a variable modelString defined somewhere that is this other model?

That did it. Thanks!

1 Like