访问confint()返回的对象的元素

时间:2015-04-27 19:41:28

标签: r

示例代码

data(anorexia, package = "MASS")
anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt),
            family = gaussian, data = anorexia)
confint(anorex.1)

这会产生

                 2.5 %     97.5 %
(Intercept) 23.5253133 76.0169047
Prewt       -0.8814505 -0.2496272
TreatCont   -7.8082428 -0.3858882
TreatFT      0.3818011  8.7443242

我现在想要访问这些置信区间,例如,创建4个变量。这样做的首选方法是什么?我知道我可以使用

Intercept <- c(confint(anorex.1)[1],confint(anorex.1)[5])
Prewt <- c(confint(anorex.1)[2],confint(anorex.1)[6])
等等......但这看起来很笨重而且不够优雅。

2 个答案:

答案 0 :(得分:4)

将结果矩阵分配给名称,然后按名称提取行:

resCI <- confint(anorex.1)
IntCI <- resCI[ "(Intercept)", ]
PrewtCI <- resCI[ "Prewt", ]

答案 1 :(得分:2)

如果您希望获得那种输出,您可以简单地换入data.frame()吗?

data.frame(confint(anorex.1))
                X2.5..    X97.5..
(Intercept) 23.5253133 76.0169047
Prewt       -0.8814505 -0.2496272
TreatCont   -7.8082428 -0.3858882
TreatFT      0.3818011  8.7443242

扫帚包让一切事物变得更容易。

require(broom)
tidy(anorex.1, conf.int=TRUE)
         term   estimate  std.error statistic      p.value   conf.low  conf.high
1 (Intercept) 49.7711090 13.3909581  3.716770 0.0004101067 23.5253133 76.0169047
2       Prewt -0.5655388  0.1611824 -3.508689 0.0008034250 -0.8814505 -0.2496272
3   TreatCont -4.0970655  1.8934926 -2.163761 0.0339993147 -7.8082428 -0.3858882
4     TreatFT  4.5630627  2.1333359  2.138933 0.0360350847  0.3818011  8.7443242