Creating summary of a lavaan.mi object (runMI()): R cannot find correct summary() method for lavaan.mi-class object

38 views
Skip to first unread message

Oscar García Casanova

unread,
Apr 23, 2024, 5:56:00 PMApr 23
to lavaan

Hi. So I am trying to use the runMI() function of the semTools package to fit a multiple impute ordinal MG-CFA model. However, it seems like R isn't finding the correct summary() methods for lavaan.mi-class objects, instead using the method for lavaanList-class objects, as per this discussion. My code looks like this (also attached):


```{r}
rm(list=ls()) # Clean workspace
options(scipen=10000) # Set scientific notation
library(tidyverse)
library(lavaan) # For CFA
library(semTools) # For measurement of invariance in MGCFA
set.seed(121012) # For reproducibility

# Create simulated categorical dataset with missing values
popModel <- "
f1 =~ 0.6*y1 + 0.6*y2 + 0.6*y3 + 0.6*y4
y1 ~*~ 1*y1
y2 ~*~ 1*y2
y3 ~*~ 1*y3
y4 ~*~ 1*y4
f1 ~~ 1*f1
y1 | 0.5*t1
y2 | 0.25*t1
y3 | 0*t1
y4 | -0.5*t1
"

dat <- simulateData(popModel, sample.nobs  = 200L)
miss.pat <- matrix(as.logical(rbinom(prod(dim(dat)), 1, 0.2)), nrow(dat), ncol(dat))
dat[miss.pat] <- NA

# Convert columns to factors with specified levels
dat$y1 <- factor(dat$y1, levels = c(1, 2))
dat$y2 <- factor(dat$y2, levels = c(1, 2))
dat$y3 <- factor(dat$y3, levels = c(1, 2))
dat$y4 <- factor(dat$y4, levels = c(1, 2))

# Define categories for a grouping variable school
schools <- c("Marx", "Durkheim", "Weber", "Comte")

# Sample the school categories randomly for each row
dat$school <- sample(schools, size = nrow(dat), replace = TRUE)

# Define Ordinal MG-CFA model
analyzeModel <- "
f1 =~ y1 + y2 + y3 + y4
y1 ~*~ 1*y1
y2 ~*~ 1*y2
y3 ~*~ 1*y3
y4 ~*~ 1*y4
"
# Create predictor matrix
predictormatrix<-quickpred(dat)

# Impute 5 datasets (common practice)
dat_imp <- mice(data = dat,
                           predictorMatrix = predictormatrix,
                           m = 5,
                           # Specify imputation method for ordinal data
                           method = "polr")

mice.imp <- NULL
for(i in 1:5) mice.imp[[i]] <- complete(dat_imp, action=i, inc=FALSE)  

# run lavaan with previously imputed data using runMI
out2 <- semTools::runMI(analyzeModel,
                        data = mice.imp,
                        fun = "cfa",
                        meanstructure = TRUE,
                        group = "school", ordered = TRUE,
                        std.lv = TRUE)

detach("package:tidyverse", unload = TRUE)
library(semTools)

summary(out2)
```

Output then is the same as the one provided in the discussion I linked:
```{r}
> summary(out2) lavaanList (0.6-17) -- based on 5 datasets (5 converged) Group 1 []: Latent Variables: est.ave f1 =~ y1 0.774 y2 0.770 y3 0.567 y4 0.674 Intercepts: est.ave .y1 0.000 .y2 0.000 .y3 0.000 .y4 0.000 f1 0.000 Thresholds: est.ave y1|t1 0.690 y2|t1 0.377 y3|t1 0.037 y4|t1 -0.570 Variances: est.ave .y1 0.387 .y2 0.397 .y3 0.669 .y4 0.510 f1 1.000 Scales y*: est.ave y1 1.000 y2 1.000 y3 1.000 y4 1.000 Group 2 []: ....
```

Meanwhile, when printing SessionInfo (in case it might be relevant) thereafter this is indicated:
```{r}
> sessionInfo() R version 4.3.3 (2024-02-29 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 11 x64 (build 22631) Matrix products: default locale: [1] LC_COLLATE=English_United Kingdom.utf8 LC_CTYPE=English_United Kingdom.utf8 [3] LC_MONETARY=English_United Kingdom.utf8 LC_NUMERIC=C [5] LC_TIME=English_United Kingdom.utf8 time zone: Europe/Berlin tzcode source: internal attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] mice_3.16.0 stargazer_5.2.3 lavaanPlot_0.8.1 tidySEM_0.2.6 OpenMx_2.21.11 reshape2_1.4.4 [7] sirt_4.1-15 blavaan_0.5-4 Rcpp_1.0.11 psych_2.4.3 semTools_0.5-6 lavaan_0.6-17 [13] factoextra_1.0.7 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.3 purrr_1.0.2 [19] readr_2.1.5 tidyr_1.3.1 tibble_3.2.1 ggplot2_3.5.1 loaded via a namespace (and not attached): [1] RColorBrewer_1.1-3 shape_1.4.6.1 rstudioapi_0.16.0 jsonlite_1.8.8 [5] magrittr_2.0.3 jomo_2.7-6 estimability_1.5 nloptr_2.0.3 [9] farver_2.1.1 rmarkdown_2.26 ragg_1.3.0 vctrs_0.6.3 [13] minqa_1.2.6 CompQuadForm_1.4.3 htmltools_0.5.7 broom_1.0.5 [17] mitml_0.4-5 parallelly_1.37.1 StanHeaders_2.32.6 htmlwidgets_1.6.4 [21] gsubfn_0.7 plyr_1.8.9 sandwich_3.1-0 emmeans_1.10.1 [25] zoo_1.8-12 admisc_0.35 igraph_2.0.3 iterators_1.0.14 [29] lifecycle_1.0.4 pkgconfig_2.0.3 Matrix_1.6-1 R6_2.5.1 [33] fastmap_1.1.1 future_1.33.2 digest_0.6.33 colorspace_2.1-0 [37] bain_0.2.10 textshaping_0.3.7 labeling_0.4.3 progressr_0.14.0 [41] fansi_1.0.4 timechange_0.3.0 httr_1.4.7 abind_1.4-5 [45] compiler_4.3.3 withr_3.0.0 pander_0.6.5 backports_1.4.1 [49] inline_0.3.19 carData_3.0-5 fastDummies_1.7.3 QuickJSR_1.1.3 [53] pkgbuild_1.4.4 pan_1.9 MASS_7.3-60.0.1 loo_2.7.0 [57] tools_4.3.3 pbivnorm_0.6.0 MplusAutomation_1.1.1 future.apply_1.11.2 [61] nnet_7.3-19 glue_1.6.2 quadprog_1.5-8 dbscan_1.1-12 [65] DiagrammeR_1.0.11 nlme_3.1-164 grid_4.3.3 checkmate_2.3.1 [69] generics_0.1.3 gtable_0.3.5 tzdb_0.4.0 data.table_1.14.8 [73] hms_1.1.3 car_3.1-2 utf8_1.2.3 ggrepel_0.9.5 [77] RANN_2.6.1 foreach_1.5.2 pillar_1.9.0 splines_4.3.3 [81] lattice_0.22-5 survival_3.5-8 tidyselect_1.2.1 pbapply_1.7-2 [85] knitr_1.46 gridExtra_2.3 CDM_8.2-6 stats4_4.3.3 [89] xfun_0.43 texreg_1.39.3 matrixStats_1.3.0 rstan_2.32.6 [93] proto_1.0.0 visNetwork_2.1.2 stringi_1.8.3 yaml_2.3.8 [97] boot_1.3-29 evaluate_0.23 codetools_0.2-19 nonnest2_0.5-6 [101] cli_3.6.1 TAM_4.2-21 rpart_4.1.23 RcppParallel_5.1.7 [105] xtable_1.8-4 systemfonts_1.0.6 munsell_0.5.1 globals_0.16.3 [109] polycor_0.8-1 coda_0.19-4.1 parallel_4.3.3 rstantools_2.4.0 [113] ellipsis_0.3.2 bayesplot_1.11.1 lme4_1.1-35.3 listenv_0.9.1 [117] glmnet_4.1-8 mvtnorm_1.2-3 scales_1.3.0 crayon_1.5.2 [121] tmvnsim_1.0-2 rlang_1.1.1 mnormt_2.1.1
```

Any clue on how can I get it right? Thank you very much!!


LavaanMI attempt.R

Terrence Jorgensen

unread,
Jun 24, 2024, 10:48:58 AM (9 days ago) Jun 24
to lavaan
You can try installing the new lavaan.mi package, along with development versions of lavaan and semTools:
 
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. Read the README and NEWS files closely before proceeding, because other small changes have been made to the user interface.

https://github.com/TDJorgensen/lavaan.mi

https://github.com/TDJorgensen/lavaan.mi/blob/main/NEWS.md

I hope to send this to CRAN by the end of the month or early July, and hoping even more to avoid the issues people have had with R not finding the right methods.

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
Reply all
Reply to author
Forward
0 new messages