从结果列表中提取SE

时间:2017-03-02 22:58:26

标签: r

如果您运行此代码,则会看到5次回归

library(quantreg)
data(engel)
xx <- income - mean(income)
fit2 = summary(rq(foodexp~income,tau=c(.05, .25, .5, .75, .95)))
fit2

每个都有一个较低的bd和较高的bd

我想提取INTERCEPT的coef和下限和上限并将其放在数据表中

现在访问coef,以及我使用[[]]的每个回归的上下截距,然后抓住第1行,然后rbind:

rbind(coef(summary(  fit2  )[[1]])[1,] ,
coef(summary(  fit2  )[[2]])[1,],
coef(summary(  fit2  )[[3]])[1,],
coef(summary(  fit2  )[[4]])[1,],
coef(summary(  fit2  )[[5]])[1,])

但必须有一种更简单的动态方式......有20个回归而不是5回归?谢谢。

1 个答案:

答案 0 :(得分:2)

library(quantreg)
data(engel)
fit2 = rq(foodexp~income, tau=c(.05, .25, .5, .75, .95), data = engel )

do.call('rbind', lapply(summary(fit2), function(x) coef(x)[1,]))
#      coefficients lower bd  upper bd
# [1,]    124.88004 98.30212 130.51695
# [2,]     95.48354 73.78608 120.09847
# [3,]     81.48225 53.25915 114.01156
# [4,]     62.39659 32.74488 107.31362
# [5,]     64.10396 46.26495  83.57896

基于@thelatemail建议 - 使用sapply

截距系数

sapply( summary(fit2), function(x) x[['coefficients']][1,] )
#                   [,1]      [,2]      [,3]      [,4]     [,5]
# coefficients 124.88004  95.48354  81.48225  62.39659 64.10396
# lower bd      98.30212  73.78608  53.25915  32.74488 46.26495
# upper bd     130.51695 120.09847 114.01156 107.31362 83.57896

收入系数

sapply( summary(fit2), function(x) x[['coefficients']][2,] )
#                   [,1]      [,2]      [,3]      [,4]      [,5]
# coefficients 0.3433611 0.4741032 0.5601806 0.6440141 0.7090685
# lower bd     0.3433270 0.4203298 0.4870223 0.5801552 0.6739000
# upper bd     0.3897500 0.4943288 0.6019890 0.6904127 0.7344405