# Stan error, variable does not exist; processing stage=data initialization; variable name=N;

**URL:** <https://discourse.mc-stan.org/t/stan-error-variable-does-not-exist-processing-stage-data-initialization-variable-name-n/34359>\
**Category:** Modeling\
**Tags:** rstan\
**Created:** [March 6, 2024, 9:35am UTC](https://discourse.mc-stan.org/t/stan-error-variable-does-not-exist-processing-stage-data-initialization-variable-name-n/34359 "2024-03-06T09:35:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jezerca\_Hodaj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jezerca_hodaj/32/18024_2.png) [@Jezerca\_Hodaj](https://discourse.mc-stan.org/u/Jezerca_Hodaj)\
**Post date:** [March 6, 2024, 9:35am UTC](https://discourse.mc-stan.org/t/stan-error-variable-does-not-exist-processing-stage-data-initialization-variable-name-n/34359/1 "2024-03-06T09:35:12Z")

</div>

I am new to R and also using stan modelling. I keep getting this error that my variable doesn’t exist

// Bayesian logistic regression model in Stan

data {  
int\<lower=0\> N; // Number of observations  
int\<lower=0, upper=1\> y[N]; // Binary outcome variable  
real age[N]; // Age variable  
int\<lower=0, upper=1\> sex[N]; // Sex variable  
int\<lower=0, upper=3\> cp[N]; // Chest pain type variable  
real trestbps[N]; // Resting blood pressure variable  
real chol[N]; // Cholesterol variable  
real thalch[N]; // Maximum heart rate achieved variable  
}

parameters {  
real alpha; // Intercept  
real beta\_age; // Coefficient for age  
real beta\_sex; // Coefficient for sex  
vector[3] beta\_cp; // Coefficients for chest pain type (3 dummy variables)  
real beta\_trestbps; // Coefficient for resting blood pressure  
real beta\_chol; // Coefficient for cholesterol  
real beta\_thalch; // Coefficient for maximum heart rate achieved  
}

model {  
// Priors  
alpha ~ normal(0, 1); // Noninformative prior for intercept  
beta\_age ~ normal(0, 1); // Noninformative prior for age coefficient  
beta\_sex ~ normal(0, 1); // Noninformative prior for sex coefficient  
beta\_cp ~ normal(0, 1); // Noninformative prior for coefficients of chest pain type  
beta\_trestbps ~ normal(0, 1); // Noninformative prior for resting blood pressure coefficient  
beta\_chol ~ normal(0, 1); // Noninformative prior for cholesterol coefficient  
beta\_thalch ~ normal(0, 1); // Noninformative prior for maximum heart rate coefficient

// Likelihood  
for (i in 1:N) {  
real linpred;  
linpred = alpha + beta\_age \* age[i] + beta\_sex \* sex[i] + beta\_cp[cp[i]] + beta\_trestbps \* trestbps[i] + beta\_chol \* chol[i] + beta\_thalch \* thalch[i];  
y[i] ~ bernoulli\_logit(linpred);  
}  
}

and

data\_list ← list(  
N = nrow(train\_data),  
y = train\_data$binary\_num,  
age = train\_data$age,  
sex = train\_data$sex,  
cp = train\_data$cp,  
trestbps = train\_data$trestbps,  
chol = train\_data$chol,  
thalch = train\_data$thalch  
)

# Compile the Stan model

stan\_model ← stan(file = “bayeslrm.stan”)

Anyone that could help me with this, Im really struggling. Thank you!

---

<div class="post-metadata">

**Author:** ![frank\_hezemans](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/frank_hezemans/32/17981_2.png) [@frank\_hezemans](https://discourse.mc-stan.org/u/frank_hezemans)\
**Post date:** [March 6, 2024, 11:39am UTC](https://discourse.mc-stan.org/t/stan-error-variable-does-not-exist-processing-stage-data-initialization-variable-name-n/34359/2 "2024-03-06T11:39:55Z")

</div>

Welcome. If I understand correctly, you haven’t passed your data object `data_list` to the call to `stan`.  
What happens if you try: `stan_model <- stan(file = "bayeslrm.stan", data = data_list)`

---

<div class="post-metadata">

**Author:** ![Jezerca\_Hodaj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jezerca_hodaj/32/18024_2.png) [@Jezerca\_Hodaj](https://discourse.mc-stan.org/u/Jezerca_Hodaj)\
**Post date:** [March 6, 2024, 3:54pm UTC](https://discourse.mc-stan.org/t/stan-error-variable-does-not-exist-processing-stage-data-initialization-variable-name-n/34359/3 "2024-03-06T15:54:26Z")

</div>

I changed the prior for the cp vector in my .stan file and I also tried your suggestions. I realized later that I hadn’t included the dataset.

This is what I have now and I still get an error.

// Bayesian logistic regression model in Stan

data {  
int\<lower=0\> N; // Number of observations  
int\<lower=0, upper=1\> y[N]; // Binary outcome variable  
real age[N]; // Age variable  
int\<lower=0, upper=1\> sex[N]; // Sex variable  
int\<lower=0, upper=3\> cp[N]; // Chest pain type variable  
real trestbps[N]; // Resting blood pressure variable  
real chol[N]; // Cholesterol variable  
real thalch[N]; // Maximum heart rate achieved variable  
}

parameters {  
real alpha; // Intercept  
real beta\_age; // Coefficient for age  
real beta\_sex; // Coefficient for sex  
vector[3] beta\_cp; // Coefficients for chest pain type (3 dummy variables)  
real beta\_trestbps; // Coefficient for resting blood pressure  
real beta\_chol; // Coefficient for cholesterol  
real beta\_thalch; // Coefficient for maximum heart rate achieved  
}

model {  
// Priors  
alpha ~ normal(0, 1); // Noninformative prior for intercept  
beta\_age ~ normal(0, 1); // Noninformative prior for age coefficient  
beta\_sex ~ normal(0, 1); // Noninformative prior for sex coefficient  
beta\_cp ~ multi\_normal(rep\_vector(0, 3), diag\_matrix(rep\_vector(1, 3))); // Noninformative multivariate normal prior for coefficients of chest pain type  
beta\_trestbps ~ normal(0, 1); // Noninformative prior for resting blood pressure coefficient  
beta\_chol ~ normal(0, 1); // Noninformative prior for cholesterol coefficient  
beta\_thalch ~ normal(0, 1); // Noninformative prior for maximum heart rate coefficient

// Likelihood  
for (i in 1:N) {  
real linpred;  
linpred = alpha + beta\_age \* age[i] + beta\_sex \* sex[i] + beta\_cp[cp[i]] + beta\_trestbps \* trestbps[i] + beta\_chol \* chol[i] + beta\_thalch \* thalch[i];  
y[i] ~ bernoulli\_logit(linpred);  
}  
}

library(rstan)

train\_data ← read.csv(“heart\_disease\_train.csv”)

# Put the data into a list

data\_list ← list(  
N = nrow(train\_data),  
y = train\_data$binary\_num,  
age = train\_data$age,  
sex = train\_data$sex,  
cp = train\_data$cp,  
trestbps = train\_data$trestbps,  
chol = train\_data$chol,  
thalch = train\_data$thalch  
)

# Compile the Stan model

stan\_model ← stan(file = “bayeslrm.stan”, data = data\_list)

and the error

SAMPLING FOR MODEL ‘anon\_model’ NOW (CHAIN 1).  
Chain 1: Unrecoverable error evaluating the log probability at the initial value.  
Chain 1: Exception: vector[uni] indexing: accessing element out of range. index 0 out of range; expecting index to be between 1 and 3 (in ‘string’, line 34, column 4 to column 155)  
[1] “Error : Exception: vector[uni] indexing: accessing element out of range. index 0 out of range; expecting index to be between 1 and 3 (in ‘string’, line 34, column 4 to column 155)”  
[1] “error occurred during calling the sampler; sampling not done”

---

<div class="post-metadata">

**Author:** ![frank\_hezemans](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/frank_hezemans/32/17981_2.png) [@frank\_hezemans](https://discourse.mc-stan.org/u/frank_hezemans)\
**Post date:** [March 6, 2024, 4:55pm UTC](https://discourse.mc-stan.org/t/stan-error-variable-does-not-exist-processing-stage-data-initialization-variable-name-n/34359/4 "2024-03-06T16:55:42Z")

</div>

If I had to guess, I’d say something is going wrong with indexing `beta_cp`, since the error message says it was expecting an index between 1 and 3 (which is the length of `beta_cp`), but the provided index (presumably `cp[i]`) was 0.

Could you verify that you’re able to compile the model (without worrying about the data) with something like `blr_model <- stan_model(file = "bayeslrm.stan")`.

Then you can use the compiled model as follows: `blr_result <- sampling(object = blr_model, data = data_list)`.

If that still throws an error the only thing I can think of is that there’s something wrong with the data.

---

<div class="post-metadata">

**Author:** ![Jezerca\_Hodaj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jezerca_hodaj/32/18024_2.png) [@Jezerca\_Hodaj](https://discourse.mc-stan.org/u/Jezerca_Hodaj)\
**Post date:** [March 7, 2024, 1:03am UTC](https://discourse.mc-stan.org/t/stan-error-variable-does-not-exist-processing-stage-data-initialization-variable-name-n/34359/5 "2024-03-07T01:03:19Z")

</div>

I think you are right and the problem was with the cp. Cp takes values 0,1,2,3.  
Cp in my data was a nonnummeric categorical data, with four categories. So instead I transformed it to numerical having these four values. I transformed the sex variable to 0 and 1 too. the rest of the variables take real values.

I changed my .stan file now and it seemed to work. I got an output.

I rewrote my model :

// Likelihood  
for (i in 1:N) {  
real linpred;  
linpred = alpha + beta\_age \* age[i] + beta\_sex \* sex[i] + beta\_cp \* cp[i] + beta\_trestbps \* trestbps[i] + beta\_chol \* chol[i] + beta\_thalch \* thalch[i];  
y[i] ~ bernoulli\_logit(linpred);  
}  
}

I am not 100% sure if the model is right. First time I am learning about logistic regression.

---

<div class="post-metadata">

**Author:** ![frank\_hezemans](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/frank_hezemans/32/17981_2.png) [@frank\_hezemans](https://discourse.mc-stan.org/u/frank_hezemans)\
**Post date:** [March 8, 2024, 8:34am UTC](https://discourse.mc-stan.org/t/stan-error-variable-does-not-exist-processing-stage-data-initialization-variable-name-n/34359/6 "2024-03-08T08:34:16Z")

</div>

Great that you got the model running!  
If you’re seeking more substantive feedback on the underlying math / parameterisation of the model, I think you’re better off opening a new topic with a more suitable title and topic / tags (and perhaps marking this thread as solved).

---

<div class="post-metadata">

**Author:** ![frank\_hezemans](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/frank_hezemans/32/17981_2.png) [@frank\_hezemans](https://discourse.mc-stan.org/u/frank_hezemans)\
**Post date:** [March 8, 2024, 10:39am UTC](https://discourse.mc-stan.org/t/stan-error-variable-does-not-exist-processing-stage-data-initialization-variable-name-n/34359/7 "2024-03-08T10:39:31Z")

</div>

Also, I would recommend checking out the [brms](https://paul-buerkner.github.io/brms/) and/or [rstanarm](https://mc-stan.org/rstanarm/) packages. I think you’ll find quite some resources for fitting logistic regression models using those packages.
