R 中模型对象的增强/变异

发布于 2025-01-16 06:55:24 字数 623 浏览 3 评论 0原文

所以我正在运行多个 Cochrane orcutt 回归,这没有问题。然后,我使用 modelsummary() 显示这些回归的输出。到目前为止仍然没有问题。

但是,当我尝试使用 modelplot() 比较模型时,在 cochrane orcutt 模型(“orcutt”类)中没有计算置信区间,因此出现以下错误:

eval(parse(text = text, keep.source = FALSE), envir) 中的错误: 未找到对象“conf.low”

我知道这里的问题是什么 - 只是没有由 cochrane.orcutt() 命令计算的置信区间“部分”。部分解决方案也是显而易见的 - 我可以使用点估计/系数和标准误差(当然默认情况下包含在模型中)来计算置信区间。

然而,当我想在 modelplot() 中使用这些置信区间值时,我的问题就出现了,因为它们不在模型对象“中”。在我的无知中,我尝试使用 mutate() 尝试以下操作来创建置信区间的下限:

model %>% 
+   mutate(`conf.low`=`coefficients`-1.96*`std.error`)

我希望这能够很好地传达我的问题,感谢您的阅读。

So I am running multiple cochrane orcutt regressions, which is no problem. I then display the output of these regressions using modelsummary(). Still no problems up to this point.

However, when I then try to compare the models using modelplot(), there are no confidence intervals computed in the cochrane orcutt model (class "orcutt") and I thus get the following error:

Error in eval(parse(text = text, keep.source = FALSE), envir) :
object 'conf.low' not found

I know what the problem here is - there are just no confidence interval "parts" computed by the cochrane.orcutt() command. A partial solution is also obvious - I can just calculate the confidence intervals using the point estimates/coefficients and the standard errors (which are of course included in the model by default).

However my problem arises when I want to use these confidence interval values in modelplot(), because they are not "in" the model object. In my ignorance, I attempted the following to try and create the lower bound of a confidence interval, using mutate():

model %>% 
+   mutate(`conf.low`=`coefficients`-1.96*`std.error`)

I hope this conveys my problem well enough, thank you for reading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Smile简单爱 2025-01-23 06:55:24

正如评论中所述,mutatedplyr 包中的一个函数,旨在处理数据帧,而不是模型对象或在 modelsummary 表上。

另外,请注意,我无法正确诊断您的问题,因为您没有提供最小可重现示例并且您不提供t 事件告诉我们您使用哪个函数来估计模型。

R 中执行 Cochrane-Orcutt 的一种方法是使用 orcutt 包:

library(orcutt)
data(icecream, package = "orcutt")
lm <- lm(cons ~ price + income + temp, data = icecream)
coch <- cochrane.orcutt(lm)

在幕后,modelsummary 包使用 broom 包中的 tidy 函数用于从模型对象中提取估计值:

library(broom)

tidy(coch)
#> # A tibble: 4 × 5
#>   term        estimate std.error statistic    p.value
#>   <chr>          <dbl>     <dbl>     <dbl>      <dbl>
#> 1 (Intercept)  0.157    0.290        0.543 0.592     
#> 2 price       -0.892    0.811       -1.10  0.282     
#> 3 income       0.00320  0.00155      2.07  0.0488    
#> 4 temp         0.00356  0.000555     6.42  0.00000102

从上面的输出中,您可以看到 broom 不会提取置信区间。这可以解释为什么 modelsummary 无法打印您的表格/图。

另一种选择是指示 modelsummary 使用 parameters 包而不是 broom 来提取估计值。这可以通过设置全局选项来实现:

library(modelsummary)
options(modelsummary_get = "parameters")
modelsummary(coch, statistic = "conf.int", output = "markdown")
模型 1
(截距)0.157
[-0.439, 0.754]
价格-0.892
[-2.562, 0.778]
收入0.003
[0.000, 0.006]
温度0.004
[0.002, 0.005]
观察数。30
R20.649
R2 调整0.607
rho0.401
number.interaction11.000
dw.original1.021
p.value.original0.000
dw.transformed1.549
p.value.transformed0.051

并且 modelplot 现在也可以工作:

modelplot(coch)

另一种选择是使用 broom 默认值,但使用您自己的 tidy_custom.orcutt 方法自定义输出。这有点复杂,但您可以在 modelsummary 网站上找到详细说明:https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html#adding-new-information-to-existing-models

As noted in the comments, mutate is a function from the dplyr package which is intended to work on data frames, and not on model objects or on modelsummary tables.

Also, please note that I can’t diagnose your problem properly because you did not supply a MINIMAL REPRODUCIBLE EXAMPLE and you don’t event tell us which function you used to estimate the model.

One way to do the Cochrane-Orcutt in R is to use the orcutt package:

library(orcutt)
data(icecream, package = "orcutt")
lm <- lm(cons ~ price + income + temp, data = icecream)
coch <- cochrane.orcutt(lm)

Behind the scenes, the modelsummary package uses the tidy function from the broom package to extract estimates from model objects:

library(broom)

tidy(coch)
#> # A tibble: 4 × 5
#>   term        estimate std.error statistic    p.value
#>   <chr>          <dbl>     <dbl>     <dbl>      <dbl>
#> 1 (Intercept)  0.157    0.290        0.543 0.592     
#> 2 price       -0.892    0.811       -1.10  0.282     
#> 3 income       0.00320  0.00155      2.07  0.0488    
#> 4 temp         0.00356  0.000555     6.42  0.00000102

From the output above, you see that broom does NOT extract a confidence interval. This could explain why modelsummary can’t print your table/plot.

One alternative option is to instruct modelsummary to use the parameters package to extract estimates instead of broom. This can be achieved by setting a global option:

library(modelsummary)
options(modelsummary_get = "parameters")
modelsummary(coch, statistic = "conf.int", output = "markdown")
Model 1
(Intercept)0.157
[-0.439, 0.754]
price-0.892
[-2.562, 0.778]
income0.003
[0.000, 0.006]
temp0.004
[0.002, 0.005]
Num.Obs.30
R20.649
R2 Adj.0.607
rho0.401
number.interaction11.000
dw.original1.021
p.value.original0.000
dw.transformed1.549
p.value.transformed0.051

And modelplot now works too:

modelplot(coch)

Yet another alternative would be to use the broom default, but to customize the output using your own tidy_custom.orcutt method. This is a bit more involved, but you’ll find detailed instruction on the modelsummary website: https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html#adding-new-information-to-existing-models

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文