您可以通过编程方式拟合模型,然后使用AIC对其进行比较:
library(lme4)
combinations <- expand.grid(fixed = 1:3, random = 1:3)
models <- lapply(seq_len(nrow(combinations)), function(i) {
f <- as.formula(paste(
'mpg ~ poly(qsec,', combinations[i, 1], ') + (poly(qsec,', combinations[i, 2], ') | cyl)'
))
lmer(f, mtcars)
})
names(models) <- apply(combinations, 1, paste, collapse = '_')
aics <- sapply(models, function(m) summary(m)$AIC)
result <- data.frame(model = names(models), AIC = aics)
result <- result[order(result$AIC), ]
result$dAIC <- result$AIC - result$AIC[1]
result
model AIC dAIC
3_3.REML 3_3 155.7776 0.0000000
3_2.REML 3_2 155.9683 0.1907229
3_1.REML 3_1 156.0175 0.2398943
2_3.REML 2_3 160.1618 4.3842105
2_2.REML 2_2 160.2372 4.4595903
2_1.REML 2_1 160.3215 4.5438645
1_3.REML 1_3 164.5201 8.7424622
1_2.REML 1_2 165.2802 9.5025476
1_1.REML 1_1 165.3264 9.5487699