将命名的模型列表传递给 anova.merMod

Pass a named list of models to anova.merMod

我希望能够将命名的模型列表(merMod 对象)传递给 anova() 并在输出中保留模型名称。这在使用 mclapply() 来 运行 一批像 glmers 这样的慢速模型并行时更有效的情况下特别有用。我想出的最好办法是在模型列表的取消命名版本上使用 do.call,但这并不理想,因为我可能有模型命名为(比如)"mod12"、"mod17" 和 "mod16",这些模型名称在输出中被转换为 "MODEL1"、"MODEL2" 和 "MODEL3"。 (在查看单个批次时,这可能看起来微不足道,但在使用数十个模型的长时间建模过程中,这肯定会造成混淆。)

请注意,这与 Create and Call Linear Models from List because I'm not trying to compare pairs of models across lists. It's also more complex than Using lapply on a list of models 不同,因为我以非一元方式使用 anova()。

这是一个最小的代表:

library(lme4)

formList <- c(mod12 = angle ~ recipe + temp + (1|recipe:replicate),
              mod17 = angle ~ recipe + temperature + (1|recipe:replicate),
              mod16 = angle ~ recipe * temperature + (1|recipe:replicate))
modList <- lapply(formList, FUN=lmer, data=cake)

# Fails because modList is named so it's interpreted as arg-name:arg pairs
do.call(anova, modList)

# Suboptimal because model names aren't preserved
do.call(anova, unname(modList))

# Fails because object isn't merMod (or some other class covered by methods("anova"))
do.call(anova, list(object=modList[1], ...=modList[-1], model.names=names(modList)))

第二个do.callreturns这个:

Data: ..1
Models:
MODEL1: angle ~ recipe + temp + (1 | recipe:replicate)
MODEL2: angle ~ recipe + temperature + (1 | recipe:replicate)
MODEL3: angle ~ recipe * temperature + (1 | recipe:replicate)
       Df    AIC    BIC  logLik deviance   Chisq Chi Df Pr(>Chisq)
MODEL1  6 1708.2 1729.8 -848.08   1696.2                          
MODEL2 10 1709.6 1745.6 -844.79   1689.6  6.5755      4     0.1601
MODEL3 20 1719.0 1791.0 -839.53   1679.0 10.5304     10     0.3953

理想情况下,输出如下所示:

Data: ..1
Models:
mod12: angle ~ recipe + temp + (1 | recipe:replicate)
mod17: angle ~ recipe + temperature + (1 | recipe:replicate)
mod16: angle ~ recipe * temperature + (1 | recipe:replicate)
      Df    AIC    BIC  logLik deviance   Chisq Chi Df Pr(>Chisq)
mod12  6 1708.2 1729.8 -848.08   1696.2                          
mod17 10 1709.6 1745.6 -844.79   1689.6  6.5755      4     0.1601
mod16 20 1719.0 1791.0 -839.53   1679.0 10.5304     10     0.3953

我该怎么做?我对 anova() 的丑陋包装器感到非常满意,如果这意味着我得到更清晰的输出。

您可以像这样传递符号列表:

with(modList,
     do.call(anova, 
             lapply(names(modList), as.name)))
#refitting model(s) with ML (instead of REML)
#Data: ..1
#Models:
#mod12: angle ~ recipe + temp + (1 | recipe:replicate)
#mod17: angle ~ recipe + temperature + (1 | recipe:replicate)
#mod16: angle ~ recipe * temperature + (1 | recipe:replicate)
#      Df    AIC    BIC  logLik deviance   Chisq Chi Df Pr(>Chisq)
#mod12  6 1708.2 1729.8 -848.08   1696.2                          
#mod17 10 1709.6 1745.6 -844.79   1689.6  6.5755      4     0.1601
#mod16 20 1719.0 1791.0 -839.53   1679.0 10.5304     10     0.3953