yrosseel / lavaan

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

center ordinal lvs during lavPredict() optimization #348

Closed ecmerkle closed 1 month ago

ecmerkle commented 1 month ago

Hi Yves, here is a small issue I discovered in lavPredict() with ordinal variables.

The optimization step involves predictions from the standard normal cdf here. This works most of the time because the lv mean (alpha) is usually fixed to 0. But it doesn't work when the lv mean is nonzero, which might happen in some multi-group models or in the silly model below that fixes the lv mean to 3. So the pull request explicitly centers the lvs by alpha before they get sent to the pnorm() lines.

data(Science, package = 'mirt')

m1 <- ' f1 =~ Comfort + Work + Future + Benefit
             f1 ~ 3*1 '

fit <- cfa(m1, data = Science, ordered = TRUE, std.lv = TRUE)
lvs <- lavPredict(fit)
summary(lvs[,1]) ## mean is near 1 in 0.6-17