Variable does not exist error when trying to fit model

Hi,
when i try to fit the model, i run into the following error

Error in new_CppObject_xp(fields$.module, fields$.pointer, …) :
Exception: variable does not exist; processing stage=data initialization; variable name=y; base type=vector_d (in ‘model19fd8e7e221_stan_19fd887c257’ at line 3)

failed to create the sampler; sampling not done
could anyone explain why this occurs?
Many thanks,
CJ

data {
  int<lower=0> n;
  vector[n] y;
  real post_exp;
}
transformed data{
  real a;
  real<lower=0> b;
  a = (1-mean(y))*n;
  b=1;
}
parameters {
  real mu;
}
model {
  mu ~ normal(a, b);
  y ~ normal(mu, 1); 
} 

stimulating data

set.seed(100)
n=100
true_mu = 1
post_exp = rnorm(n=n, mean=true_mu, sd=1)

fitting the model

library(rstan)
options(mc.cores = parallel::detectCores())
data = list(n=n, post_exp=post_exp, a=13, b=1)
fit <- sampling(normal, data=data, chains=4, iter=2000, warmup=1000)

The list of data that you pass to Stan

data = list(n=n, post_exp=post_exp, a=13, b=1)

does not have any values for y. I think that is the cause of the error. Perhaps the variable post_exp in your R code should by passed to y? Also, you declare the data post_exp in the Stan model but you do not use it anywhere and you pass in values of a and b, though they are transformed data. I do not know whether passing in a and b will cause any problems, but it is confusing.

2 Likes