simsem / semTools

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

capture output of measurementInvariance() #31

Closed wepelham3 closed 6 years ago

wepelham3 commented 6 years ago

Is there an existing way to capture the summary output that gets printed to the console when you run measurementInvariance()? I'm referring to this...

Measurement invariance models:

Model 1 : fit.configural
Model 2 : fit.loadings
Model 3 : fit.intercepts
Model 4 : fit.means

Chi Square Difference Test

               Df    AIC    BIC  Chisq Chisq diff Df diff Pr(>Chisq)    
fit.configural 48 7484.4 7706.8 115.85                                  
fit.loadings   54 7480.6 7680.8 124.04      8.192       6     0.2244    
fit.intercepts 60 7508.6 7686.6 164.10     40.059       6  4.435e-07 ***
fit.means      63 7543.1 7710.0 204.61     40.502       3  8.338e-09 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Fit measures:

                 cfi rmsea cfi.delta rmsea.delta
fit.configural 0.923 0.097        NA          NA
fit.loadings   0.921 0.093     0.002       0.004
fit.intercepts 0.882 0.107     0.038       0.015
fit.means      0.840 0.122     0.042       0.015`

Use case would be, e.g., you're testing invariance across twenty different group variables and would like to be able to easily sort and manipulate the resulting chi square difference tests.

You might try something like this...

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

obj <- measurementInvariance(HW.model, data=HolzingerSwineford1939, group="school")

summary(obj)

...except that summary(obj) is not the summary table that gets printed when you run measurementInvariance, but a table of the fits used in producing that summary table.

Right now I resort to sending the output of measurementInvariance() to a text file and manually parsing it back into a dataframe.

Would be nice to have a function that takes the obj and returns the chi square test of differences / delta fit measures as a dataframe.

TDJorgensen commented 6 years ago

There is already a hidden printInvarianceResult() function that prints the tables to the screen, but it never returned anything. I updated it to return the table of anova() output, or a list with both anova() and fitMeasures() output if the fit.measures= argument is non-NULL.

foo <- semTools:::printInvarianceResult(out, fit.measures = c("cfi", "rmsea"), method = "satorra.bentler.2001")
str(foo)
foo$anova
foo$fitMeasures

This will remain a hidden function (hence the semTools:::) because I plan to massively update the measurementInvariance() suite of functions in the future.