simsem / semTools

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

Error: `$ operator is invalid for atomic vectors` when using `permuteMeasEq()` #105

Closed isaactpetersen closed 2 years ago

isaactpetersen commented 2 years ago

I'm receiving the following error when trying to use permuteMeasEq(): Error: $ operator is invalid for atomic vectors

I'm using semTools_0.5-5 on a Linux cluster. Below is the code:

library("lavaan")
library("semTools")

myAFIs <- c("chisq", "chisq.scaled",
            "rmsea", "cfi", "tli", "srmr",
            "rmsea.robust", "cfi.robust", "tli.robust")
moreAFIs <- NULL

cfaModel <- '
 #Factor loadings
 visual  =~ x1 + x2 + x3
 textual =~ x4 + x5 + x6
 speed   =~ x7 + x8 + x9
'

configuralInvarianceModel <- measEq.syntax(configural.model = cfaModel,
                                           data = HolzingerSwineford1939,
                                           ID.fac = "std.lv",
                                           group = "school")

configuralInvarianceSyntax <- as.character(configuralInvarianceModel)

configuralInvarianceModel_fit <- cfa(configuralInvarianceSyntax,
                                     data = HolzingerSwineford1939,
                                     group = "school",
                                     missing = "ML",
                                     estimator = "MLR")

numPermutations <- 500
set.seed(52242)
configuralInvarianceTest <- permuteMeasEq(nPermute = numPermutations,
                                          modelType = "mgcfa",
                                          con = configuralInvarianceModel_fit,
                                          uncon = NULL,
                                          AFIs = myAFIs,
                                          moreAFIs = moreAFIs,
                                          parallelType = "multicore",
                                          iseed = 52242)

Below is my sessionInfo():

sessionInfo()
## R version 4.1.2 (2021-11-01)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.3 LTS
## 
## Matrix products: default
## BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=C             
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
##  [1] grid      stats4    splines   stats     graphics  grDevices utils    
##  [8] datasets  methods   base     
## 
## other attached packages:
##  [1] mirtCAT_1.11            shiny_1.7.1             dmacs_0.1.0            
##  [4] dagitty_0.3-1           nFactors_2.4.1          corrplot_0.92          
##  [7] msir_1.3.3              ggpubr_0.4.0            gridExtra_2.3          
## [10] uroc_0.1.0              PredictABEL_1.2-4       ResourceSelection_0.3-5
## [13] rms_6.2-0               Hmisc_4.6-0             Formula_1.2-4          
## [16] survival_3.2-13         ROCR_1.0-11             pROC_1.18.0            
## [19] magrittr_2.0.1          mirt_1.34               lattice_0.20-45        
## [22] drc_3.0-1               semTools_0.5-5          quantreg_5.86          
## [25] SparseM_1.81            mice_3.14.0             snow_0.4-4             
## [28] simsem_0.5-16           DT_0.20                 MASS_7.3-54            
## [31] kableExtra_1.3.4        rockchalk_1.8.144       semPlot_1.1.2          
## [34] MOTE_1.0.2              here_1.0.1              gtheory_0.1.2          
## [37] lme4_1.1-27.1           Matrix_1.3-4            irrCAC_1.0             
## [40] psychmeta_2.6.0         lavaan_0.6-9            MBESS_4.8.1            
## [43] blandr_0.5.1            psych_2.1.9             viridis_0.6.2          
## [46] viridisLite_0.4.0       bookdown_0.24           rmarkdown_2.11         
## [49] knitr_1.36              tinytex_0.35            forcats_0.5.1          
## [52] stringr_1.4.0           purrr_0.3.4             readr_2.1.0            
## [55] tidyr_1.1.4             tibble_3.1.6            ggplot2_3.3.5          
## [58] tidyverse_1.3.1         dplyr_1.0.7            
## 
## loaded via a namespace (and not attached):
##   [1] ModelMetrics_1.2.2.2  coda_0.19-4           bit64_4.0.5          
##   [4] multcomp_1.4-17       data.table_1.14.2     rpart_4.1-15         
##   [7] generics_0.1.1        TH.data_1.1-0         polspline_1.1.19     
##  [10] future_1.23.0         bit_4.0.4             tzdb_0.2.0           
##  [13] httpuv_1.6.3          webshot_0.5.2         xml2_1.3.2           
##  [16] lubridate_1.8.0       assertthat_0.2.1      gower_0.2.2          
##  [19] xfun_0.28             hms_1.1.1             jquerylib_0.1.4      
##  [22] promises_1.2.0.1      evaluate_0.14         fansi_0.5.0          
##  [25] progress_1.2.2        dbplyr_2.1.1          readxl_1.3.1         
##  [28] igraph_1.2.9          DBI_1.1.1             tmvnsim_1.0-2        
##  [31] Rsolnp_1.16           htmlwidgets_1.5.4     reshape_0.8.8        
##  [34] ellipsis_0.3.2        backports_1.4.0       V8_3.6.0             
##  [37] pbivnorm_0.6.0        permute_0.9-5         RcppParallel_5.1.4   
##  [40] jmvcore_2.2           vctrs_0.3.8           abind_1.4-5          
##  [43] caret_6.0-90          withr_2.4.2           PBSmodelling_2.68.8  
##  [46] checkmate_2.0.0       vroom_1.5.6           vegan_2.5-7          
##  [49] fdrtool_1.2.17        prettyunits_1.1.1     mclust_5.4.8         
##  [52] mnormt_2.0.2          svglite_2.0.0         cluster_2.1.2        
##  [55] mi_1.0                crayon_1.4.2          recipes_0.1.17       
##  [58] pkgconfig_2.0.3       labeling_0.4.2        nlme_3.1-153         
##  [61] nnet_7.3-16           rlang_0.4.12          globals_0.14.0       
##  [64] lifecycle_1.0.1       MatrixModels_0.5-0    sandwich_3.0-1       
##  [67] kutils_1.70           modelr_0.1.8          cellranger_1.1.0     
##  [70] tcltk_4.1.2           rprojroot_2.0.2       matrixStats_0.61.0   
##  [73] regsem_1.8.0          carData_3.0-4         boot_1.3-28          
##  [76] zoo_1.8-9             reprex_2.0.1          base64enc_0.1-3      
##  [79] png_0.1-7             arm_1.12-2            parallelly_1.29.0    
##  [82] rstatix_0.7.0         jpeg_0.1-9            ggsignif_0.6.3       
##  [85] lpSolve_5.6.15        scales_1.1.1          plyr_1.8.6           
##  [88] compiler_4.1.2        RColorBrewer_1.1-2    plotrix_3.8-2        
##  [91] cli_3.1.0             listenv_0.8.0         pbapply_1.5-0        
##  [94] htmlTable_2.3.0       mgcv_1.8-38           tidyselect_1.1.1     
##  [97] stringi_1.7.5         lisrelToR_0.1.4       sem_3.1-13           
## [100] highr_0.9             yaml_2.2.1            OpenMx_2.19.8        
## [103] latticeExtra_0.6-29   sass_0.4.0            polynom_1.4-0        
## [106] animation_2.7         tools_4.1.2           future.apply_1.8.1   
## [109] parallel_4.1.2        rstudioapi_0.13       foreach_1.5.1        
## [112] foreign_0.8-81        GPArotation_2014.11-1 prodlim_2019.11.13   
## [115] farver_2.1.0          digest_0.6.28         lava_1.6.10          
## [118] Rcpp_1.0.7            car_3.0-12            broom_0.7.10         
## [121] ez_4.4-0              later_1.3.0           httr_1.4.2           
## [124] Deriv_4.1.3           colorspace_2.0-2      rvest_1.0.2          
## [127] XML_3.99-0.8          fs_1.5.0              truncnorm_1.0-8      
## [130] conquer_1.2.1         systemfonts_1.0.3     xtable_1.8-4         
## [133] jsonlite_1.7.2        nloptr_1.2.2.3        corpcor_1.6.10       
## [136] timeDate_3043.102     glasso_1.11           dcurver_0.9.2        
## [139] ipred_0.9-12          R6_2.5.1              mime_0.12            
## [142] pillar_1.6.4          htmltools_0.5.2       glue_1.5.0           
## [145] fastmap_1.1.0         minqa_1.2.4           class_7.3-19         
## [148] codetools_0.2-18      mvtnorm_1.1-3         utf8_1.2.2           
## [151] bslib_0.3.1           curl_4.3.2            gtools_3.9.2         
## [154] zip_2.2.0             openxlsx_4.2.4        qgraph_1.9           
## [157] munsell_0.5.0         iterators_1.0.13      haven_2.4.3          
## [160] reshape2_1.4.4        gtable_0.3.0
TDJorgensen commented 2 years ago

Thanks for the reprex, it helped me verify this had nothing to do with conflicts between semTools and the mountain of other packages in your path. I found the same problem after loading only semTools (which already loads lavaan) and excluding the unnecessary (in this case) missing= argument. Using debug(), I found that there was no problem running parallel::mclapply(), but it returned more informative errors for each node:

[1] Error in get(as.character(FUN), mode = "function", envir = envir):
object 'permuteOnce.mgcfa' of mode 'function' was not found

I have been unable to locate a NEWS file stating a change to whether mclapply(FUN=) can accept a character string naming a function (as lapply(FUN=) and parallel::parLapply() still can), but I swear I have used permuteMeasEq() with parallel="multicore" on my own computer multiple times in the past. Currently, the mclapply() help page simply states it must be a function, stating nothing about (back)quoted names. So I have updated permuteMeasEq() to convert the paste()d string to as.name() before passing it to mclapply(), and that works on your reprex. Hopefully, you have no problem installing the development version of semTools on your cluster.

isaactpetersen commented 2 years ago

Wonderful, thanks very much for your help with that, Terrence! Much appreciated.