simsem / semTools

Useful tools for structural equation modeling
75 stars 36 forks source link

cfa.mi summary not providing parameter estimates #135

Closed zstickley closed 9 months ago

zstickley commented 9 months ago

Hello everyone!

I've been running into an issue recently when running CFA models on imputed data. Whenever I run summary() on the fit object from cfa.mi, I get model fit information but not parameter estimates. Instead, I receive the following error:

Error in if (categorical.flag) { : argument is of length zero.

I made sure that I'm using the most current versions of R (4.3.2), lavaan (0.6-17), and semTools (0.5-6).

Here is a reproducible example:

#### Data Simulation ####
simMod <- '
LV =~ (.7)*v1 + 
      (.7)*v2 + 
      (.7)*v3 + 
      (.7)*v4 + 
      (.7)*v5 + 
      (.7)*v6

LV ~~ 1*LV
LV ~ 0*1

v1 ~ 3*1
v2 ~ 3*1
v3 ~ 3*1
v4 ~ 3*1
v5 ~ 3*1
v6 ~ 3*1
'

dat <- lavaan::simulateData(model = simMod,
                            model.type = "cfa",
                            meanstructure = T,
                            std.lv = T,
                            sample.nobs = 500)

# Impose missingness
dat$v1[sample(1:500,50,replace = F)] <- NA
dat$v2[sample(1:500,50,replace = F)] <- NA
dat$v3[sample(1:500,50,replace = F)] <- NA
dat$v4[sample(1:500,50,replace = F)] <- NA
dat$v5[sample(1:500,50,replace = F)] <- NA
dat$v6[sample(1:500,50,replace = F)] <- NA

#### MICE Imputation ####
library(mice)

# Create predictor matrix
qp <- quickpred(dat)

# Impute data using Mice
ImpMice <- mice(data = dat,
                predictorMatrix = qp,
                m = 100,
                maxit = 10)

#### SEM Testing ####
mod <- ' 
LV =~ v1 + 
      v2 + 
      v3 + 
      v4 + 
      v5 + 
      v6
'

library(semTools)

fitMice <- cfa.mi(model = mod,
                 data = ImpMice,
                 estimator="ML", 
                 std.lv=TRUE,
                 meanstructure=TRUE,
                 miPackage = "mice")

summary(fitMice, 
        standardized = T, 
        fit.measures=TRUE, 
        fmi = T)
lloydxeno commented 9 months ago

I ran into the same issue with cfa.mi giving "Error in if (categorical.flag) { : argument is of length zero" when summary is called.

This is probably a result of lavaan's upgrade to version 0.6-17. semTools worked (and still works) fine with lavaan 0.6-16. The following is reproducible code.


require(remotes) install_version("lavaan", version = "0.6-16", repos = "http://cran.us.r-project.org") library(semTools) library(Amelia) ceer_parent_amelia <- amelia(ss, m = 5, ncpus = 11) imputes <- ceer_parent_amelia$imputations

CEER_Model <- ' dysreg =~ temper + argues + blame + touchy + spiteful + uncooperative + moodswing + frustrated + irritable '

ceer_by_sex_parent <- cfa.mi(CEER_Model, data = imputes, group = "male", estimator = "DWLS")

summary(ceer_by_sex_parent) lavaan.mi object based on 5 imputed data sets. See class?lavaan.mi help page for available methods.

Convergence information: The model converged on 5 imputed data sets

Rubin's (1987) rules were used to pool point and SE estimates across 5 imputed data sets, and to calculate degrees of freedom for each parameter's t test and CI.

Parameter Estimates:

Standard errors Standard Information Expected Information saturated (h1) model Unstructured

. . . rest of the output follows .......

TDJorgensen commented 9 months ago

Yes, that bug should be resolved now. Try installing the latest software:

remotes::install_github("yrosseel/lavaan")
remotes::install_github("simsem/semTools/semTools")
lloydxeno commented 9 months ago

Thanks so much. I confirm that semTools 0.5-6.933 work with lavaan 0.6-18.1989. Much appreciated!

zstickley commented 9 months ago

This update works for me as well. Thank you very much!