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

9
我希望能够将一个命名的模型列表(merMod对象)传递给anova()函数,并在输出中保留模型名称。这在使用mclapply()并行运行一批缓慢的模型(如glmer)时特别有用。我想到的最好方法是对模型列表进行去命名处理,然后使用do.call,但这并不理想,因为我的模型可能会被命名为“mod12”、“mod17”和“mod16”,而这些模型名称在输出中会被翻译为“MODEL1”、“MODEL2”和“MODEL3”。 (当查看单个批次时,这可能看起来微不足道,但在长时间建模过程中,使用几十个模型肯定会导致混乱。)
请注意,这与从列表创建和调用线性模型不同,因为我不是要在列表之间比较模型对。这也比在模型列表上使用lapply更复杂,因为我以非一元方式使用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.call 返回这个:
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()周围,我也会很高兴。

1个回答

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

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

啊,使用with()很好。我知道它一定与环境有关。谢谢! - Dan Villarreal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接