Skip to main content

Simple Linear Regression

Regression with one predictor

Fitting, testing, checking, and predicting from a simple linear regression on the lion nose data from lecture.
Author

Bill Perry

📥 Download the R script 📊 Download the data

What regression does

Regression fits a straight line that predicts a response (Y) from a predictor (X). Unlike correlation, the roles matter: X explains Y.

\[Y = \beta_0 + \beta_1 X + \varepsilon\]

  • \(\beta_0\) — the intercept: predicted Y when X = 0
  • \(\beta_1\) — the slope: change in Y for a one-unit increase in X
  • \(\varepsilon\) — the residual: what the line misses

\(H_0: \beta_1 = 0\) (X does not predict Y) vs. \(H_A: \beta_1 \neq 0\)

Data: the proportion of black pigment on a lion’s nose and its age in years. Can we age a lion from its nose?

Assumptions (checked on the residuals, after fitting): linearity, normality, equal variance, independence.

Step 1: Load packages and data

library(tidyverse)
library(car)   # Anova()

lion_df <- read_csv("data/lion_noses.csv")

lion_df
# A tibble: 32 × 2
   proportion_black age_years
              <dbl>     <dbl>
 1             0.21       1.1
 2             0.14       1.5
 3             0.11       1.9
 4             0.13       2.2
 5             0.12       2.6
 6             0.13       3.2
 7             0.12       3.2
 8             0.18       2.9
 9             0.23       2.4
10             0.22       2.1
# ℹ 22 more rows

Step 2: Plot it

lion_df %>%
  ggplot(aes(proportion_black, age_years)) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(x = "Proportion of black on nose", y = "Age (years)") +
  theme_bw()

Step 3: Fit the model

lion_model <- lm(age_years ~ proportion_black, data = lion_df)

coef(lion_model)
     (Intercept) proportion_black 
       0.8790062       10.6471194 

The fitted line is: age = 0.88 + 10.65 × proportion black.

Step 4: Check the assumptions

Check these before trusting any p-value.

par(mfrow = c(2, 2))
plot(lion_model)

par(mfrow = c(1, 1))
  • Residuals vs Fitted: no curve, no funnel shape → linear, equal variance.
  • Q-Q Residuals: points near the line → normal residuals.
  • Scale-Location: flat red line → equal variance.
  • Residuals vs Leverage: no points beyond the dashed Cook’s distance lines → no single point controls the fit.
shapiro.test(residuals(lion_model))

    Shapiro-Wilk normality test

data:  residuals(lion_model)
W = 0.93879, p-value = 0.0692

Step 5: Test the slope

summary(lion_model)

Call:
lm(formula = age_years ~ proportion_black, data = lion_df)

Residuals:
    Min      1Q  Median      3Q     Max 
-2.5449 -1.1117 -0.5285  0.9635  4.3421 

Coefficients:
                 Estimate Std. Error t value Pr(>|t|)    
(Intercept)        0.8790     0.5688   1.545    0.133    
proportion_black  10.6471     1.5095   7.053 7.68e-08 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.669 on 30 degrees of freedom
Multiple R-squared:  0.6238,    Adjusted R-squared:  0.6113 
F-statistic: 49.75 on 1 and 30 DF,  p-value: 7.677e-08

Reading the output:

  • Estimate for proportion_black: the slope. Each +0.1 in proportion black adds about 1.06 years.
  • Std. Error, t value, Pr(>|t|): the test of \(H_0: \beta_1 = 0\).
  • Multiple R-squared: the share of variation in age explained by the nose (0.62).
  • F-statistic: the same test as the slope’s t (F = t²) with one predictor.

The ANOVA table splits the variation into the part the line explains and the residual:

anova(lion_model)
Analysis of Variance Table

Response: age_years
                 Df  Sum Sq Mean Sq F value    Pr(>F)    
proportion_black  1 138.544 138.544   49.75 7.677e-08 ***
Residuals        30  83.543   2.785                      
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Step 6: Predict

Predict the age of a lion whose nose is 50% black. A confidence interval is for the mean age of all such lions; a prediction interval is for one lion, so it is wider.

new_lion <- tibble(proportion_black = 0.50)

predict(lion_model, new_lion, interval = "confidence")
       fit      lwr     upr
1 6.202566 5.388042 7.01709
predict(lion_model, new_lion, interval = "prediction")
       fit      lwr     upr
1 6.202566 2.698512 9.70662

Step 7: Write it up

lion_sum <- summary(lion_model)
lion_f   <- lion_sum$fstatistic

Lion age increased with the proportion of black on the nose (age = 0.88 + 10.65 × proportion black; F1,30 = 49.8, p < 0.001, R² = 0.62).

Report the equation, F (or t) with df, p, and R². Show the scatterplot with the fitted line.