Home > Back-end >  How can I access specific values of a lavaan model by code?
How can I access specific values of a lavaan model by code?

Time:01-27

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.

CodePudding user response:

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"))

CodePudding user response:

Use broom::tidy() and broom::glance() to get model information in a data.frame object. I will use the standard example from lavaan package

library(lavaan)
#> This is lavaan 0.6-12
#> lavaan is FREE software! Please report any bugs.

HS.model <- ' visual  =~ x1   x2   x3
              textual =~ x4   x5   x6
              speed   =~ x7   x8   x9 '

fit <- cfa(HS.model, data = HolzingerSwineford1939)


broom::tidy(fit)
#> # A tibble: 24 × 9
#>    term          op    estimate std.e…¹ stati…²   p.value std.lv std.all std.nox
#>    <chr>         <chr>    <dbl>   <dbl>   <dbl>     <dbl>  <dbl>   <dbl>   <dbl>
#>  1 visual =~ x1  =~       1      0        NA    NA         0.900   0.772   0.772
#>  2 visual =~ x2  =~       0.554  0.0997    5.55  2.80e- 8  0.498   0.424   0.424
#>  3 visual =~ x3  =~       0.729  0.109     6.68  2.31e-11  0.656   0.581   0.581
#>  4 textual =~ x4 =~       1      0        NA    NA         0.990   0.852   0.852
#>  5 textual =~ x5 =~       1.11   0.0654   17.0   0         1.10    0.855   0.855
#>  6 textual =~ x6 =~       0.926  0.0554   16.7   0         0.917   0.838   0.838
#>  7 speed =~ x7   =~       1      0        NA    NA         0.619   0.570   0.570
#>  8 speed =~ x8   =~       1.18   0.165     7.15  8.56e-13  0.731   0.723   0.723
#>  9 speed =~ x9   =~       1.08   0.151     7.15  8.40e-13  0.670   0.665   0.665
#> 10 x1 ~~ x1      ~~       0.549  0.114     4.83  1.34e- 6  0.549   0.404   0.404
#> # … with 14 more rows, and abbreviated variable names ¹​std.error, ²​statistic

broom::glance(fit)
#> # A tibble: 1 × 17
#>    agfi   AIC   BIC   cfi chisq  npar  rmsea rmsea.conf.h…¹   srmr   tli conve…²
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>          <dbl>  <dbl> <dbl> <lgl>  
#> 1 0.894 7517. 7595. 0.931  85.3    21 0.0921          0.114 0.0652 0.896 TRUE   
#> # … with 6 more variables: estimator <chr>, ngroups <int>,
#> #   missing_method <chr>, nobs <int>, norig <int>, nexcluded <int>, and
#> #   abbreviated variable names ¹​rmsea.conf.high, ²​converged

Created on 2023-01-26 with reprex v2.0.2
  • Related