yrosseel / lavaan

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

Error in lavScore with missing data #372

Open jhorzek opened 3 months ago

jhorzek commented 3 months ago

Hi,

I just ran into some errors with the updated version of the lavScore function that sent me down a small rabbit hole :). According to the comments, the function was updated in version 0.5-17 to always use all cases - even those with missings. However, if I try to get the scores of a model with listwise deletion, I get an error:

library(lavaan)

model <- ' 
  # latent variable definitions
     ind60 =~ x1 + x2 + x3
     dem60 =~ y1 + a*y2 + b*y3 + c*y4
     dem65 =~ y5 + a*y6 + b*y7 + c*y8

  # regressions
    dem60 ~ ind60
    dem65 ~ ind60 + dem60
'

PoliticalDemocracy_NA <- PoliticalDemocracy
PoliticalDemocracy_NA[1, "x3"] <- NA

# if there are missing data points, the scores function runs into errors.
fit <- sem(model, 
           data = PoliticalDemocracy_NA)
lavScores(fit)
# Error in t(X) - lavsamplestats@mean[[g]] %*% J : non-conformable arrays

This error occurs because all data points with missings have been removed from data X, but J is still based on the full sample size (see here).

Best, Jannik

jhorzek commented 3 months ago

I've also found a separate bug in lavScores that is resolved in a pull request.

yrosseel commented 3 months ago

Indeed. The code in lavScores() is very old, and needs to be revised. It did not follow up on changes that were made later. I will have a good look at this in the next days.