Combining Markov switching models and Linear models in a structural equation

Dear all,

I'm relatively new on R and yet having to do something quite complicated (at least for me).
I have stock's returns (r`i') and market return (Rm) for a total of 100 stocks, even though I will replicate the analysis for other markets. My analysis is composed of three phases, but I'm stuck in the first.
I need to find intercept and coefficient for a system of simultaneous equations, one modeled as msmFift and the other as OLS. In particular I want the beta of the second equation to be conditional on the regime identified in the first equation. Algebraically:


In this way I aim to find the conditional betas, depending on each regime (assume 2). In addition, for further analysis, I need to extract the variance-covariance matrix for each system of equations, that is looping over all the stocks in the market.

Any idea about the way to proceed, functions/packages?



Without additional detail, it's hard to provide detailed help. I suspect that you may find Jim Savage's post quite useful. If you choose to go the Stan route (I would), the Stan forums may be a good place to ask for help as well.