Exception: variable does not exist

I am trying Stan for the first time. I specified a model but received the error:
Error in new_CppObject_xp(fields$.module, fields$.pointer, …) :
Exception: variable does not exist; processing stage=data initialization;

I looked at the similar questions and couldn’t get it to work. I decided to try a very simple comparison using the schools example on the stan page. The first method (fit1) works but the second (fit2) reproduces the error in my code. The associated .stan file is copied directly from the stan website. I am running RStudio on Google cloud in Linux (my poor laptop has another model running).

library("rstan") # observe startup messages
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))

# Compile the model
compiled_model <- stan_model("schools.stan")

fit1 <- stan(file = 'schools.stan', data = schools_dat)
fit2 <- sampling(compiled_model, data_list = schools_dat)
1 Like

Hi and welcome. Is that the full error code?

The full error code is:

   Exception: variable does not exist; processing stage=data initialization; variable name=J; base type=int  (in 'model63e221acfa51_schools' at line 3)
failed to create the sampler; sampling not done```

Usually the means J is not being declared in the model. Can you post the model code?

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
}

data_list ? This should be data no?

1 Like

Yep! I changed it somewhere along the way. It works fine. It’s interesting to me that R doesn’t give some sort of invalid keyword argument error. The error is sent by c++ from the stan model.

1 Like

Yeah, it’s because R will just pass on undeclared named arguments through …, and if the functions thereafter use ‘…’, it will just keep getting passed down and ignored. So basically, data_list was being ignored, and sampling() does not require a data argument. Valid call, but stan was receiving no data.

1 Like

Thanks for the additional information.