模型摘要和R中的工具变量诊断。

huangapple go评论63阅读模式
英文:

Model summary and diagnostics for instrumental variables in R

问题

我使用ivreg包运行工具变量回归(2SLS)。

library(ivreg)
as_tibble(mtcars)

model_iv <- ivreg(formula = mpg ~ disp + drat |
                                         drat + carb,
              data = mtcars)

和许多其他人一样,我对与我的模型报告相关的诊断(例如弱工具、wu-hausman和sargan)感兴趣。

summary(model_iv, diagnostics = TRUE)
  
Diagnostic tests:
                 df1 df2 statistic p-value   
Weak instruments   1  29     8.286 0.00743 **
Wu-Hausman         1  28    11.594 0.00202 **
Sargan             0  NA        NA      NA   
---
Signif. codes:  0***0.001**0.01*0.05 ‘.’ 0.1 ‘ ’ 1

有一种方法可以将这些添加到stargazer中:

https://stackoverflow.com/questions/33048097/r-robust-ses-and-model-diagnostics-in-stargazer-table

我还在modelsummary的问题中找到了这个讨论:

https://github.com/vincentarelbundock/modelsummary/issues/123

我尝试了modelsummary,但似乎这个功能还没有实现。是否有另一个包用于包括工具变量模型诊断的模型报告?

英文:

I run instrumental variable regression (2SLS) with ivreg package.

library(ivreg)
as_tibble(mtcars)

model_iv &lt;- ivreg(formula = mpg ~ disp + drat |
                                         drat + carb,
              data = mtcars)

As many other people, I am interested in the diagnostics (e.g. weak instruments, wu-hausman and sargan) attached to my model reporting.

summary(model_iv, diagnostics = TRUE)

Diagnostic tests:
                 df1 df2 statistic p-value   
Weak instruments   1  29     8.286 0.00743 **
Wu-Hausman         1  28    11.594 0.00202 **
Sargan             0  NA        NA      NA   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

There is a workaround to add those to stargazer:

https://stackoverflow.com/questions/33048097/r-robust-ses-and-model-diagnostics-in-stargazer-table

And I found the very discussion in an issue for modelsummary:

https://github.com/vincentarelbundock/modelsummary/issues/123

I tried the modelsummary but it seems that the feature hasn't been implemented. Is there another package for model reporting including diagnostics for instrumental variable models?

答案1

得分: 3

你可以使用 modelsummary 轻松完成这个任务。请查看网站上有关如何添加新拟合优度统计信息的详细文档:<https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html#new-models-and-custom-statistics>

基本上有3种选项。

选项 1:add_rows

创建一个与您的表具有相同列数的数据框,并使用 add_rows 参数将其添加到表底部。这允许您完全掌控,但可能会有些繁琐。这也相当直观,所以我这里不提供示例。只需查看 ?modelsummary

选项 2:metrics 参数

在幕后,modelsummary 使用 performance 包提取拟合优度统计信息。该包支持 metrics="all" 参数,有时会返回更多信息。在这种情况下,我们可以将 metrics 直接传递给 modelsummary 并获取 Wu-Haussman 统计信息:

library(ivreg)
library(tibble)
library(modelsummary)

model_iv &lt;- ivreg(
    formula = mpg ~ disp + drat | drat + carb,
    data = mtcars)

modelsummary(model_iv, metrics = &quot;all&quot;)
(1)
(Intercept) 51.687
(17.451)
disp -0.072
(0.020)
drat -4.169
(3.649)
Num.Obs. 32
R2 0.455
R2 Adj. 0.418
AIC 193.3
BIC 199.2
RMSE 4.38
wu.hausman 11.5944242731131
wu.hausman.p 0.00201604586133519

当然,您可能希望使用 gof_map 参数来格式化此统计信息,以减少小数位数并清理名称。

选项 3:glance_custom

最后,您可以定义一个 glance_custom.ivreg() 方法,自动提取、格式化和添加统计信息。上面我提供的链接中有详细的教程,下面是一个简单的示例:

glance_custom.ivreg &lt;- function(x, ...) { # 不要忘了 ...
    s &lt;- summary(x)$diagnostics
    wi &lt;- s[1, &quot;statistic&quot;]
    wh &lt;- s[2, &quot;statistic&quot;]
    tibble(
        &quot;弱工具&quot; = round(wi, 2),
        &quot;Wu-Haussman&quot; = round(wh, 3))
}

modelsummary(model_iv)
(1)
(Intercept) 51.687
(17.451)
disp -0.072
(0.020)
drat -4.169
(3.649)
Num.Obs. 32
R2 0.455
R2 Adj. 0.418
AIC 193.3
BIC 199.2
RMSE 4.38
弱工具 8.29
Wu-Haussman 11.594

所有后续的 ivreg 模型将自动具有此新的统计信息。

英文:

You can easily do this with modelsummary. Please see the detailed documentation on how to add new goodness-of-fit statistics on the website: <https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html#new-models-and-custom-statistics>

There are basically 3 options.

Option 1: add_rows

Build your own data frame with the same number of columns as your table, and use the add_rows argument to stick it at the bottom of the table. This allows you full control, but can be tedious. It is also quite self-explanatory, so I don’t give an example here. Just see ?modelsummary.

Option 2: metrics argument

Behind the scenes, modelsummary extracts goodness of fit statistics using the performance package. That package supports a metrics=&quot;all&quot; argument which sometimes return more information. In this case, we can feed metrics directly to modelsummary and get the Wu-Haussman statistic:

library(ivreg)
library(tibble)
library(modelsummary)

model_iv &lt;- ivreg(
    formula = mpg ~ disp + drat | drat + carb,
    data = mtcars)

modelsummary(model_iv, metrics = &quot;all&quot;)
(1)
(Intercept) 51.687
(17.451)
disp -0.072
(0.020)
drat -4.169
(3.649)
Num.Obs. 32
R2 0.455
R2 Adj. 0.418
AIC 193.3
BIC 199.2
RMSE 4.38
wu.hausman 11.5944242731131
wu.hausman.p 0.00201604586133519

Of course, you’ll probably want to format this statistic with the gof_map argument to reduce the number of digits and clean up the names.

Option 3: glance_custom

Finally, you can define a glance_custom.ivreg() method to extract, format, and add the statistic automatically. There is a detailed tutorial at the link I posted above, so here’s a simple example:

glance_custom.ivreg &lt;- function(x, ...) { # don&#39;t forget ...
    s &lt;- summary(x)$diagnostics
    wi &lt;- s[1, &quot;statistic&quot;]
    wh &lt;- s[2, &quot;statistic&quot;]
    tibble(
        &quot;Weak Instrument&quot; = round(wi, 2),
        &quot;Wu-Haussman&quot; = round(wh, 3))
}

modelsummary(model_iv)
(1)
(Intercept) 51.687
(17.451)
disp -0.072
(0.020)
drat -4.169
(3.649)
Num.Obs. 32
R2 0.455
R2 Adj. 0.418
AIC 193.3
BIC 199.2
RMSE 4.38
Weak Instrument 8.29
Wu-Haussman 11.594

All subsequent ivreg models will automatically have this new statistic.

huangapple
  • 本文由 发表于 2023年2月23日 22:10:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定