simsem / semTools

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

moreFitIndices doesnt calculate scaled indices with WLS #32

Closed maugavilla closed 6 years ago

maugavilla commented 6 years ago

I noticed that moreFitIndices doesnt calculate the scales versions when used with WLS, found that is due to the line of code

if (lavInspect(object, "options")$test %in% c("satorra.bentler", "yuan.bentler"))

While with WLS the test is "scaled.shifted"

Here is my edit to moreFitIndices by adding that test to calculate the scale one

Also, when I ask for "all", it gives this error, which I couldnt backtrack. It does work when I ask for gammahat explicitly

moreFitIndices(fit11.2) Error in lav_partable_indep_or_unrestricted(lavobject = lavobject, lavdata = lavdata, : object 'fit' not found

moreFitIndices <- function (object, fit.measures = "all", nPrior = 1) { fit.choices <- c("gammaHat", "adjGammaHat", "baseline.rmsea", "gammaHat.scaled", "adjGammaHat.scaled", "baseline.rmsea.scaled", "aic.smallN", "bic.priorN", "hqc", "sic") flags <- setdiff(fit.measures, c("all", fit.choices)) if (length(flags)) stop(paste("Argument 'fit.measures' includes invalid options:", paste(flags, collapse = ", "), "Please choose 'all' or among the following:", paste(fit.choices, collapse = ", "), sep = "\n")) if ("all" %in% fit.measures) fit.measures <- fit.choices fit <- lavInspect(object, "fit") p <- length(lavaan::lavNames(object, type = "ov", group = 1)) nParam <- fit["npar"] ngroup <- lavInspect(object, "ngroups") N <- n <- lavInspect(object, "ntotal") if (lavInspect(object, "options")$mimic == "EQS") n <- n - ngroup f <- -2 fit["logl"] result <- list() if (length(grep("gamma", fit.measures, ignore.case = TRUE))) { gammaHat <- p/(p + 2 ((fit["chisq"] - fit["df"])/n)) adjGammaHat <- 1 - (((ngroup p (p + 1))/2)/fit["df"]) (1 - gammaHat) result["gammaHat"] <- gammaHat result["adjGammaHat"] <- adjGammaHat if (lavInspect(object, "options")$test %in% c("satorra.bentler", "yuan.bentler","scaled.shifted")) { gammaHatScaled <- p/(p + 2 ((fit["chisq.scaled"] - fit["df.scaled"])/n)) adjGammaHatScaled <- 1 - (((ngroup p (p + 1))/2)/fit["df.scaled"]) (1 - gammaHatScaled) result["gammaHat.scaled"] <- gammaHatScaled result["adjGammaHat.scaled"] <- adjGammaHatScaled } } if (length(grep("rmsea", fit.measures))) { result["baseline.rmsea"] <- nullRMSEA(object, silent = TRUE) if (lavInspect(object, "options")$test %in% c("satorra.bentler", "yuan.bentler","scaled.shifted")) { result["baseline.rmsea.scaled"] <- nullRMSEA(object, scaled = TRUE, silent = TRUE) } } if (!is.na(f)) { if ("aic.smallN" %in% fit.measures) { warning("AICc was developed for univariate linear models. It is ", "probably not appropriate to use AICc to compare SEMs.") result["aic.smallN"] <- fit[["aic"]] + (2 nParam (nParam + 1))/(N - nParam - 1) } if ("bic.priorN" %in% fit.measures) { result["bic.priorN"] <- f + log(1 + N/nPrior) nParam } if ("hqc" %in% fit.measures) result["hqc"] <- f + 2 log(log(N)) nParam if ("sic" %in% fit.measures) result["sic"] <- sic(f, object) } class(result) <- c("lavaan.vector", "numeric") unlist(result[fit.measures]) }

TDJorgensen commented 6 years ago

Same would happen when test == "mean.var.adjusted", so I just changed it to grab a scaled version whenever ! test %in% c("standard","bollen.stine"). Available in the development version.

I do not have a problem requesting the default "all" with ML, MLM/R, or MLMV. If you still get that error, can you post a script to reproduce the problem?