yrosseel / lavaan

an R package for structural equation modeling and more
http://lavaan.org
435 stars 99 forks source link

`fitMeasures` does not return some fit measures even if specified in the argument `fit.measures` #221

Closed sfcheung closed 2 years ago

sfcheung commented 3 years ago

I am using version in version 0.6-9. I believe this is bug because the argument fit.measures of the function fitMeasures does not behave as it is documented.

This is the example from the help page of fitMeasures:

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

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

This is the output:

> fitMeasures(fit)
               npar                fmin               chisq                  df              pvalue 
             21.000               0.142              85.306              24.000               0.000 
     baseline.chisq         baseline.df     baseline.pvalue                 cfi                 tli 
            918.852              36.000               0.000               0.931               0.896 
               nnfi                 rfi                 nfi                pnfi                 ifi 
              0.896               0.861               0.907               0.605               0.931 
                rni                logl   unrestricted.logl                 aic                 bic 
              0.931           -3737.745           -3695.092            7517.490            7595.339 
             ntotal                bic2               rmsea      rmsea.ci.lower      rmsea.ci.upper 
            301.000            7528.739               0.092               0.071               0.114 
       rmsea.pvalue                 rmr          rmr_nomean                srmr        srmr_bentler 
              0.001               0.082               0.082               0.065               0.065 
srmr_bentler_nomean                crmr         crmr_nomean          srmr_mplus   srmr_mplus_nomean 
              0.065               0.073               0.073               0.065               0.065 
              cn_05               cn_01                 gfi                agfi                pgfi 
            129.490             152.654               0.943               0.894               0.503 
                mfi                ecvi 
              0.903               0.423 

However, some fit measures are not returned if requested. For example, "srmr_bentler" and "crmr":

fitMeasures(fit, "srmr_bentler")
fitMeasures(fit, "crmr")

They return a numeric vector of zero length:

str(fitMeasures(fit, "crmr"))
# num(0) 

This is a minor bug because there is a simple workaround:

fitMeasures(fit)["crmr"]
#      crmr 
# 0.07290147 
fitMeasures(fit)["srmr_bentler"]
#srmr_bentler 
#  0.06520506 

I haven't checked other measures. For measures of the SRMR/RMR family, only "srmr" and "rmr"are checked:

https://github.com/yrosseel/lavaan/blob/de141af743b37d45c7567d08e72f199e5d8ed882/R/lav_fit_measures.R#L1051

For measures of the SRMR/RMR family, perhaps it can be fixed by changing the above line to:

    if(any(fit.srmr2 %in% fit.measures) &&  object@Data@nlevels == 1L) {

fit.srms2 is defined here:

https://github.com/yrosseel/lavaan/blob/de141af743b37d45c7567d08e72f199e5d8ed882/R/lav_fit_measures.R#L187-L206

I haven't checked other measures but I think only a few other groups of measures are affected, e.g., the RMSEA family, listed in fit.rmsea. I believe they can be fixed in a similar way if they have the same problem.

My two cents.

yrosseel commented 2 years ago

True indeed. You need to include the 'main' fit index in the list, if you wish to extract the 'derived' fit indices (in many cases).

I could fix this, but I prefer to first rewrite the fitMeasures() function. In the new version, fitMeasures() will call other functions per family (a function for RMSEA and friends, lavResiduals() for the SRMR, and so on).

yrosseel commented 2 years ago

The fitMeasures() function has now been rewritten, and all requested fit measures should now be returned in dev 0.6-13.