Function for Scaled Difference Tests (chi-square and logliklihood values)

#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

Advertisements

About Philip Parker

I am a post doc in developmental and educational psychology at a Germany university. I did my PhD at the university of Sydney in stress and well-being. Most days I am hunched over a computer yelling at statistical software or responding to journal editors who seem to always want twice the amount of content but with half the words. For fun I like to read up on the latest developments in R and programming various functions.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s