How to get CI 95% for coefficients of linear model using simpleboot package

Viewed 357

I'm trying to predict a linear model (basic linear regressions with 4 predictors) with the procedure lm(). This works all fine.

What I want to do now is bootstrapping the model. After a quick research on Google I found out about the package simpleboot which seemed to be quite easy to understand.

I can easily bootstrap the lm.object using something like this:

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

and afterwards print the object boot_mod.

I can also access the list in which the coefficients for each bootstrap sample are among other metrics such as RSS, R² and so on.

Can anyone tell me how I can save all coefficients from the boot list in a list or dataframe?

The result would look like this at best:

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)
2 Answers

You can also use the function sample of the same package simpleboot:

given the output from either lm.boot or loess.boot, you can specify what kind of information you want to extract:

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

It outputs either a vector or matrix depending on the entity extracted.

Source: https://rdrr.io/cran/simpleboot/man/samples.html

Here is a tidyverse option to save all coefficients from the 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
Related