# Exception: variable does not exist

**URL:** <https://discourse.mc-stan.org/t/exception-variable-does-not-exist/15209>\
**Category:** RStan\
**Tags:** rstan, specification\
**Created:** [May 17, 2020, 7:38pm UTC](https://discourse.mc-stan.org/t/exception-variable-does-not-exist/15209 "2020-05-17T19:38:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfhawkin](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@jfhawkin](https://discourse.mc-stan.org/u/jfhawkin)\
**Post date:** [May 17, 2020, 7:38pm UTC](https://discourse.mc-stan.org/t/exception-variable-does-not-exist/15209/1 "2020-05-17T19:38:44Z")

</div>

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)

```

---

<div class="post-metadata">

**Author:** ![Ara\_Winter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ara_winter/32/4284_2.png) [@Ara\_Winter](https://discourse.mc-stan.org/u/Ara_Winter)\
**Post date:** [May 18, 2020, 6:14pm UTC](https://discourse.mc-stan.org/t/exception-variable-does-not-exist/15209/2 "2020-05-18T18:14:12Z")

</div>

Hi and welcome. Is that the full error code?

---

<div class="post-metadata">

**Author:** ![jfhawkin](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@jfhawkin](https://discourse.mc-stan.org/u/jfhawkin)\
**Post date:** [May 18, 2020, 7:19pm UTC](https://discourse.mc-stan.org/t/exception-variable-does-not-exist/15209/3 "2020-05-18T19:19:17Z")

</div>

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```
````

---

<div class="post-metadata">

**Author:** ![Ara\_Winter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ara_winter/32/4284_2.png) [@Ara\_Winter](https://discourse.mc-stan.org/u/Ara_Winter)\
**Post date:** [May 18, 2020, 9:12pm UTC](https://discourse.mc-stan.org/t/exception-variable-does-not-exist/15209/4 "2020-05-18T21:12:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jfhawkin](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@jfhawkin](https://discourse.mc-stan.org/u/jfhawkin)\
**Post date:** [May 18, 2020, 9:32pm UTC](https://discourse.mc-stan.org/t/exception-variable-does-not-exist/15209/5 "2020-05-18T21:32:47Z")

</div>

```
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
}

```

---

<div class="post-metadata">

**Author:** ![Stephen\_Martin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stephen_martin/32/7879_2.png) [@Stephen\_Martin](https://discourse.mc-stan.org/u/Stephen_Martin)\
**Post date:** [May 20, 2020, 1:59am UTC](https://discourse.mc-stan.org/t/exception-variable-does-not-exist/15209/6 "2020-05-20T01:59:49Z")

</div>

> [@jfhawkin](#):
>
> `fit2 <- sampling(compiled_model, data_list = schools_dat)`

`data_list` ? This should be `data` no?

---

<div class="post-metadata">

**Author:** ![jfhawkin](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@jfhawkin](https://discourse.mc-stan.org/u/jfhawkin)\
**Post date:** [May 20, 2020, 12:43pm UTC](https://discourse.mc-stan.org/t/exception-variable-does-not-exist/15209/7 "2020-05-20T12:43:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Stephen\_Martin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stephen_martin/32/7879_2.png) [@Stephen\_Martin](https://discourse.mc-stan.org/u/Stephen_Martin)\
**Post date:** [May 20, 2020, 9:39pm UTC](https://discourse.mc-stan.org/t/exception-variable-does-not-exist/15209/8 "2020-05-20T21:39:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jfhawkin](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@jfhawkin](https://discourse.mc-stan.org/u/jfhawkin)\
**Post date:** [May 21, 2020, 9:34pm UTC](https://discourse.mc-stan.org/t/exception-variable-does-not-exist/15209/9 "2020-05-21T21:34:07Z")

</div>

Thanks for the additional information.
