p-values can be computed by inverting the corresponding confidence intervals, as described in Section 14.2 of Thulin (2024) and Section 3.12 in Hall (1992). This package contains functions for computing bootstrap p-values in this way. The approach relies on the fact that:
Summary tables with confidence intervals and p-values for the
coefficients of regression models can be obtained using the
boot_summary (most models) and
censboot_summary (models with censored response variables)
functions. Currently, the following models are supported:
lm,glm or
MASS::glm.nb,nls,MASS::rlm,MASS::polr,lme4::lmer or
lmerTest::lmer,lme4::glmer.survival::coxph
(using censboot_summary).survival::survreg or rms::psm (using
censboot_summary).residuals(object, type="pearson") returns Pearson
residuals; fitted(object) returns fitted values;
hatvalues(object) returns the leverages, or perhaps the
value 1 which will effectively ignore setting the hatvalues. In
addition, the data argument should contain no missing
values among the columns actually used in fitting the model.A number of examples are available in Chapters 8 and 9 of Modern Statistics with R.
Here are some simple examples with a linear regression model for the
mtcars data:
library(boot.pval)
# Bootstrap summary of a linear model for mtcars:
model <- lm(mpg ~ hp + vs, data = mtcars)
boot_summary(model)
#> Estimate Lower.bound Upper.bound p.value
#> (Intercept) 26.96300111 21.46322260 32.86658599 <0.001
#> hp -0.05453412 -0.08304728 -0.02615399 0.003
#> vs 2.57622314 -1.20409325 6.24265656 0.205
# Use 9999 bootstrap replicates and adjust p-values for
# multiplicity using Holm's method:
boot_summary(model, R = 9999, adjust.method = "holm")
#> Estimate Lower.bound Upper.bound p.value Adjusted p-value
#> (Intercept) 26.96300111 21.18788537 32.85955904 <1e-04 0.0003
#> hp -0.05453412 -0.08296301 -0.02470924 <1e-04 0.0003
#> vs 2.57622314 -1.41150568 6.52312994 0.2038 0.2038
# Use case resampling instead of residual resampling:
boot_summary(model, method = "case")
#> Estimate Lower.bound Upper.bound p.value
#> (Intercept) 26.96300111 21.4061939 35.40581427 <0.001
#> hp -0.05453412 -0.1047486 -0.02785278 <0.001
#> vs 2.57622314 -1.6460323 6.44258375 0.228
# Export results to a gt table:
boot_summary(model, R = 9999) |>
summary_to_gt()| Estimate | 95 % CI | p-value | |
|---|---|---|---|
| (Intercept) | 26.963 | (21.372, 32.714) | <1e-04 |
| hp | −0.055 | (−0.083, −0.026) | <1e-04 |
| vs | 2.576 | (−1.453, 6.402) | 0.2044 |
See Davison and Hinkley (1997) for details about residual resampling (the default) and case resampling.
# Export results to a Word document:
library(flextable)
boot_summary(model, R = 9999) |>
summary_to_flextable() |>
save_as_docx(path = "my_table.docx")And a toy example for a generalised linear mixed model (using a small number of bootstrap repetitions):
For complex models, speed can be greatly improved by using
parallelisation. For lmer and glmer models,
this is set using the parallel (available options are
"multicore" and "snow"). The number of CPUs to
use is set using ncpus.
model <- glmer(TICKS ~ YEAR + (1|LOCATION),
data = grouseticks, family = poisson)
boot_summary(model, R = 999, parallel = "multicore", ncpus = 10)For other models, use ncores:
Survival regression models should be fitted using the argument
model = TRUE. A summary table can then be obtained using
censboot_summary. By default, the table contains
exponentiated coefficients (i.e. hazard ratios, in the case of a Cox PH
model).
library(survival)
# Weibull AFT model:
model <- survreg(formula = Surv(time, status) ~ age + sex, data = lung,
dist = "weibull", model = TRUE)
# Table with exponentiated coefficients:
censboot_summary(model)
#> Using exponentiated coefficients.
#> Estimate Lower.bound Upper.bound p.value
#> (Intercept) 531.0483429 194.6016789 1375.832825 <0.001
#> age 0.9878178 0.9733598 1.002701 0.109
#> sex 1.4653368 1.1694586 1.872684 <0.001
# Cox PH model:
model <- coxph(formula = Surv(time, status) ~ age + sex, data = lung,
model = TRUE)
# Table with hazard ratios:
censboot_summary(model)
#> Using exponentiated coefficients.
#> Estimate Lower.bound Upper.bound p.value
#> age 1.017191 0.9983369 1.038788 0.085
#> sex 0.598566 0.4251827 0.803276 0.003
# Table with original coefficients:
censboot_summary(model, coef = "raw")
#> Using raw coefficients.
#> Estimate Lower.bound Upper.bound p.value
#> age 0.01704533 -0.001605989 0.0368712 0.068
#> sex -0.51321852 -0.829519243 -0.2039491 0.003To speed up computations using parallelisation, use the
parallel and ncpus arguments: