library(dplyr)
library(purrr)
data(efc)
bs <- bootstrap(efc, 100)
# now run models for each bootstrapped sample
bs$models <- map(bs$strap, ~lm(neg_c_7 ~ e42dep + c161sex, data = .x))
# extract coefficient "dependency" and "gender" from each model
bs$dependency <- map_dbl(bs$models, ~coef(.x)[2])
bs$gender <- map_dbl(bs$models, ~coef(.x)[3])
# get bootstrapped confidence intervals
boot_ci(bs$dependency)
# compare with model fit
fit <- lm(neg_c_7 ~ e42dep + c161sex, data = efc)
confint(fit)[2, ]
# alternative function calls.
boot_ci(bs$dependency)
boot_ci(bs, dependency)
boot_ci(bs, dependency, gender)
boot_ci(bs, dependency, gender, method = "q")
# compare coefficients
mean(bs$dependency)
boot_est(bs$dependency)
coef(fit)[2]
# bootstrap() and boot_ci() work fine within pipe-chains
efc %>%
bootstrap(100) %>%
mutate(
models = map(strap, ~lm(neg_c_7 ~ e42dep + c161sex, data = .x)),
dependency = map_dbl(models, ~coef(.x)[2])
) %>%
boot_ci(dependency)
# check p-value
boot_p(bs$gender)
summary(fit)$coefficients[3, ]
if (FALSE) {
# 'spread_coef()' from the 'sjmisc'-package makes it easy to generate
# bootstrapped statistics like confidence intervals or p-values
library(dplyr)
library(sjmisc)
efc %>%
# generate bootstrap replicates
bootstrap(100) %>%
# apply lm to all bootstrapped data sets
mutate(
models = map(strap, ~lm(neg_c_7 ~ e42dep + c161sex + c172code, data = .x))
) %>%
# spread model coefficient for all 100 models
spread_coef(models) %>%
# compute the CI for all bootstrapped model coefficients
boot_ci(e42dep, c161sex, c172code)
# or...
efc %>%
# generate bootstrap replicates
bootstrap(100) %>%
# apply lm to all bootstrapped data sets
mutate(
models = map(strap, ~lm(neg_c_7 ~ e42dep + c161sex + c172code, data = .x))
) %>%
# spread model coefficient for all 100 models
spread_coef(models, append = FALSE) %>%
# compute the CI for all bootstrapped model coefficients
boot_ci()}
Run the code above in your browser using DataLab