Bootstrapping and
confidence intervals


Data Analysis for Psychology in R 2

Dr. Elizabeth Pankratz (elizabeth.pankratz@ed.ac.uk)


Department of Psychology
University of Edinburgh
2026–2027

Course overview


Introduction to linear models
(with Dr. Patrick Sturt)
Introduction to linear regression
Interpreting linear models
Testing predictors and evaluating linear models
F-tests and model comparison
Practice analysis: Linear models
Extending linear models
(with Dr. Elizabeth Pankratz)
Categorical predictors and treatment coding
Sum coding
Assumptions and diagnostics
Bootstrapping and confidence intervals
Practice analysis: Categorical predictors (led by Patrick)
Interactions
(with Dr. Elizabeth Pankratz)
Mean-centering and numeric/categorical interactions
Numeric/numeric interactions
Categorical/categorical interactions
Testing simple effects and correcting for multiple comparisons
Practice analysis: Interactions (led by Patrick)
Logistic regression
(with Dr. Elizabeth Pankratz)
Probabilities and log-odds
Modelling binary outcomes with logistic regression
Interactions, assumptions, diagnostics, comparisons
Practice analysis: Logistic regression (led by Patrick)
Report feedback, exam prep, full-course Q&A

Building an analysis workflow

This week’s learning objectives


What is bootstrapping?

How does bootstrapping work?

When would we bootstrap a linear model’s estimates?

What does “confidence” in “95% confidence interval” actually mean?

What’s a bootstrap?


Next question: What do bootstraps have to do with stats?

We’ll come back to this question in a bit!

What problem does bootstrapping solve?

What problem does bootstrapping solve?

The core challenge in statistics:

We want to know something about a population, but we can’t possibly study the whole population.

We can only draw a sample from it.


We ask a question of the sample (e.g., what’s the mean?). And we get a single answer (e.g., 10).

How do we know if this answer is representative of the population overall?


Ideally, we would take many samples from the population, get their means, and compare them all.

But we are constrained to this one sample.


One strategy: Use mathematical tools like the formula you learned for the standard error.

But those tools only give valid information if the assumptions behind them are reasonable (e.g., that errors are normally distributed).

Otherwise, they give bad estimates of the actual variability in the population.

Another strategy: Treat the sample we’ve drawn as if it is a population, and draw many new samples from it.

This strategy doesn’t rely on the same mathematical assumptions.

This method is called bootstrapping.

Bootstrapping: Resampling from a sample

Why is bootstrapping an OK thing to do?


  • Our main sample contains the best possible information we can get about a population. (Basically anything that we guessed would be worse.)

  • If our main sample was drawn at random, it will probably look quite a bit like the population.

  • So, drawing bootstrap samples from our main sample and summarising them will probably create a sampling distribution that looks a lot like the one we would get if we could sample repeatedly from the whole population.

Generate bootstrapped samples by sampling with replacement

Imagine our main sample contains five shapes:

If we could only sample each data point once:

Each sample is just a shuffled version of the original.

If we could sample each data point repeatedly:

Each sample is distinct!

To create diverse samples, we need to be able to sample each data point repeatedly.

This is called “sampling with replacement”.

Bootstrapping demo

Activity: Bootstrapping our own samples


Population: All numbers from 1 to 100 (population mean = 50.5)

Main sample (size 10):

6     8     17     32     70     76     79     81     85     93


Bootstrapped samples (size 10): Over to you!

What just happened?

Think of bootstrapping as a useful approximation, not a perfect estimate.

  • The main sample was not a perfect representation of the population (different mean, for example).
  • Bootstrapping is only as good as the main sample.

Estimating variability using the sampling distribution


  • For each bootstrapped sample, we compute a summary statistic.
  • We represent all those summary statistics together using a sampling distribution.
  • A sampling distribution is useful because with it, we can estimate a parameter’s variability (i.e., standard error, confidence intervals).


The standard error of the parameter = the standard deviation of the sampling distribution.

The 95% CI of the parameter = the range from the 2.5th percentile to the 97.5th percentile of the sampling distribution.

  • 2.5th percentile = 2.5% of values in the distribution are below this value
  • 97.5th percentile = 97.5% of values in the distribution are below this value

Any summary statistic can be bootstrapped


There’s nothing special about the mean.

We can use bootstrapping for any measure that summarises data points.


Most usefully for us: We can bootstrap the parameters of a linear model.



NOTE: You will never need to do bootstrapping step by step, the way I’m about to show you.

My goal is to show you what happens behind the scenes inside R.

That way you’ll know what’s going on when you run the code.

Bootstrapping LM, step by step

Our main sample

Doesn’t look good for equal variance of errors…

Bootstrapping LM, step by step (Sample 1)

  1. From the main sample, take a new sample with replacement. You always sample the same number of data points as there were in the main sample. One sample bootstrapped.
  1. Fit the linear model to the bootstrapped sample. Save the intercept and slope estimates.
  1. Do 1 and 2 again and again. We’ll build a sampling distribution of intercept estimates and a sampling distribution of slope estimates.

(1)

(2)


Call:
lm(formula = y ~ x, data = boot_samples[[1]])

Coefficients:
(Intercept)            x  
      2.462       -0.661  

(3)

Bootstrapping LM, step by step (Sample 1)

(1)

(2)


Call:
lm(formula = y ~ x, data = boot_samples[[1]])

Coefficients:
(Intercept)            x  
      2.462       -0.661  

(3)

Bootstrapping LM, step by step (Sample 2)

(1)

(2)


Call:
lm(formula = y ~ x, data = boot_samples[[2]])

Coefficients:
(Intercept)            x  
      2.160       -0.663  

(3)

Bootstrapping LM, step by step (Sample 3)

(1)

(2)


Call:
lm(formula = y ~ x, data = boot_samples[[3]])

Coefficients:
(Intercept)            x  
      2.211       -0.684  

(3)

Sampling distribution of the intercept




\(\leftarrow\) As we draw more bootstrapped samples, the mean intercept (solid line) will match the original sample intercept better and better.


The standard deviation of the sampling distribution (dotted lines) = the standard error of the intercept, which we use to calculate the intercept’s 95% CI.


We can’t trust the original sample’s 95% CI (because assumptions weren’t reasonable), but we can trust the bootstrapped 95% CI.

Sampling distribution of the slope




\(\leftarrow\) As we draw more bootstrapped samples, the mean slope (solid line) will come to match the original sample’s slope.


The standard deviation of the sampling distribution (dotted lines) = the standard error of the slope, which we’ll use to calculate the slope’s 95% CI (which we can actually trust).


Take-home message: Bootstrapping is a way of estimating a parameter’s standard error, and thus 95% CI, when assumptions about errors aren’t reasonable.


What questions do you have right now?

Bootstrapping a linear model in R

Bootstrapping a linear model in R


Fit a standard linear model to the main sample.

mod <- lm(y ~ x, data = orig_data)


Use Boot() from the package car to draw R = 1000 bootstrapped samples.

By default, the bootstrapped samples will contain the same number of data points as the original sample.

mod_boot <- car::Boot(mod, R = 1000)


Now summary() will give us a standard error for each parameter (which is the same as the standard deviation of each parameter’s sampling distribution).

summary(mod_boot)

Number of bootstrap replications R = 1000 
            original bootBias bootSE bootMed
(Intercept)    2.115 0.006860  0.183   2.124
x             -0.813 0.000135  0.160  -0.811

Our bootstrapping closely matches Boot()


Number of bootstrap replications R = 1000 
            original bootBias bootSE bootMed
(Intercept)    2.115 0.006860  0.183   2.124
x             -0.813 0.000135  0.160  -0.811


Get 95% confidence intervals


confint(
  mod_boot, 
  type = 'perc'  # type = 'perc' gives us the 2.5th and 97.5th percentiles 
                 # from the bootstrapped distribution
)
Bootstrap percent confidence intervals

            2.5 % 97.5 %
(Intercept)  1.74  2.477
x           -1.14 -0.526


Hypothesis tests:

Because these 95% confidence intervals don’t include 0,

  • we can reject the H0 that the Intercept = 0 at \(\alpha\) = .05.
  • we can reject the H0 that the slope = 0 at \(\alpha\) = .05.

Reporting results of a bootstrapped linear model


Report the coefficient’s estimate and 95% CI as usual (there’s no p-value so you can leave that out).

Also make sure to include something like


Because we could not assume normality of errors or equal variance of errors, all the standard errors and 95% CIs we report here were estimated from 1000 bootstrapped samples.

So, what do bootstraps have to do with stats?

What do bootstraps have to do with stats?


Bradley Efron is the inventor of bootstrapping, and he introduced it in his 1979 paper by saying it’s a method

(Reader, the reasons did not become obvious.)


The internet’s best guess: it has to do with the saying “pull yourself up by your bootstraps”.

  • Previously, this saying meant “a pointless/useless act”.
  • These days, people use it to mean that you’re independent, a self-starter.
  • We think that “bootstrapping” is used in that sense: you can estimate variability from just a single sample, so in a way, your sample is “pulling itself up by its bootstraps” (?)

Confidence intervals deep dive

Interpreting confidence intervals


It’s common to imagine that a 95% CI means that there’s a 95% probability that our true value is in the interval.

But this isn’t what they really mean! :(

So what do confidence intervals mean?


Confidence intervals rely on the idea of “hypothetical repeated sampling”.

  • hypothetical: we’re imagining something that isn’t really happening
  • repeated: we’re imagining that we’re doing something over and over again
  • sampling: we’re imagining that we’re drawing samples from the same population over and over again


Side note: Hypothetical repeated sampling is different from bootstrapping because

  • hypothetical repeated sampling imagines repeatedly drawing samples from the population
  • bootstrapping actually repeatedly draws samples from our original sample

We can simulate hypothetical repeated sampling by defining our own “reality”


Let’s look at the association between hours of sleep and the number of riddles solved in an hour.

In the real true population (which we have defined, in order to illustrate how confidence intervals work),
the true association looks like this:

Repeatedly draw samples from that true population and fit a linear model to each

Some 95% CIs of the slope don’t contain the true population value of 4

How many CIs contain the true value?


If we’re using a 95% CI, then under hypothetical repeated sampling:

  • 95 out of every 100 CIs will contain the true population value.
  • 5 of every 100 CIs will not.

How many CIs contain the true value?


If we’re using a 60% CI, then under hypothetical repeated sampling:

  • 60 out of every 100 CIs will contain the true population value.
  • 40 of every 100 CIs will not.

So what is “confidence”?


“Confidence” refers to the process that generates the intervals.

  • A 95% confidence interval is generated using a process that, in the long run, makes 95 intervals that contain the true population value for every 5 intervals that do not.

  • In general: a \(n\)% confidence interval is generated using a process that, in the long run, makes \(n\) intervals that contain the true population value for every 100 \(-~n\) that do not.


This is why it’s not true to say “there’s a 95% probability that the true value is in the 95% CI”.

  • We have no idea whether any given confidence interval contains the true population value.
  • Either it does contain the true value (then the probability = 100%) or it does not (probability = 0%).
  • For a given confidence interval, we have no way of knowing how likely each of those two outcomes is.

Building an analysis workflow

Building an analysis workflow

Building an analysis workflow

What questions do you have right now?


Back matter

Revisiting this week’s learning objectives

br


What is bootstrapping?

  • An alternative way of estimating the variability of a coefficient estimate (i.e., the standard error and confidence interval).
  • Unlike regular linear models, bootstrapping doesn’t require that errors be normally distributed or equal across the range of the predictor.

How does bootstrapping work?

  • It is based on repeatedly resampling from a given sample and then computing summary statistics on those samples.
  • By combining those summary values, we get a sampling distribution: our best guess about how that statistic is distributed in the population.

Revisiting this week’s learning objectives


When would we bootstrap a linear model’s estimates?

  • When a linear model’s residuals don’t fulfil the model’s assumptions about normality of errors and equal variance of errors.
  • Those assumptions are what makes the formulae for estimating standard error and confidence intervals work.
  • When those assumptions aren’t reasonable, then we need a different way of estimating standard error and CIs: bootstrapping.

What does “confidence” in “95% confidence interval” actually mean?

  • “Confidence” is a property of the process that generates confidence intervals.
  • A 95% confidence interval is generated by a process that produces 95 intervals that contain the true value for every 5 that do not.
  • 95% confidence interval \(\neq\) 95% probability that the interval contains the true value!!
    • Either the 95% CI contains the true value or it doesn’t. We have no idea how probable each of those scenarios is.

This week


Tasks:


Work on exercises in labs


Complete the weekly quiz

Get support:


Consult the flash cards


Ask questions anonymously on Piazza


We really like seeing you in office hours!

95% CIs from the sampling distribution’s quantiles

confint(mod_boot, type = 'perc')
Bootstrap percent confidence intervals

            2.5 % 97.5 %
(Intercept)  1.74  2.477
x           -1.14 -0.526

The CIs from Boot() are close to the 2.5th and 97.5th quantiles of our manually created sampling distribution:

quantile(
  boot_coefs_df_1k$Intercept,
  probs = c(0.025, 0.975)
)
 2.5% 97.5% 
 1.73  2.47 
quantile(
  boot_coefs_df_1k$x,
  probs = c(0.025, 0.975)
)
  2.5%  97.5% 
-1.151 -0.532