Published on *OpenMx* (http://openmx.psyc.virginia.edu)

By *felix.s*

Created *02/16/2010 - 04:54*

Tue, 02/16/2010 - 04:54 — felix.s [1]

Dear OpenMx community,

I'm rather new to the field of SEM, but I am very happy about OpenMx - the learning curve really was really steep (... in the sense of quick successes). Congratulations!

In some post there are already some hints about a "mxCompare" function. I tried to implement a likelihood ratio / chi square test myself:

The function does some minor error checking (e.g., whether all compared models have the same saturated likelihood, which should be an indicator that all models are nested). The functions orders models based on their dfs and compares models row by row (i.e., each model is compared to its direct predecessor; comparable to the anova-approach).

LRtest <- function(..., digits=3) { library(plyr) model_list <- list(...) if (length(model_list) < 2) stop("Error: At least two mx-models have to be provided!") res <- ldply(model_list, function(x) { summ <- summary(x) return(data.frame(model=x@name, AIC.Mx=summ$AIC.Mx, BIC.Mx=summ$BIC.Mx, RMSEA=summ$RMSEA, satL=summ$SaturatedLikelihood, m2LL=summ$Minus2LogLikelihood, chisq=summ$Chi, df=summ$degreesOfFreedom)) }) res <- res[order(res$df, decreasing=TRUE),] res$delta.chi <- NA res$delta.df <- NA res$p.value <- NA # check if the saturated likelihood is the same in all provided models (which should be the case if all are nested) if (any(res$satL != res$satL[1])) stop("Error: Not all models are nested!") for (m in 2:nrow(res)) { res$delta.df[m] <- res$df[m-1] - res$df[m] if (res$delta.df[m] > 0) { res$delta.chi[m] <- res$chisq[m-1] - res$chisq[m] res$p.value[m] <- pchisq(res$delta.chi[m], res$delta.df[m], lower.tail=FALSE) } } res[,-1] <- round(res[,-1], digits) return(res) }

A sample output would look like:

model AIC.Mx BIC.Mx RMSEA satL m2LL chisq df delta.chi delta.df p.value 1 m0 196.378 31.925 0.165 1136.021 1410.398 274.378 39 NA NA NA 2 m1 182.128 33.295 0.170 1136.021 1386.149 250.128 34 24.250 5 0.000 3 m2 64.193 -25.672 0.114 1136.021 1268.213 132.193 34 NA 0 NA 4 m3 60.728 -18.909 0.118 1136.021 1254.749 118.728 29 13.464 5 0.019 5 m4 45.251 -19.851 0.113 1136.021 1231.272 95.251 25 23.477 4 0.000 6 m5 47.574 -13.593 0.120 1136.021 1227.595 91.574 22 3.678 3 0.298 </CODE> (sorry, the columns are somewhat disaligned in the post) In the output, model m2 could not be compared to m1, as both have the same amount of dfs. I only work with RAM-style models, so I don't know whether this approach generalizes to matrix-style. I would be happy if some "pros" could comment on that (and give me a hint whether that approach is reasonable or even correct ;-) Best, Felix

**Links:**

[1] http://openmx.psyc.virginia.edu/users/felixs

[2] http://openmx.psyc.virginia.edu/thread/698

[3] http://openmx.psyc.virginia.edu/thread/445

[4] http://openmx.psyc.virginia.edu/forums/openmx-developer-forums/openmx-wishlist