You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R语言的dredge函数中获取各模型组合的p值?

解决dredge函数添加模型p值的问题

要在dredge的输出中加入每个模型组合的整体显著性p值,你可以利用extra参数支持自定义函数的特性,自己编写函数提取线性模型的p值,具体步骤如下:

  1. 加载数据与依赖包,拟合全模型
data(mtcars)
library(MuMIn)

# 拟合包含所有预测变量的全模型
lm_full <- lm(mpg ~ cyl + disp + hp, data = mtcars)
  1. 定义提取模型整体p值的函数
    对于线性模型,模型整体的显著性p值来自F检验结果,我们可以通过summary()提取F统计量后,用pf()计算对应的p值:
get_model_p <- function(model) {
  f_stats <- summary(model)$fstatistic
  # 计算F检验的右尾p值
  pf(f_stats[1], f_stats[2], f_stats[3], lower.tail = FALSE)
}
  1. 在dredge中调用自定义函数
    将自定义函数传入extra参数,同时指定输出列名(这里用p.value),和已有的R^2一起使用:
lm_gd <- dredge(lm_full, rank = "AIC", extra = c("R^2", "p.value" = get_model_p))
# 查看结果
lm_gd

运行后,lm_gd的输出会新增p.value列,对应每个模型组合的整体显著性p值。


内容的提问来源于stack exchange,提问作者Kevin López Reyes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 08:39:59