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!