Error when trying to knit related to lavaan

53 views Asked by At

I'm getting this error message when trying to knit. Any suggestions?

Quitting from lines 25-90 [unnamed-chunk-1] (Week-4-Assignment_KK.Rmd)

Error in if (categorical.flag) ...: ! argument is of length zero Backtrace:

  1. rmarkdown::render(...)
  2. knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
  3. knitr:::process_file(text, output)
  4. knitr:::process_group.block(group)
  5. knitr:::call_block(x) ...
  6. evaluate:::evaluate_call(...)
  7. knitr (local) value_fun(ev$value, ev$visible)
  8. knitr (local) fun(x, options = options)
  9. rmarkdown:::knit_print.data.frame(x, ...)
  10. lavaan:::print.lavaan.parameterEstimates(x) Execution halted

This is the code in the chunk where it stops:

options(repos = "https://cloud.r-project.org/")
install.packages("psych")
install.packages("semTools")
install.packages("Amelia")
install.packages("lavaan")
library(psych)
library(semTools)
library(Amelia)
library(lavaan)
data(bfi)

edu3 <- bfi[which(bfi$education == 3),] #only include people with edu = 3
bfi_new <- edu3[, 1:25] #removes all the non BFI item

#define model and order ordinal variables
cfa_mod<-'
F1=~E1+E2+E3+E4+E5
F2=~N1+N2+N3+N4+N5
F3=~C1+C2+C3+C4+C5
F4=~O1+O2+O3+O4+O5
F5=~A1+A2+A3+A4+A5
'

#do MI after specify SE to be robust and specify ordinal data
out <- cfa.mi(cfa_mod, data = bfi_new, m = 20, seed = 712, se="robust",
               miArgs = list(ordered=c("E1","E2","E3","E4","E5",
                                       "N1","N2","N3","N4","N5",
                                       "O1","O2","O3","O4","O5",
                                       "C1","C2","C3","C4","C5",
                                       "A1","A2","A3","A4","A5")))

#run CFA
summary(out,fit=T, pool = TRUE)
fit <- cfa(cfa_mod, bfi_new)
summary(fit,standardize=T) 

  
  #RMSEA                                          0.076
      #this RSMEA indicates good model fit
  #Confidence interval - lower                    0.073
      #upper bound should ideally be <.05 to indicate good fit, this does not indicate good        fit
  #Confidence interval - upper                    0.079
      #upper bound should ideally be <.08 to indicate good fit, so this is a good fit
  #P-value H_0: RMSEA <= 0.05                     0.000
  #SRMR                                           0.076
      #Since the value is <.08, the model demonstrates acceptable fit
  #Comparative Fit Index (CFI)                    0.773
    #CFI should ideally be >.9, so this does not indicate strong model fit
  #Tucker-Lewis Index (TLI)                       0.743
    #TLI should ideally be >.9, so this does not indicate strong model fit

  #Model test
    #Test statistic                              2194.264
    #Degrees of freedom                               265
    #P-value                                        0.000

  #Baseline test
    #Test statistic                              8803.497
    #Degrees of freedom                               300
    #P-value                                        0.000
      

    #The lower test in the model tests indicates the model is a better fit than the baseline test. P<.001 indicates a statistically significant result in both tests. 35 cases were removed due to missing data.


I tried adding the first line above installing packages, uninstalled and reinstalled the packages, restarted R multiple times. Still getting the same error.

1

There are 1 answers

2
Terrence On

That error is related to 2 new attributes() that lavaan added to its custom data.frame class for its parameterEstimates(), which has been incorporated into the development version of semTools. You can install that using the remotes package:

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