gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:18:11 24 4
gpt4 key购买 nike

我希望能够将命名的模型列表(merMod 对象)传递给 anova() 并在输出中保留模型名称。这在使用 mclapply() 更有效地并行运行一批慢模型(如 glmers)的上下文中特别有用。我想出的最好方法是在模型列表的未命名版本上使用 do.call,但这并不理想,因为我可能有名为(例如)“mod12”、“mod17”和“mod16”的模型并且这些模型名称在输出中被转换为“MODEL1”、“MODEL2”和“MODEL3”。 (在查看单个批次时,这可能看起来微不足道,但在使用数十个模型进行长时间建模的过程中,这绝对是混淆的秘诀。)

请注意,这与 Create and Call Linear Models from List 不是同一个问题。因为我不是在尝试跨列表比较模型对。它也比 Using lapply on a list of models 更复杂因为我以非一元方式使用 anova() 。

这是一个最小的reprex:

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() 周围的丑陋包装感到非常满意,如果这意味着我得到了更清晰的输出。

最佳答案

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

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

关于r - 将命名的模型列表传递给 anova.merMod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52868667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com