Hello! I’m really new to Stan, I’m working on my thesis and I’m trying to write a stan model for estimating a mixture regression model with multiple covariates. I’m trying to modify the model from the following link, in order to include more than one covariate
https://stats.stackexchange.com/questions/259705/how-to-build-a-bayesian-regression-model-of-a-response-that-is-a-gaussian-mixtur . I have all continuous variables, both the responde and the explicatives. The model is:
data {
int<lower=1> K; // number of mixture components
int<lower=1> N; // number of data points
real y[N]; // observations
real x[N]; // covariates
}
parameters {
simplex[K] pi_prob; // mixing proportions
real alpha[K]; // locations of mixture components
real<lower=0> sigma[K]; // scales of mixture components
real beta; // regression coefficient
}
model {
real ps[K]; // temp for log component densities
alpha ~ normal(0, 30);
for (n in 1:N) {
for (k in 1:K) {
ps[k] = log(pi_prob[k]) + normal_lpdf(y[n] | alpha[k] + x[n] * beta, sigma[k]);
}
target += log_sum_exp(ps);
}
}
generated quantities {
int z; // class index
real y_rep[N];
for (i in 1:N) {
z = categorical_rng(pi_prob);
y_rep[i] = normal_rng(alpha[z] + beta * x[i], sigma[z]);
}
}
It would be very helpful if anyone can help me, and sorry if the question has already been answered.
Thanks,
Giovanni