simsem / semTools

Useful tools for structural equation modeling
74 stars 35 forks source link

Error with summary(... standardized=T) for runMI lavaan model. #94

Closed lisa-sk closed 2 years ago

lisa-sk commented 3 years ago

Intro

A month or two ago, I had a similar issue as #43. I used the proposed solutions of updating the packages from there:

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

install.packages("devtools")
devtools::install_github("simsem/semTools/semTools")

This seemed to have fixed the problem then, but re-running my scripts now does not work.

Problem setup

Unfortunately I cannot share my dataset, but this is the script that I run to fit my model:

library(mice)
library(lavaan) # SEM model
library(semTools) # To integrate mice
library(devtools)

#Mice imputation
Dataset_imputed <- mice(Dataset_preimp, method = "pmm", maxit = 50, m = 30, seed = 2019)

model1 <- '

#regressions 
A_at_time_2~ A_at_time_1+ B_at_time_1
B_at_time_2~ B_at_time_1+ A_at_time_1

#confounders
B_at_time_1~ age + sex + education 
A_at_time_1~ age + sex + education 

#covariances
B_at_time_1~~ A_at_time_1
B_at_time_2~~ A_at_time_2
'

mymodelfit_pre1imp<- runMI(model1, data = Dataset_imputed , fun = "sem", estimator = "WLSMV", 
                           std.lv = T, se = "robust.sem", test = "scaled.shifted",
                           ordered = c("A_at_time_1", "A_at_time_2"))

Then, I want to show a standardized summary, but this fails:

> summary(mymodelfit_pre1imp, standardized=T, rsq=T)
Error in .local(object, ...) : 
  unused arguments (standardized = TRUE, rsq = TRUE)

Based on another thread, I found that fitmeasures should solve this problem, but it seems it does not for me:

> fitmeasures(mymodelfit_pre1imp)
"D3" only available using maximum likelihood estimation. Changed test to "D2".
Robust corrections are made by pooling the naive chi-squared statistic across 30 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. 

Error in getMethod("resid", "lavaan.mi") : 
  no method found for function 'resid' and signature lavaan.mi
> inspect(mymodelfit_pre1imp, "fit")
Error in lavListInspect(object = object, what = what, add.labels = TRUE,  : 
  unknown `what' argument in inspect function: `fit'

Note, summary(model1) does work as expected, and the model converged:

Convergence information:
The model converged on 30 imputed data sets 

Environment

This is my output of sessionInfo:

> sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

Random number generation:
 RNG:     Mersenne-Twister 
 Normal:  Inversion 
 Sample:  Rounding 

locale:
[1] LC_COLLATE=Dutch_Netherlands.1252  LC_CTYPE=Dutch_Netherlands.1252    LC_MONETARY=Dutch_Netherlands.1252 LC_NUMERIC=C                      
[5] LC_TIME=Dutch_Netherlands.1252    

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

other attached packages:
[1] devtools_2.4.2 usethis_2.0.1  semTools_0.5-4 lavaan_0.6-8   mice_3.13.0    lme4_1.1-26    Matrix_1.3-3  

loaded via a namespace (and not attached):
 [1] statmod_1.4.35    tidyselect_1.1.1  remotes_2.3.0     purrr_0.3.4       splines_4.1.0     lattice_0.20-44   testthat_3.0.2   
 [8] colorspace_2.0-0  vctrs_0.3.6       generics_0.1.0    stats4_4.1.0      yaml_2.2.1        utf8_1.1.4        rlang_0.4.10     
[15] pkgbuild_1.2.0    pillar_1.5.1      nloptr_1.2.2.2    withr_2.4.2       glue_1.4.2        DBI_1.1.1         sessioninfo_1.1.1
[22] lifecycle_1.0.0   munsell_0.5.0     gtable_0.3.0      memoise_2.0.0     callr_3.7.0       fastmap_1.1.0     ps_1.6.0         
[29] parallel_4.1.0    fansi_0.4.2       broom_0.7.6       Rcpp_1.0.6        scales_1.1.1      backports_1.2.1   cachem_1.0.4     
[36] desc_1.3.0        pkgload_1.2.1     tmvnsim_1.0-2     fs_1.5.0          mnormt_2.0.2      ggplot2_3.3.3     processx_3.5.0   
[43] dplyr_1.0.5       rprojroot_2.0.2   grid_4.1.0        cli_2.5.0         tools_4.1.0       magrittr_2.0.1    tibble_3.1.0     
[50] crayon_1.4.1      tidyr_1.1.3       pbivnorm_0.6.0    pkgconfig_2.0.3   MASS_7.3-54       ellipsis_0.3.1    prettyunits_1.1.1
[57] assertthat_0.2.1  minqa_1.2.4       R6_2.5.0          boot_1.3-28       nlme_3.1-152      compiler_4.1.0   
lisa-sk commented 3 years ago

I may have found the problem (not solved yet):

There seems to be an issue with downloading the development version of semTools :

devtools::install_github("simsem/semTools/semTools")

WARNING: Rtools is required to build R packages, but no version of Rtools compatible with R 4.1.0 was found. (Only the following incompatible version(s) of Rtools were found:3.4)

Please download and install Rtools 4.0 from https://cran.r-project.org/bin/windows/Rtools/.
Downloading GitHub repo simsem/semTools@HEAD
These packages have more recent versions available.
It is recommended to update all of them.
Which would you like to update?

1: All                           
2: CRAN packages only            
3: None                          
4: lavaan (0.6-8 -> 0.6-9) [CRAN]

Enter one or more numbers, or an empty line to skip updates: 

WARNING: Rtools is required to build R packages, but no version of Rtools compatible with R 4.1.0 was found. (Only the following incompatible version(s) of Rtools were found:3.4)

Please download and install Rtools 4.0 from https://cran.r-project.org/bin/windows/Rtools/.
   Warning message:
   In normalizePath(path.expand(path), winslash, mustWork) :
     path[5]="V:/MEDEWE~1/040814~1.R/R/R-41~1.0/library": Het systeem kan het opgegeven pad niet vinden
   Warning message:
   package "methods" in options("defaultPackages") was not found 
   Warning messages:
   1: In normalizePath(path.expand(path), winslash, mustWork) :
     path[3]="V:/MEDEWE~1/040814~1.R/R/R-41~1.0/library": Het systeem kan het opgegeven pad niet vinden
   2: In normalizePath(path.expand(path), winslash, mustWork) :
     path[4]="V:/MEDEWE~1/040814~1.R/R/R-41~1.0/library": Het systeem kan het opgegeven pad niet vinden
   Warning message:
   In normalizePath(path.expand(path), winslash, mustWork) :
     path[3]="V:/MEDEWE~1/040814~1.R/R/R-41~1.0/library": Het systeem kan het opgegeven pad niet vinden
   Warning message:
   In normalizePath(path.expand(path), winslash, mustWork) :
     path[3]="V:/MEDEWE~1/040814~1.R/R/R-41~1.0/library": Het systeem kan het opgegeven pad niet vinden
   During startup - Warning messages:
   1: package 'datasets' in options("defaultPackages") was not found 
   2: package 'utils' in options("defaultPackages") was not found 
   3: package 'grDevices' in options("defaultPackages") was not found 
   4: package 'graphics' in options("defaultPackages") was not found 
   5: package 'stats' in options("defaultPackages") was not found 
   6: package 'methods' in options("defaultPackages") was not found 
   Error in normalizePath(path.expand(path), winslash, mustWork) : 
     path[1]="V:/MEDEWE~1/040814~1.R/R/R-41~1.0/library/compiler": Het systeem kan het opgegeven pad niet vinden
   Calls: getNamespace ... loadNamespace -> makeNamespace -> setNamespaceInfo -> normalizePath
   Execution halted
Error: Failed to install 'semTools' from GitHub:
  System command 'Rcmd.exe' failed, exit status: 1, stdout + stderr (last 10 lines):
E> 1: package 'datasets' in options("defaultPackages") was not found 
E> 2: package 'utils' in options("defaultPackages") was not found 
E> 3: package 'grDevices' in options("defaultPackages") was not found 
E> 4: package 'graphics' in options("defaultPackages") was not found 
E> 5: package 'stats' in options("defaultPackages") was not found 
E> 6: package 'methods' in options("defaultPackages") was not found 
E> Error in normalizePath(path.expand(path), winslash, mustWork) : 
E>   path[1]="V:/MEDEWE~1/040814~1.R/R/R-41~1.0/library/compiler": Het systeem kan het opgegeven pad niet vinden
E> Calls: getNamespace ... loadNamespace -> makeNamespace -> setNamespaceInfo -> normalizePath
E> Execution halted
In addition: Warning messages:
1: In normalizePath(path.expand(path), winslash, mustWork) :
  path[2]="V:/MEDEWE~1/040814~1.R/R/R-41~1.0/library": Het systeem kan het opgegeven pad niet vinden
2: In normalizePath(path.expand(path), winslash, mustWork) :
  path[3]="V:/MEDEWE~1/040814~1.R/R/R-41~1.0/library": Het systeem kan het opgegeven pad niet vinden

I do have Rtools version 4.0 installed, but R does not seem to find it.

TDJorgensen commented 2 years ago

I hope you were able to resolve your issue with Rtools so you can use the devtools package. Since this is not an issue with semTools, I will close this issue. For what it is worth, semTools 0.5-5 is on its way to CRAN now, so you will be able to have the past 6 months of progress from there soon.