# Setup
library(boot)
library(tidyverse)
get_mpg_mean <- function(data, indices){
# Function has to take in data and indices
new_data = data[indices,] # Resample based on indices
return(mean(new_data$mpg)) # Return statistic of interest
}
# Create the boot object
boot_obj = boot(data = mtcars,
statistic = get_mpg_mean,
R = 1000)
boot_obj
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = mtcars, statistic = get_mpg_mean, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 20.09062 0.06126875 1.049063
## (Intercept) wt hp disp
## 2.5% 32.33534 -5.861030 -0.06023895 -0.01733316
## 97.5% 42.02881 -1.857575 -0.01681910 0.01675699
cis <- sapply(1:length(var_names),
function(x) boot.ci(boot_obj,
index=x,
type = 'bca')$bca[4:5])
colnames(cis) <- var_names
print(cis)
## (Intercept) wt hp disp
## [1,] 31.95369 -5.576118 -0.05609859 -0.02235941
## [2,] 41.76300 -1.521268 -0.01415651 0.01381162
library(dotwhisker)
library(broom)
tidy(lm(mpg ~ wt + hp + disp, data=mtcars), conf.int = T) %>%
mutate(conf.low = as.numeric(cis[1,]),
conf.high = as.numeric(cis[2,])) %>%
by_2sd(mtcars) %>%
dwplot(show_intercept = F) + theme_bw() +
theme(legend.position="none") +
xlab('Beta coefficient with bootstrapped 95% CIs') + ylab('Variable') +
geom_vline(xintercept = 0, colour = "grey60", linetype = 2) #