yrosseel / lavaan

an R package for structural equation modeling and more
http://lavaan.org
429 stars 98 forks source link

2-level model, centering within-only variables in Sigma.W #278

Closed ecmerkle closed 1 year ago

ecmerkle commented 1 year ago

I believe this fixes a bug that was a tricky one to find, related to observed variables that appear at the within level but not at the between level.

In computing Sigma.W, the within-only variables were being centered by group means. But I believe that all the variance of within-only variables should be attributed to the within level, so we should center by grand mean instead of group mean. The blavaan models under development also work better (better sampling and agreement with lavaan) with this change.

There might be some matrix trick to replace the loop that I added, but I couldn't immediately think of a good way to do it.

yrosseel commented 1 year ago

Thanks. You are right. This bug could stay hidden for a long time for the simple reason that lavaan re-estimates Sigma.W and Sigma.B using the EM algorithm later.