Translating Lavaan model to Stan - Low E-BFMI

I think the main difference between your Stan code and the blavaan code is that you are sampling the latent variables as parameters and blavaan is not. It is possible to write this model as a multivariate normal of all three observed variables, with the latent variables integrated out of the likelihood. That really improves efficiency because you have many fewer parameters to sample and to autodiff. And it is possible to sample the latent variables in generated quantities if you want them.

Some further details appear in this discourse thread and in this JSS paper. Also, see this post that illustrates how the marginalization works for a specific model.

If you plan to do something outside of the usual SEM framework, though, the marginalization of latent variables could get tricky. It may depend on whether you plan to use a multivariate normal likelihood.

3 Likes