如何使用 simpleboot 包获得 CI 95% 的线性模型系数
How to get CI 95% for coefficients of linear model using simpleboot package
我正在尝试使用过程 lm()
预测线性模型(具有 4 个预测变量的基本线性回归)。这一切正常。
我现在要做的是 bootstrapping 模型。在快速研究 Google 之后,我发现了 simpleboot
这个包,它似乎很容易理解。
我可以很容易地 bootstrap lm.object 使用这样的东西:
boot_mod <- lm.boot(mod,R=100,rows=TRUE)
然后打印对象 boot_mod
.
我还可以访问列表,其中每个 bootstrap 样本的系数都在其他指标中,例如 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)
这是一个 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
也可以使用同一个包sample
的功能simpleboot
:
给定 lm.boot
或 loess.boot
的输出,您可以指定要提取的信息类型:
samples(object, name = c("fitted", "coef", "rsquare", "rss"))
它根据提取的实体输出向量或矩阵。
我正在尝试使用过程 lm()
预测线性模型(具有 4 个预测变量的基本线性回归)。这一切正常。
我现在要做的是 bootstrapping 模型。在快速研究 Google 之后,我发现了 simpleboot
这个包,它似乎很容易理解。
我可以很容易地 bootstrap lm.object 使用这样的东西:
boot_mod <- lm.boot(mod,R=100,rows=TRUE)
然后打印对象 boot_mod
.
我还可以访问列表,其中每个 bootstrap 样本的系数都在其他指标中,例如 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)
这是一个 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
也可以使用同一个包sample
的功能simpleboot
:
给定 lm.boot
或 loess.boot
的输出,您可以指定要提取的信息类型:
samples(object, name = c("fitted", "coef", "rsquare", "rss"))
它根据提取的实体输出向量或矩阵。