#For use when comparing the fit of nested models with complex data, # (e.g. TYPE = COMPLEX is mplus) # The Scaled Difference Chi-square Test Statistic can be found at #http://preprints.stat.ucla.edu/260/chisquare.pdf # This function provides scaled differences tests based on chi-square # and loglikihood values. # The notation below is taken from http://www.statmodel.com/chidiff.shtml #For the chi-square difference test (which is the default in this function), # the notation from #www.statmodel.com reads: # "d0 is the degrees of freedom in the nested model, #c0 is the scaling correction factor for the nested model, #d1 is the degrees of freedom in the comparison model, #and c1 is the scaling correction factor for the comparison model. #Be sure to use the correction factor given in the output for the H0 model. #... T0 and T1 are the MLM, MLR, #or WLSM chi-square values for the nested and comparison model, respectively." #For the difference test using logliklihood value (loglike=TRUE): #d0 and d1 are the number of parameters for the H0 and H1 models. #c0 and c1 are the scaling correction factors for the H0 and H1 models. # t0 and t1 are the loglikelihood values for the H0 and H1 models. scale.diff.test <- function (d0, d1, c0, c1, t0, t1, loglike=FALSE) { if(loglike) {cd.2<- (d0*c0 - d1*c1 ) / (d0 - d1) x<- t0 - t1 TRd.2<- -2*(x) / cd.2 df.2 <- abs(d0 - d1) p.2 <- pchisq(TRd.2, df.2) sig.2 <- 1 - p.2 cat ("scaled loglikelihood difference test for complex data:", "\n") cat ("value =", TRd.2) cat (" df=", df.2) cat (" sig. =", sig.2, "\n")} else {cd<- (d0*c0 - d1*c1 ) / (d0 - d1) TRd<-(t0*c0 - t1*c1) / cd df <- abs(d0 - d1) p <- pchisq(TRd, df) sig <- 1 - p cat ("scaled Chi-squared difference test for complex data:", "\n") cat ("value =", TRd) cat (" df=", df) cat (" sig. = ", sig, "\n") } } scale.diff.test (45, 43, 1.12, 1.123, 246.44, 237.846) scale.diff.test (39, 47, 1.45, 1.546, -2606, -2583, loglike=TRUE) scale.diff.test (162, 166, 1.088, 1.09, 777.043, 791.371) scale.diff.test (166, 162, 1.09, 1.088, 791.371, 777.043) scale.diff.test (77, 72, 1, 1, 250, 231)

Created by Pretty R at inside-R.org

Examples:

scale.diff.test (45, 43, 1.12, 1.123, 246.44, 237.846)

scale.diff.test (39, 47, 1.45, 1.546, -2606, -2583, loglike=TRUE)

Output:

scaled Chi-squared difference test for complex data:

value = 8.443147 df= 2 sig. = 0.01467553

scaled loglikelihood difference test for complex data:

value = 22.84012 df= 8 sig. = 0.003575695