rr-lavaan

How can I access specific values of a lavaan model by code?


I have to run many CFAs and want to automate saving specific output values in a data frame so I can convert it to a latex table later.

Specifically I get my output something like this using lavaan:

model <- 'y =~ x1 + x2 + x3'
fit <- cfa(model, data)
sum <- summary(fit_os_bi, fit.measures=TRUE, standardized=T)

I managed to extract some values like this p_val <- sum$test$standard$pvalue but I couldn't figure out how to get to CFI, TLI, RMSEA, and SRMR. I think I'm even missing the right search terms to google that problem successfully.

How can I access these values from the summary-object? I'd be grateful if you could provide me with the right code or point me to a resource that explains it!

Here's an excerpt of the cfa summary:

lavaan 0.6-12 ended normally after 42 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of model parameters                         9

  Number of observations                           213

Model Test User Model:
                                                      
  Test statistic                                 1.625
  Degrees of freedom                                 1
  P-value (Chi-square)                           0.202

User Model versus Baseline Model:

  Comparative Fit Index (CFI)                    0.997
  Tucker-Lewis Index (TLI)                       0.983

Root Mean Square Error of Approximation:

  RMSEA                                          0.054
  90 Percent confidence interval - lower         0.000
  90 Percent confidence interval - upper         0.200
  P-value RMSEA <= 0.05                          0.315

Standardized Root Mean Square Residual:

  SRMR                                           0.014

I looked at 'sum' in the environment inspector in R-Studio (where I found the location of the p-value) and searched the documentation of lavaan, but to no avail.

Since the values I'm looking for appear in the output I expect they must be stored somewhere in the summary-object.


Solution

  • lavaan has a number of helper functions to extract coefficients from the model object. In this case you can use fitMeasures():

    fitMeasures(fit, c("pvalue", "cfi", "tli", "rmsea","srmr"))