Bayesian mixture model with multiple covariates


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

General: I can’t say for sure but I suspect you’ll struggle getting this to work because it may be impossible to specify a uniqueness constraint on your Gaussian components to enables identifiability in the model. I.e. there may be a large number or even a possible infinity of parameter combinations that get you the same result therefore the parameters are physically meaningless. Again, just a first approximation, I can’t say for sure.

This bit is a problem, since ps[k] is set again and again ,each time wiping out the previous value. So you’ll just end up with ps[k] set at the last value of n.