In my R code below, suppose I want to compare all unique 2 m
objects using a similar R routine. For example, to compare m1
and m2
, my routine is:
pchisq(2 * (logLik(m2) - logLik(m1)), df = abs(m1$df.residual - m2$df.residual), lower = F)
Question:
I was wondering how I could make a function to make all unique pairwise comparisons for all m
objects using my routine?
Here is what I've tried without success:
## Suppose we have 4 `m` objects: `m1...m4` (6 unique pairwise comparisons possible)
m1 <- lm(hp ~ vs, mtcars)
m2 <- lm(hp ~ vs*wt, mtcars)
m3 <- lm(hp ~., mtcars)
m4 <- lm(hp ~ vs * gear * wt, mtcars)
compare <- function(...){
m <- list(...)
L <- length(m) - 1
lapply(1:L, function(i) pchisq(2 * (logLik(m[[i+1]]) - logLik(m[[i]])), df = abs(m[[i]]$df.residual - m[[i+1]]$df.residual), lower.tail = FALSE) )
}
# Example of use:
compare(m1, m2, m3, m4)
anova(m1, m2, m4, m3, test = "Chisq")
lm
andanova
supports that and there are many other methods as well. If you are using something not shown suggest you mention it.