【问题标题】:How to get CI 95% for coefficients of linear model using simpleboot package如何使用 simpleboot 包获得线性模型系数的 CI 95%
【发布时间】:2019-11-06 15:02:25
【问题描述】:

我正在尝试使用程序lm() 预测一个线性模型(具有 4 个预测变量的基本线性回归)。这一切正常。

我现在想做的是引导模型。在 Google 上快速搜索后,我发现了 simpleboot 包,它似乎很容易理解。

我可以使用以下方法轻松引导 lm.object:

boot_mod <- lm.boot(mod,R=100,rows=TRUE) 

然后打印对象boot_mod

我还可以访问列表,其中每个引导样本的系数与其他指标(如 RSS、R² 等)一起列出。

谁能告诉我如何将引导列表中的所有系数保存在列表或数据框中?

结果最好是这样的:

boot_coef

sample coef 1 coef 2 coef 3...
1      1,1    1,4    ...
2      1,2    1,5    ...
3      1,3    1,6    ...

library(tidyverse)
library(simpleboot)
### Some Dummy-Data in a dataframe
a <- c(3,4,5,6,7,9,13,12)
b <- c(5,9,14,22,12,5,12,18)
c <- c(7,2,8,7,12,5,3,1)
df <- as_data_frame(list(x1=a,x2=b,y=c))
### Linear model
mod <- lm(y~x1+x2,data=df)
### Bootstrap
boot_mod <- lm.boot(mod,R=10,rows = TRUE)

【问题讨论】:

  • 抱歉正确理解我的问题:boot_coef 是我希望收到的结果。我实际运行的代码却以“library(tidyverse”开头

标签: r lm


【解决方案1】:

这是一个tidyverse 选项,用于保存boot.list 中的所有系数:

library(tidyverse)
as.data.frame(boot_mod$boot.list) %>% 
        select(ends_with("coef")) %>% # select coefficients
        t(.) %>% as.data.frame(.) %>% # model per row
        rownames_to_column("Sample")  %>% # set sample column
        mutate(Sample = parse_number(Sample))
# output
   Sample (Intercept)         x1          x2
1       1    5.562417 -0.2806786  0.12219191
2       2    8.261905 -0.8333333  0.54761905
3       3    9.406171 -0.5863124  0.07783740
4       4    8.996784 -0.6040479  0.06737891
5       5   10.908036 -0.7249561 -0.03091908
6       6    8.914262 -0.5094340  0.05549390
7       7    7.947724 -0.2501127 -0.08607481
8       8    6.255539 -0.2033771  0.07463971
9       9    5.676581 -0.2668020  0.08236743
10     10   10.118126 -0.4955047  0.01233728

【讨论】:

  • 按预期工作。非常感谢
  • 嗨@ANG,我刚刚接受了您的回答作为解决方案。抱歉,我还没有这样做。我是 stackoverflow 的新手。
【解决方案2】:

也可以使用同包simpleboot的函数sample

根据lm.bootloess.boot 的输出,您可以指定要提取的信息类型:

samples(object, name = c("fitted", "coef", "rsquare", "rss"))

它根据提取的实体输出向量或矩阵。

来源: https://rdrr.io/cran/simpleboot/man/samples.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    相关资源
    最近更新 更多