Error in getting fit measures for CFA with imputed data

54 views
Skip to first unread message

SALOME chibwana

unread,
Jun 22, 2024, 8:58:50 AM (11 days ago) Jun 22
to lavaan
I am carrying out a confirmatory factor analysis on categorical (ordinal) data. Whilst I can get the standardized output, I get an error when I try to get the fitmesures.

library(lavaan)
library(mice)
library(semTools)

str(cfadata)#all items are recognised as factors, output for one item below#
PAI1 : Factor w/ 4 levels "1","2","3","4": NA 1 1 2 1 4 1 3 2 1 ..

set.seed(123)

mice.imp<-mice(cfadata)
mice.imp <- NULL
for(i in 1:5)mice.imp[[i]] <- complete(imp, action=i, inc=FALSE)
 
all(sapply(mice.imp, is.data.frame))
[1] TRUE

P3<- 'bonding=~PAI3+PAI4+PAI5+PAI6+PAI7+PAI8+PAI9+PAI10+PAI11+PAI12+PAI13+PAI14+PAI15+PAI16+PAI17+PAI18+PAI19+PAI20+PAI21'
data(cfadata)
out2 <- cfa.mi(model=P3, data=mice.imp, FUN=function(fit) {list(wrmr=lavaan::fitMeasures(fit, "wrmr"), zeroCells=lavaan::lavInspect(fit, "zero.cell.tables"))})

standardizedsolution(out2, type = "std.lv")
lavTestLRT.mi(out2, test = "D2", pool.robust = TRUE)
fitMeasures(out2, fit.measures = c("rmsea","srmr","cfi"),test = "D2", pool.robust = TRUE)
Error in fitMeasures(out2, fit.measures = c("rmsea", "srmr", "cfi"), test = "D2", : could not find symbol "fm.args" in environment of the generic function

Any ideas as how to sort this?
Regards

Terrence Jorgensen

unread,
Jun 24, 2024, 8:56:15 AM (9 days ago) Jun 24
to lavaan
str(cfadata)#all items are recognised as factors, output for one item below#
PAI1 : Factor w/ 4 levels "1","2","3","4": NA 1 1 2 1 4 1 3 2 1 ..

The class "factor" indicates these are treated as nominal variables, rather than ordinal.  For mice to use ordered-logistic regression imputation models, you should reassign them as class c("ordered","factor") using the ordered() function.

out2 <- cfa.mi(model=P3, data=mice.imp, FUN=function(fit) {list(wrmr=lavaan::fitMeasures(fit, "wrmr"), zeroCells=lavaan::lavInspect(fit, "zero.cell.tables"))})

Are you only including the FUN= argument because it was in the help-page example?  That was an arbitrary choice.  The WRMR index is not recommended, and there is no need to keep track of tables if you are not getting warnings about them.

standardizedsolution(out2, type = "std.lv") 
lavTestLRT.mi(out2, test = "D2", pool.robust = TRUE)
fitMeasures(out2, fit.measures = c("rmsea","srmr","cfi"),test = "D2", pool.robust = TRUE)
Error in fitMeasures(out2, fit.measures = c("rmsea", "srmr", "cfi"), test = "D2", : could not find symbol "fm.args" in environment of the generic function

Try installing the latest development versions of lavaan and semTools from GitHub, as well as the new lavaan.mi package:
 
remotes::install_github("yrosseel/lavaan") 
remotes::install_github("TDJorgensen/lavaan.mi") 
remotes::install_github("simsem/semTools/semTools")

I deprecated runMI() functionality from semTools, and the lavaan.mi package is now updated with many bug-fixes and new features.  Note that standardizedSolution() is a lavaan function, which was not available for semTools::cfa.mi() output, but standardizedSolution.mi() is now available for lavaan.mi::cfa.mi() output.  for lavTestLRT.mi(), the argument test= has been changed to pool.method= to avoid conflicts with the lavaan::lavTestLRT() function's test= argument.  
Read the README and NEWS files closely before proceeding, because other small changes have been made to the user interface.



I hope to send this to CRAN by the end of the month or early July.

Terrence D. Jorgensen    (he, him, his)
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam
http://www.uva.nl/profile/t.d.jorgensen


SALOME chibwana

unread,
Jun 25, 2024, 10:41:29 AM (8 days ago) Jun 25
to lavaan
Terrence
I have changed the ariables to ordered factor
str(cfadata) $ PAI1 : Ord.factor w/ 4 levels "1"<"2"<"3"<"4": NA 1 1 2 1 4 1 3 2 1 ...

The model specification remains the same (with the exception of the 'FUN' script) which i have removed.
set.seed(123)
mice.imp<-mice(cfadata)
mice.imp <- NULL
for(i in 1:5)mice.imp[[i]] <- complete(imp, action=i, inc=FALSE)

all(sapply(mice.imp, is.data.frame))


P3<- 'bonding=~PAI3+PAI4+PAI5+PAI6+PAI7+PAI8+PAI9+PAI10+PAI11+PAI12+PAI13+PAI14+PAI15+PAI16+PAI17+PAI18+PAI19+PAI20+PAI21'
impout<- lavaan.mi::cfa.mi(P3, data = mice.imp)

summary(impout) #top part of the summary command shown# lavaanList (0.6-19.2150) -- based on 5 datasets (5 converged) lavaanList (0.1-0.0028) -- based on 5 datasets (5 converged)

When I try to run any of these commands I get teh same error

lavaan.mi::lavTestLRT.mi(impout, pool.method = "D4", pool.robust = TRUE)
lavaan.mi::standardizedSolution.mi(impout)
lavaan.mi::fitMeasures(impout,  fit.measures = c("rmsea","srmr","cfi"),pool.method = "D4", pool.robust = TRUE)

Error in lavListInspect(object, "options") : inherits(object, "lavaanList") is not TRUE

Terrence Jorgensen

unread,
Jun 26, 2024, 3:00:46 AM (7 days ago) Jun 26
to lavaan
Looks like you are using lazy loading:

impout<- lavaan.mi::cfa.mi(P3, data = mice.imp)

You have to attach an R package if you expect it to be functional.

library(lavaan.mi)

Hopefully that resolves the issue.

SALOME chibwana

unread,
Jun 27, 2024, 5:44:33 AM (6 days ago) Jun 27
to lavaan
hello

I did. I get the same error regardless of code

> standardizedSolution.mi(out2, type = "std.lv") Error in lavListInspect(object, "options") : inherits(object, "lavaanList") is not TRUE > lavaan.mi::standardizedSolution.mi(out2, type = "std.lv") Error in lavListInspect(object, "options") : inherits(object, "lavaanList") is not TRUE

Regards
Reply all
Reply to author
Forward
0 new messages