statforclinicians

Draft: This page has not been reviewed by a statistician yet. Use it as a starting point and check important choices with a statistician.

How to

How to get a bootstrap confidence interval

When there is no simple formula for a confidence interval, or you do not want to assume a normal distribution, the bootstrap gets one by resampling your own data.

What it does

  1. Draw a new sample of the same size from your patients, with replacement. Some patients appear twice, some not at all.
  2. Calculate your statistic (a mean, a median, a difference, kappa) in that new sample.
  3. Repeat this thousands of times (for example 5,000 to 10,000).
  4. The middle 95% of those values is the 95% confidence interval (the percentile interval).

When it helps

  • The statistic has no easy formula for its interval: a median, a difference in medians, a ratio, or a coefficient such as kappa.
  • You want a confidence interval for the mean of skewed data, such as length of stay or costs, where the mean itself is what matters.
  • You want to check a result from a formula-based interval against a method that assumes less.

When it does not help

  • Very small samples. The bootstrap only reuses what you have, so the interval can be too narrow when the sample is small or the data are strongly skewed.
  • Clustered or repeated data. Resample whole patients, or whole clusters such as wards, never single rows.
  • A sample that is biased or not representative. Resampling it does not repair it.
  • Extreme statistics, such as the maximum, and very small or very large percentiles.

How to do it in R

The first block does the whole procedure in base R. The second uses the boot package, which also gives the BCa interval (a refined version of the percentile interval) and resamples within groups.

R
# The bootstrap by hand: the mean of one variable
x <- na.omit(d$length_of_stay)       # missing values out first
set.seed(2026)                       # the same interval every time you run it
boot_means <- replicate(10000, mean(sample(x, replace = TRUE)))
quantile(boot_means, c(0.025, 0.975))   # 95% percentile interval
R
# With the boot package: the median, with percentile and BCa intervals
library(boot)
set.seed(2026)
b <- boot(x, statistic = function(data, i) median(data[i]), R = 10000)
boot.ci(b, type = c("perc", "bca"))

# Difference in medians between two groups: resample within each group
diff_median <- function(data, i) {
  s <- data[i, ]
  median(s$length_of_stay[s$group == "a"]) - median(s$length_of_stay[s$group == "b"])
}
b2 <- boot(d, diff_median, R = 10000, strata = factor(d$group))
boot.ci(b2, type = "perc")

How to do it in SPSS

Many SPSS dialogs have a Bootstrap button. It needs the Bootstrapping option of your SPSS licence. Or paste this syntax in a syntax window (File > New > Syntax):

SPSS
* Bootstrap 95% CI for the mean (needs the Bootstrapping option of SPSS).
BOOTSTRAP
  /SAMPLING METHOD=SIMPLE
  /VARIABLES INPUT=length_of_stay
  /CRITERIA CILEVEL=95 CITYPE=BCA NSAMPLES=5000
  /MISSING USERMISSING=EXCLUDE.
DESCRIPTIVES VARIABLES=length_of_stay
  /STATISTICS=MEAN STDDEV.

What to report

  • That you used a bootstrap confidence interval, which type (percentile or BCa), and the number of resamples.
  • The seed, so that others can reproduce the interval.
  • The number of patients actually used, after missing values.
  • The estimate from your original sample next to the interval.

Further reading

  • Efron B, Tibshirani RJ. An Introduction to the Bootstrap. Chapman & Hall; 1993.
  • Davison AC, Hinkley DV. Bootstrap Methods and their Application. Cambridge University Press; 1997.

Back to your choice

Go on with the test chooser, or read about the sign test and the one-sample t-test.