Consistent Error Message in CFA with categorical data using runMI and MICE imputations

240 views
Skip to first unread message

Domib

unread,
Feb 4, 2021, 7:22:00 AM2/4/21
to lavaan
Dear Lavaan group,

Help is needed. 

I carry out a CFA with categorical data using a data with missing values. My approach, however, seems to run into issues that previous posts do not seem to address. Following the previous suggestions, I prepared the analysis in R as follows:

install.packages("lavaan", repos = "http://www.da.ugent.be", type = "source")
install.packages("devtools")
devtools::install_github("simsem/semTools/semTools")

I also downloaded and installed Rtools 3.5 to make sure all is in place. So I should have amongst others the development version of lavaan in place. In order to generate the imputations, I have been using MICE (EXCLUDE is a set of variables I did not want to use for the imputations):

PREDICTORMATRIX <- quickpred(BASELINE, exclude = EXCLUDE, mincor = 0.2, method = "kendall")
IMPUTE <- mice(BASELINE, predictorMatrix = PREDICTORMATRIX, m = 5)

I want to test various models all looped through the cfa command. One of the simpler models is of the form

MODELS$M1 <- '
C_Mot =~ 1* C_AB2+ C_AB3+ C_AB4+ C_AB5 + C_AB6 + C_AB8 + C_AB_PO2
E_Motivation =~ 1*E_AB2+ E_AB3+ E_AB4+ E_AB5 + E_AB6 + E_AB8 
K_Motivation =~ 1* K_AB2+ K_AB3+ K_AB4+ K_AB5 + K_AB6 + K_AB8
M_Motivation =~ 1* M_AB2+ M_AB3+ M_AB4+ M_AB5 + M_AB6 + M_AB8 + M_AB_PO2'        

The different models are saved in the list called MODELS. Since the I have multiply imputed data sets, I use runIM. In the loop below, all analysis output will be saved in the list called fits. VARIABLES specifies the categorical variables in the model (e.g. C_AB2).

for (MOD in 1: length(MODELS)) {
  fits$fit <- runMI(MODELS[[MOD]], 
                    data=IMPUTE,
                    fun="cfa",
                    ordered= VARIABLES)
  names(fits)[length(fits)] <- paste0("M",MOD)
}

Without imputations, this general set up (e.g. loop) seems to have worked fine. With the imputations, however, I nw run into problems. The loop spits out the following error message:

"D3" only available using maximum likelihood estimation. Changed test to "D2".
Robust corrections are made by pooling the naive chi-squared statistic across 5 imputations for which the model converged, then applying the average (across imputations) scaling factor and shift parameter to that pooled value. 
To instead pool the robust test statistics, set test = "D2" and pool.robust = TRUE. 

and 
 
Error in getMethod("resid", "lavaan.mi") : 
  no method found for function 'resid' and signature lavaan.mi
In addition: Warning messages:
  1: In pchisq(X2, DF, ncp = N * DF * 0.05^2/nG, lower.tail = FALSE) :
  full precision may not have been achieved in 'pnchisq'
2: In pchisq(X2.sc, DF, ncp = N * DF * 0.05^2/nG, lower.tail = FALSE) :
  full precision may not have been achieved in 'pnchisq'

The first error message has continued to emerge for seevral iterations of my attempts to make it work. In fact, it made me look up solutions in this group amongst others. The second one is rather new and I could not find anything about it. Any help much appreciated. I also include the session info:

> sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

Matrix products: default

locale:
  [1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
  [1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
  [1] mice_3.13.0        robustHD_0.6.1     perry_0.2.0        robustbase_0.93-6  ggplot2_3.3.0     
[6] semTools_0.5-4.904 lavaan_0.6-8       psych_1.9.12.31    moments_0.14       readxl_1.3.1      

loaded via a namespace (and not attached):
  [1] Rcpp_1.0.3        lattice_0.20-38   tidyr_1.0.3       prettyunits_1.1.1 ps_1.3.3         
[6] assertthat_0.2.1  rprojroot_1.3-2   R6_2.4.1          cellranger_1.1.0  backports_1.1.6  
[11] stats4_3.6.2      pillar_1.4.4      rlang_0.4.10      curl_4.3          rstudioapi_0.11  
[16] callr_3.5.1       pbivnorm_0.6.0    desc_1.2.0        devtools_2.3.2    munsell_0.5.0    
[21] broom_0.5.6       compiler_3.6.2    pkgconfig_2.0.3   mnormt_2.0.2      pkgbuild_1.2.0   
[26] tmvnsim_1.0-2     nnet_7.3-12       tidyselect_1.0.0  tibble_3.0.1      fansi_0.4.1      
[31] crayon_1.3.4      dplyr_0.8.5       withr_2.4.1       MASS_7.3-51.5     grid_3.6.2       
[36] nlme_3.1-142      gtable_0.3.0      lifecycle_0.2.0   magrittr_1.5      scales_1.1.1     
[41] cli_2.0.2         cachem_1.0.1      fs_1.5.0          remotes_2.2.0     testthat_2.3.2   
[46] ellipsis_0.3.1    generics_0.0.2    vctrs_0.2.4       tools_3.6.2       glue_1.4.0       
[51] DEoptimR_1.0-8    purrr_0.3.4       processx_3.4.5    pkgload_1.1.0     fastmap_1.1.0    
[56] colorspace_1.4-1  sessioninfo_1.1.1 memoise_2.0.0     usethis_2.0.0 


Thanks,

Dom

 





Domib

unread,
Feb 5, 2021, 12:21:37 AM2/5/21
to lavaan
Hi all,

I think I solved the problem. I notived that devtools also requires the package 'testthat', which I had not downloaded. Since this was done, it all seems to work fine. I am now a very happy analyst!

Best,

Dom 

Domib

unread,
Feb 9, 2021, 5:43:29 AM2/9/21
to lavaan
Hi all,

This might be a question for Terrence Jorgensen (I hope this is not against any etiquette if I address him directly). The previous error message resurfaced (see my first email in this thread). After having run my model using the multiply imputed data set I continued my work in R. Today, out of the blue, I was rerunning my model and then again encounterd the problem:
 
Error in getMethod("resid", "lavaan.mi") : 
  no method found for function 'resid' and signature lavaan.mi
In addition: Warning messages:
  1: In pchisq(X2, DF, ncp = N * DF * 0.05^2/nG, lower.tail = FALSE) :
  full precision may not have been achieved in 'pnchisq'
2: In pchisq(X2.sc, DF, ncp = N * DF * 0.05^2/nG, lower.tail = FALSE) :
  full precision may not have been achieved in 'pnchisq'

I then followed your advice given to someone else. I closed the session, opened it again, re-nstalled semToolsm closed the session to open it again. I then just loaded the lavaan and the semTools package. I was then able to run the model again. I assume one of the packages previously loaded must have clashed with semTools? Is there any conflict one knows of?

Thanks,

Dom  

Terrence Jorgensen

unread,
Feb 9, 2021, 5:49:09 PM2/9/21
to lavaan
I assume one of the packages previously loaded must have clashed with semTools? Is there any conflict one knows of?

That is what I assumed, but could never find out what the specific problem was for the few people this happened to. 

Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam
 

Domib

unread,
Feb 9, 2021, 11:29:20 PM2/9/21
to lavaan
Hi Terrence,

Thanks for the reply. This morning the problme occured again. Super frustrating. However, I found a potential solution proposed by you some time ago that seem to have worked again. For the record I post it here again. May it be of help to others as well:

It looks like you are using RStudio, which has default settings to automatically save your workspace when you shutdown RStudio, then load it automatically when you start RStudio in the same directory.  That can have unintended negative consequences, such as loading the same namespaces that were loaded before (sometimes preventing proper re-installation, or loading an irrelevant package that also has a coef method that is competing with semTools for some reason).

Click on Tools --> Global options... , and in the General tab, do 2 things:
  1. uncheck the box that says "Restore .RData into workspace at startup
  2. Choose "Never" in the drop-down menu next to "Save workspace to .RData on exit"
Then shut down and restart RStudio.  You can delete the (probably hidden) .RData file from your directory, if you feel like it -- it serves no purpose unless you want to save something specific, which should be in your R scripts rather than something automatic that RStudio does without asking you first.

When you have restarted RStudio, run sessionInfo() first thing to make sure nothing is attached except the base packages:

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

Then run library(semTools), which will automatically load lavaan. 

Best,

Dom 

Terrence Jorgensen

unread,
Feb 10, 2021, 4:02:29 PM2/10/21
to lavaan
Thanks.  Did you happen to compare sessionInfo() to see what was getting automatically loaded that might have interfered with the semTools path?

Domib

unread,
Feb 12, 2021, 12:31:25 PM2/12/21
to lavaan
Hi Terrence,

Unfortunately, I did not do this. I am afraid I will not be of any help! Dom
Reply all
Reply to author
Forward
0 new messages