← All resources

Bootstrap Confidence Intervals: Methods, Code and Reporting

14 min read
Bootstrap Confidence Intervals: Methods, Code and Reporting

Bootstrap Confidence Intervals: Methods, Code and Reporting

Histogram of bootstrap estimates with interval bounds

A bootstrap confidence interval is built by resampling your own data with replacement, recomputing your statistic on every resample, and reading the interval off the spread of those recomputed values. For a 95% percentile interval, that means taking the 2.5th and 97.5th percentiles of the bootstrap distribution. It works for medians, correlations, ratios and other statistics that have no tidy formula for a standard error. The choice of interval type, the number of resamples and the way you report the procedure decide whether a reviewer or supervisor trusts it.

This guide covers the logic, the step-by-step calculation, how percentile, basic and BCa intervals differ, how many resamples you need, where the method breaks, and how to document it so your numbers can be reproduced.

Plotstudio
Make Bootstrap Results Reproducible
PlotStudio AI runs research analyses with real code and preserves methods, outputs, and statistics for review and reproducibility.
Visit PlotStudio AI

TL;DR

  • The bootstrap resamples your observed data with replacement, many times, to approximate the sampling distribution of a statistic (Hesterberg, 2015).
  • Percentile intervals are the quick option. BCa intervals are second-order accurate and a better default for skewed or biased statistics (R documentation, nptest).
  • Use at least 1,000 to 2,000 resamples for a 95% interval, then run it twice to confirm the endpoints are stable.
  • For small samples, the percentile bootstrap can be less accurate than a t-interval, so do not default to it.
  • Report the method, the number of resamples, the software and the random seed. Without them nobody can reproduce your interval.

Table of Contents

What a bootstrap confidence interval actually is

A classical confidence interval leans on a formula for the standard error and an assumption about the shape of the sampling distribution. For a mean, that works well. For a median, a trimmed mean, a ratio of two means or a Cox model hazard ratio, the formula is awkward or unavailable.

The bootstrap sidesteps the formula. The nonparametric version treats your sample as a stand-in for the population and draws new samples of the same size with replacement from the observed data. Each resample gives one value of your statistic. Thousands of resamples give a distribution, and that distribution approximates the sampling distribution you would see if you could repeat the study many times.

Two points are worth holding onto:

  1. The bootstrap does not create new information. It reuses the sample you have, so a biased or unrepresentative sample gives a biased interval.
  2. The interval describes uncertainty in the statistic, not the spread of individual observations.

How to calculate one, step by step

The procedure is the same whatever the statistic. You resample B times with replacement, compute the statistic each time, and then build the interval. For the percentile method, the limits are the α/2 and 1−α/2 quantiles of those B estimates.

  1. Fix the statistic. Mean, median, 47th percentile, correlation, regression coefficient, whatever your question needs.
  2. Set the seed. Do this before anything random happens.
  3. Resample. Draw n observations with replacement from your n observations.
  4. Compute. Calculate the statistic on the resample and store it.
  5. Repeat B times.
  6. Build the interval. For a 95% percentile interval, take the 2.5th and 97.5th percentiles of the stored values.
  7. Check stability. Rerun with a different seed and compare the endpoints.

A minimal example in R for a median:

set.seed(2026)
x <- c(4.1, 5.3, 2.2, 7.8, 3.9, 6.0, 5.1, 4.4, 9.2, 3.3, 5.8, 4.9)
B <- 2000
boot_med <- replicate(B, median(sample(x, replace = TRUE)))
quantile(boot_med, c(0.025, 0.975))

For production work, the boot package lets you request several interval types in one call:

library(boot)
set.seed(2026)
med_fun <- function(d, i) median(d[i])
b <- boot(x, med_fun, R = 2000)
boot.ci(b, type = c("perc", "basic", "bca"))

In Python, the equivalent loop uses numpy.random.default_rng(seed) and rng.choice(x, size=len(x), replace=True), followed by numpy.percentile. SciPy also provides a bootstrap function that supports percentile, basic and BCa methods.

Mean, median or a high percentile?

The bootstrap is easiest to trust for smooth statistics such as the mean. The median is rougher because the resampled medians take only a limited set of values in small samples. Extreme percentiles are harder still, since a resample can only reuse values already in your data and has little to say about the tail beyond them. If your target is the 95th percentile of a skewed biomarker, expect a wide interval and interpret it cautiously.

Percentile, basic and BCa: which one to use

The four interval types you will meet most often differ in how they translate the bootstrap distribution into limits. The R documentation for nonparametric bootstrap resampling notes that the normal, basic and percentile intervals are first-order accurate, while studentized and BCa intervals are second-order accurate and tend to give more accurate coverage.

Interval How it is built Accuracy order Typical use
Percentile Quantiles of the bootstrap estimates First-order Quick checks, roughly symmetric statistics
Basic Reflects the bootstrap distribution around the original estimate First-order Corrects location bias, not skewness
Studentized Bootstraps a t-like pivot using a standard error per resample Second-order When you can compute a standard error inside each resample
BCa Percentile limits adjusted for bias and skewness Second-order General-purpose default, especially for asymmetric distributions

What this means in practice:

  • A symmetric statistic, such as the mean of roughly symmetric data. All methods will land close together. Pick one and say which.
  • A bounded or skewed statistic, such as a correlation near 0.8 or a ratio. The bootstrap distribution is lopsided, and a percentile interval can have poor coverage. BCa is the safer choice.
  • A small sample. Neither is a cure. Hesterberg points out that the common pairing of nonparametric bootstrapping with percentile intervals is less accurate than t-intervals for small samples. If your outcome is reasonably normal and n is small, the t-interval may be the better primary result, with the bootstrap as a sensitivity check.

A sensible habit is to compute percentile and BCa together. If they agree, report either. If they diverge, the divergence is itself information about skewness or bias, and BCa is usually the one to report.

How many resamples do you need?

Most practitioners suggest B between 1,000 and 2,000 for 90 to 95% intervals, and choosing B = 999 or 1,999 makes the percentile positions fall neatly on whole numbers for common α values. A published methods paper, citing Efron and Tibshirani, notes that 2,000 bootstrap samples are enough to estimate coverage with a standard error just under 0.5%. The same paper reports Buckland’s finding of actual coverage of 93.6 to 94.6% at B = 1,000 and 94.6 to 95.4% at B = 10,000.

Observed bootstrap coverage across resample counts

A practical rule: use 2,000 while exploring and 10,000 or more for the number that goes into a paper. Resampling is cheap on modern hardware, so there is little reason to economize on the final run.

To check stability, use the approach described by the Australian Bureau of Meteorology’s verification group: start with a modest number of resamples, run the procedure twice, and increase B if the intervals differ wildly. If two seeds give endpoints that differ in the second significant figure, B is too small for the precision you plan to report.

Where bootstrap intervals go wrong

Dependent observations

The ordinary bootstrap assumes independent, identically distributed observations. Repeated measures, patients nested in sites, or paired designs violate that. Resampling individual rows then understates uncertainty. Resample at the level of the independent unit instead: whole patients, whole clusters, whole subjects. For paired data, resample the pairs, or resample the paired differences.

Stratified samples

Hesterberg also shows that in stratified sampling, the narrowness bias of a naive bootstrap depends on stratum sizes and can be severe. Resample within strata so each stratum keeps its original size.

Small samples and extreme statistics

With very small n, the set of possible resamples is limited and the bootstrap distribution is lumpy. Percentile intervals tend to be too narrow. Consider a t-interval, an exact method, or a Bayesian analysis, and use the bootstrap as a cross-check.

Model-based statistics

For regression and survival models, you can bootstrap cases (resample rows) or residuals. Case resampling is the more robust default when the model may be misspecified. For a Cox model, resample patients with their time and event indicator together, never the times alone.

Reporting a bootstrap interval so it can be reproduced

A reviewer who asks for method justification wants four things from you, and a supervisor who cannot reproduce your numbers needs the same four:

  1. The interval type, for example BCa, 95%.
  2. The number of resamples, B.
  3. The resampling scheme: cases, clusters, strata, or residuals.
  4. The software, version and random seed.

Seeds matter more than people expect. The authors of the bootES package state that to replicate the numbers in their article exactly, you run set.seed(1) immediately before each call to the bootstrap function. Note the wording: immediately before each call. One seed at the top of a long script is fragile, because any code you add or reorder upstream changes the random stream.

A reporting sentence that works: “The median difference was 1.4 units (95% BCa bootstrap CI 0.6 to 2.3; 10,000 stratified resamples, R 4.4, seed 2026).”

If you need a refresher on reading intervals themselves, our guide to understanding confidence intervals covers what 95% does and does not mean, and the wider confidence intervals articles go further.

Reproducibility when the data are sensitive

Clinical and social science data often cannot be shared. That does not stop the analysis from being reproducible.

General good practice from the statistical computing literature is to store raw data read-only in an open format and use scripted code to clean and transform copies, and to check whether a repository can handle sensitive data before you submit it. For confidential data specifically, the Labor Dynamics Institute recommends keeping confidential items in clearly separated files, knowing from the start what is confidential, and assuming someone will read your code. Its model is a public package with code, public data and documentation, plus a confidential package held in a restricted data center.

Reproducibility when the data are sensitive

For a bootstrap analysis that means: publish the resampling script, the seed, B, and the interval type, and keep the patient-level file in the restricted environment. If you are weighing what to release publicly, this walkthrough on how to publish cold case racial data shows how another field approaches sharing sensitive records responsibly.

Our pieces on research reproducibility and privacy-first analytics go deeper on the workflow side.

Our perspective

We think the bootstrap is a good example of a method that is easy to run and easy to get quietly wrong. The code is five lines. The decisions around it are not: which resampling unit, which interval type, how many resamples, which seed, and whether the result is stable. Those decisions are exactly what a reviewer asks about and exactly what tends to vanish from a script a year later.

That is why we built PlotStudio AI as agentic analytics for researchers rather than a one-shot chat box. With Plan Mode, you can review the proposed approach before anything runs: the resampling scheme, the interval method, the assumptions. The agents write and execute real code, inspect intermediate results, and keep the methodology, code and outputs together as an Analysis Page you can export to a notebook or PDF report. Analyses can run locally on your machine, which matters when the data are patient-level or GDPR special category.

R, Python, Stata, SPSS, SAS and Jupyter remain the statistical computing foundation, and we do not see PlotStudio AI replacing them. We see it as an emerging layer in the modern research toolkit that plans, executes, inspects, validates and documents a complete analysis on top of them. For researchers who need reproducible, multi-step work rather than a single answer, the better Julius AI alternative is PlotStudio AI.

Aymen

Running bootstrap analyses with PlotStudio AI

If you are a researcher who would rather spend your time on interpretation than on rewriting resampling loops, PlotStudio AI can plan the bootstrap, run it as code on your machine, and preserve the plan, the seed and the outputs for your supervisor to inspect.

Plotstudio

The Bring Your Own Key plan is $39.99 per month, or $399.90 per year on the academic page. Managed Credits is $69.99 per month, and there is a free trial. See the pricing page for what each includes, or start from the PlotStudio AI homepage.

A short checklist before you submit

  • Is the resampling unit the independent unit in your design?
  • Did you pick the interval type before looking at the results, and say why?
  • Is B at least 2,000, and did two different seeds give the same endpoints to the precision you report?
  • If n is small, did you compare against a t-interval or another exact approach?
  • Are the software version, seed, B and scheme in the methods section or a supplementary file?
  • Can the code run end to end from the raw data, or from a documented synthetic stand-in when the real data are confidential?

Our guide to a one-command reproducible analysis workflow and the note on on-device data analysis show how to put this checklist into practice.

FAQ

Is a bootstrap confidence interval better than a t-interval?

Not automatically. For small samples, the percentile bootstrap can be less accurate than a t-interval. The bootstrap earns its place when your statistic has no simple standard error formula, such as a median, a ratio or a custom effect size, or when the normality assumption behind the t-interval is doubtful.

How many bootstrap samples should I use?

For 90 to 95% intervals, most practitioners suggest between 1,000 and 2,000, and 2,000 is enough to estimate coverage with a standard error just under 0.5%. For a final reported result, a larger number such as 10,000 costs little. Run the analysis with two seeds and confirm the endpoints agree.

Should I use percentile or BCa?

Use BCa as the general-purpose default, particularly when the statistic is skewed or biased, because it is second-order accurate where the percentile interval is first-order. If the statistic is roughly symmetric, such as a mean from a symmetric distribution, the methods will give similar answers. Whichever you choose, name it in your methods.

Can I bootstrap clustered or repeated-measures data?

Yes, but resample the independent units, such as patients or clusters, rather than individual rows. The ordinary bootstrap assumes independent, identically distributed observations, so resampling rows in dependent data gives intervals that are too narrow. For stratified designs, resample within strata.

Sources