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

如何在R的modelsummary中获取弱工具变量F检验统计量?

如何在modelsummary中获取弱工具变量F检验统计量?

先用ivreg拟合工具变量模型,通过summary(diagnostics=TRUE)可以看到弱工具变量F检验的结果:

data(mtcars)
library(ivreg)

iv_model <- ivreg(mpg ~ qsec + cyl + drat | disp | wt, data = mtcars)
summary(iv_model, diagnostics = TRUE)

运行输出:

Call:
ivreg(formula = mpg ~ qsec + cyl + drat | disp | wt, data = mtcars)


Coefficients:
            Estimate Std. Error t value Pr(>|t|)   
(Intercept) 23.28560   20.84029   1.117  0.27370   
disp        -0.05730    0.02053  -2.791  0.00953 **
qsec         0.20443    0.59223   0.345  0.73263   
cyl          0.88477    1.52033   0.582  0.56542   
drat         0.25095    2.19015   0.115  0.90962   

Diagnostic tests:
                 df1 df2 statistic  p-value    
Weak instruments   1  27     19.96 0.000127 ***
Wu-Hausman         1  26     13.87 0.000956 ***
Sargan             0  NA        NA       NA    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

但默认调用modelsummary并设置metrics = "all"时,只会显示Wu-Hausman检验和Sargan检验的结果:

library(modelsummary)
modelsummary(iv_model, metrics = "all")

解决方法

方法1:自定义统计量映射(gof_map)

通过gof_map参数指定需要展示的检验项,直接包含弱工具变量检验:

gof_custom <- list(
  list(raw = "weak instruments", clean = "Weak instruments F", fmt = 2),
  list(raw = "wu.hausman", clean = "Wu-Hausman", fmt = 2),
  list(raw = "sargan", clean = "Sargan", fmt = 2)
)

modelsummary(iv_model, gof_map = gof_custom)

方法2:提取诊断结果后手动添加

先从模型的诊断结果中提取弱工具变量检验的统计量和p值,再用add_rows参数添加到表格中:

# 提取弱工具变量检验结果
weak_instr_res <- summary(iv_model, diagnostics = TRUE)$diagnostics["Weak instruments", ]

# 构建待添加的行
add_row <- data.frame(
  term = "Weak instruments",
  statistic = round(weak_instr_res["statistic"], 2),
  p.value = round(weak_instr_res["p.value"], 6),
  check.names = FALSE
)

# 生成表格并添加行
modelsummary(iv_model, metrics = "all", add_rows = add_row)

两种方法都能让modelsummary的输出表格中显示弱工具变量F检验的统计量和对应p值。

内容的提问来源于stack exchange,提问作者Marco

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 12:35:13