1.40 Linear Regression

Describing Variable Relationships

Direction

A set of two plots, with the left labeled 'Direction- Positive' and the right labeled 'Direction- Negative'. On the left, the points start in the bottom left and spread to the top right; on the right, the points start in the top left and spread to the bottom right. Text on the top of the left plot emphasizes this relationship - 'Low values of x -> low values of y, high values of x -> high values of y'; similar text on the top of the right plot emphasizes the hallmark of a negative relationship - 'low values of x -> high values of y, high values of x -> low values of y'.

Positive slope: as \(x\) increases, \(y\) increases too.

Negative slope: as \(x\) increases, \(y\) decreases.

Form

The form of an association is whether it follows a linear pattern, or some sort of more complicated pattern - periodic, polynomial (quadratic, cubic, etc.)

A set of 4 plots showing linear, quadratic, periodic, and a dinosaur in the pattern of the points. The linear form has points increasing from left to right across the plot in a straight line. In the quadratic form, the points make a 'u' shape. In the periodic form, there is a up-down-up-down-up pattern. And the dinosaur points show the outline of a t-rex.

Strength

The strength of an association indicates how well the value of one variable can be predicted if you know the value of the other variable.

A series of plots which show no association, very weak association (cor = 0.2), weak association (cor = 0.4), moderate association (cor = 0.65), strong association (cor = 0.85), and very strong association (cor = 0.99)

We can describe the strength and direction of a linear relationship using the correlation coefficient

Correlation Coefficient

XKCD about weak correlations. It shows two panels - the first has a straight line, and the annotation in red of R^2 = 0.06. The second shows the same dots with some connected with red lines, constellation style, labeled Rexthor, the dog-bearer. Underneath, the text says 'I don't trust linear regressions when it's harder to guess the direction of the correlation from the scatter plot than to find new constellations on it.' The mouseover text says 'The 95% confidence interval suggests that Rexthor's dog could also be a cat, or possibly a teapot.'

  • Correlation only measures linear relationships

  • It is not an appropriate summary measure for non-linear relationships

Linear Regression, Explained Visually

Head-Brain Relationship

R.J. Gladstone (1905). “A Study of the Relations of the Brain to the Size of the Head”, Biometrika, Vol. 4, p 105-123.

  • 237 autopsies at Middlesex Hospital, London

“No case has been included in which the brain showed a condition which would have obviously affected its weight, nor have individuals with foreign or Jewish names been included.”

😱 This is not acceptable in 2026, yikes. But, in 1905, this was reasonable for science. 😬

Variables:

  • Gender
  • Age (20 - 45 or 46+)
  • Brain Weight (g)
  • Head Size (cubic cm)

An illustration of how the dimensions of the brain were measured, showing the circumference and the cross-sectional measurements.

Skull Measurement Diagram from the paper

Graphical Summary

A scatterplot of Head Circumference (cm) on the x-axis and Brain Weight (g) on the y-axis. Points are colored by Gender, with orange points representing females and purple points representing males. There is a linear relationship between head circumference and brain weight, with orange (female) data points concentrated on the lower left 2/3 of the plot, and the purple points are concentrated on the upper right 2/3 of the plot. There is a fair amount of overlap between the two groups of points in the middle of the plot. As the head circumference increases, it appears that the variability in brain weight also increases, as the points lie on a fan-shaped wedge through the plot.

  • Brain weight is hard to measure (unless you’re dead)

  • Head circumference is easy to measure

Goal: Predict brain weight using head circumference

Fitting the Best Line

Fitting the Best Line

Slope and Intercept are Random Variables

library(infer)
brain <- read_csv("head-brain.csv") |>
  mutate(Gender = factor(Gender, labels = c("Male", "Female")))

brain_samples <- brain |>
  rep_slice_sample(n = 100, replace = F, reps = 50) |>
  nest(data = -replicate) |>
  mutate(reg = map(data, ~lm(Brain ~ Head, data = .)),
         coefs = map(reg, coef)) |>
  unnest_wider(coefs)
1
Sample 100 points from the dataset, and repeat 50 times.
2
Collapse all columns except for replicate into a data set. This makes it easier to fit a regression to each set of points separately.
3
Fit the regression to each unique dataset.
4
Get the coefficients from each regression object – they are in a vector, so this result will still be nested.
5
Unnest the coefficients so that each coefficient has its own column.

Slope and Intercept are Random Variables

The sampled points produce different regression lines:

A plot of head size by brain weight, with 50 regression lines, each drawn from a sample of 100 data points. The slope and intercept are different for each sample, indicating that they are random variables.

Slope and Intercept are Random Variables

reg <- lm(Brain ~ Head, 
          data = brain)
summary(reg)

Call:
lm(formula = Brain ~ Head, data = brain)

Residuals:
    Min      1Q  Median      3Q     Max 
-175.98  -49.76   -1.76   46.60  242.34 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 325.57342   47.14085   6.906 4.61e-11 ***
Head          0.26343    0.01291  20.409  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 72.43 on 235 degrees of freedom
Multiple R-squared:  0.6393,    Adjusted R-squared:  0.6378 
F-statistic: 416.5 on 1 and 235 DF,  p-value: < 2.2e-16

Density plots for the slope and intercept of the regression lines. The plot on the left shows the slope, the plot on the right shows the intercept.

Density plots for the slope and intercept of the regression lines. The plot on the left shows the slope, the plot on the right shows the intercept.

Hypothesis Tests for Linear Regression

\[y = \beta_0 + \beta_1 x + \epsilon\] \[\hat y = \beta_0 + \beta_1 x\]

\(H_0:\) There is no relationship between \(x\) and \(y\) (substitute variables), e.g.

\(H_0: \beta_1 = 0,\) that is, there is no relationship between head circumference and brain weight.


\(H_A:\) There is a relationship between \(x\) and \(y\), e.g.,

\(H_A: \beta_1 \neq 0,\) that is, there is a relationship between head circumference and brain weight.

Hypothesis Tests for Linear Regression


Call:
lm(formula = Brain ~ Head, data = brain)

Residuals:
    Min      1Q  Median      3Q     Max 
-175.98  -49.76   -1.76   46.60  242.34 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 325.57342   47.14085   6.906 4.61e-11 ***
Head          0.26343    0.01291  20.409  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 72.43 on 235 degrees of freedom
Multiple R-squared:  0.6393,    Adjusted R-squared:  0.6378 
F-statistic: 416.5 on 1 and 235 DF,  p-value: < 2.2e-16

We reject \(H_0\) (t = 20.409, p < 0.0001) and conclude that it is more likely that there is a relationship between head size and brain weight. As head size increases by 1 cm, there is a corresponding increase in estimated brain weight of 0.263 grams.

Confidence Intervals for Linear Regression

\[\text{Estimate} \pm \text{Multiplier}\cdot\text{Std. Error}\]


Call:
lm(formula = Brain ~ Head, data = brain)

Residuals:
    Min      1Q  Median      3Q     Max 
-175.98  -49.76   -1.76   46.60  242.34 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 325.5734    47.1409    6.91  4.6e-11 ***
Head          0.2634     0.0129   20.41  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 72.4 on 235 degrees of freedom
Multiple R-squared:  0.639, Adjusted R-squared:  0.638 
F-statistic:  417 on 1 and 235 DF,  p-value: <2e-16
  • Estimate: 0.263
  • Multiplier: 1.96 (get with qnorm)
  • Std Error: 0.013

We are 95% confident that the average population increase in brain weight (g) when head size increases by 1 cm is between 0.238 and 0.289.