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.