yrosseel / lavaan

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

handle between-only ovs when predicting level 2 lvs #292

Closed ecmerkle closed 10 months ago

ecmerkle commented 10 months ago

The blavaan multilevel testing continues! This pr addresses a lavPredict() bug when there are between-only variables. The data from those variables were not being included in the level 2 lv predictions.

The example below uses the data from "school.dat". It has a negative variance estimate, and I should probably add a third between-only variable. But it should be enough to demonstrate the problem.

model <- '
    level: within
        fw =~ y6 + y7 + y8 + y9

    level: between
        fb =~ y6 + y7 + y8 + y9
        fb2 =~ x3 + x4 '

fit <- sem(model, data = Data, cluster = "school")

out <- lavPredict(fit, level = 2)
cor(out) # lv correlation is 1
yrosseel commented 10 months ago

Nice catch! Thanks a lot!