Regime conditional distribution

Dear all,

I need to develop a model, but I need some hints in terms of coding.
I need to find the regime dependent variance covariance matrix, for a linear model conditional on 2 states identified with a msmFit model. I have a id specific measure, call it y_i, and an average across several ids, call it x_m.
The different steps I need to implement in R, for which I need help are the following:

  1. Assuming that my variables are jointly gaussian distributed conditional on the regime, such that:
    y_it | x_mt, S_t ~ N(alpha_i + beta_{i,s} x_t; sigma_i^2)
  2. I need to write the distribution of each id conditional on the average as:
    (y_it, x_mt) | S_t
    Where S_t is the regime identified with a Markov chain
  3. Then, since this is normal, I need to find analytically the mean and variance.
  4. With the alpha, beta and sigma I need to find the densities conditional on the regimes
  5. Using forward filtering I will finally estimate the likelihood

How can I code this?

Thank You