Data Analysis for Psychology in R 2
Department of Psychology
University of Edinburgh
2026–2027
|
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 |
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?
Next question: What do bootstraps have to do with stats?
We’ll come back to this question in a bit!
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.
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.
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”.
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!
Think of bootstrapping as a useful approximation, not a perfect estimate.
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.
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.
Doesn’t look good for equal variance of errors…
(1)
(2)
Call:
lm(formula = y ~ x, data = boot_samples[[1]])
Coefficients:
(Intercept) x
2.462 -0.661
(3)
(1)
(2)
Call:
lm(formula = y ~ x, data = boot_samples[[1]])
Coefficients:
(Intercept) x
2.462 -0.661
(3)
(1)
(2)
Call:
lm(formula = y ~ x, data = boot_samples[[2]])
Coefficients:
(Intercept) x
2.160 -0.663
(3)
(1)
(2)
Call:
lm(formula = y ~ x, data = boot_samples[[3]])
Coefficients:
(Intercept) x
2.211 -0.684
(3)

\(\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.

\(\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?
Fit a standard linear model to the main sample.
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.
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).
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
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,
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.
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”.
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! :(
Confidence intervals rely on the idea of “hypothetical repeated sampling”.
Side note: Hypothetical repeated sampling is different from bootstrapping because
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:
If we’re using a 95% CI, then under hypothetical repeated sampling:
If we’re using a 60% CI, then under hypothetical repeated sampling:
“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”.
br
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?
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!
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: