我正在尝试用lm()
程序预测一个线性模型(具有4个预测器的基本线性回归)。这一切都很好。
我现在要做的是引导模型。在谷歌上快速搜索后,我发现了simpleboot
包,它似乎很容易理解。
我可以使用下面这样的代码轻松地引导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)
发布于 2019-06-25 03:40:27
下面是一个保存boot.list
中所有系数的tidyverse
选项
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
发布于 2020-05-10 18:28:42
您也可以使用相同包simpleboot
的函数sample
给定lm.boot
或loess.boot
的输出,您可以指定要提取的信息类型:
samples(object, name = c("fitted", "coef", "rsquare", "rss"))
它根据提取的实体输出向量或矩阵。
https://stackoverflow.com/questions/56742772
复制相似问题