
SDS 291
October 27, 2025
summary() output!Research Question
Does penguin species moderate/modify the relationship between bill length and bill depth?
\[\begin{gather*} {H_0:}\quad \mathbb{E}[\texttt{Depth}_i | x] = \beta_0 + \beta_1\texttt{Length}_i + \beta_2\texttt{Adelaide}_i + \beta_3\texttt{Gentoo}_i\\[-0.2em] vs.\\[-0.2em] {H_A:}\quad \mathbb{E}[\texttt{Depth}_i | x] = \beta_0 + \beta_1\texttt{Length}_i + \beta_2\texttt{Adelaide}_i + \beta_3\texttt{Gentoo}_i \\ \quad\quad + \beta_4\left(\texttt{Length}_i\right)\left(\texttt{Adelaide}_i\right) + \beta_5\left(\texttt{Length}_i\right)\left(\texttt{Gentoo}_i\right) \end{gather*}\]


Tip
Can’t just look at the individual \(p\)-values for the two interaction terms
Call:
lm(formula = bill_depth_mm ~ bill_length_mm * species, data = penguins)
Residuals:
Min 1Q Median 3Q Max
-2.6574 -0.6675 -0.0524 0.5383 3.5032
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.56914 1.70983 4.427 1.29e-05 ***
bill_length_mm 0.22221 0.03493 6.361 6.55e-10 ***
speciesAdelie 3.83998 2.05398 1.870 0.0624 .
speciesGentoo -2.31813 2.16945 -1.069 0.2860
bill_length_mm:speciesAdelie -0.04338 0.04558 -0.952 0.3419
bill_length_mm:speciesGentoo -0.01737 0.04480 -0.388 0.6985
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.9548 on 336 degrees of freedom
(2 observations deleted due to missingness)
Multiple R-squared: 0.7697, Adjusted R-squared: 0.7662
F-statistic: 224.5 on 5 and 336 DF, p-value: < 2.2e-16

\[\begin{align*} SST &= \sum_{i=1}^{n}(y_i - \bar{y})^2\\[0.25em] df &= n - 1 \\[0.25em] MST &= \frac{SST}{n-1} \end{align*}\]

\[\begin{align*} SSM &= \sum_{i=1}^{n}(\widehat{y}_i - \bar{y})^2\\[0.25em] df &= k\\[0.25em] MSM &= \frac{SSM}{k} \end{align*}\]

\[\begin{align*} SSE &= \sum_{i=1}^{n}(y_i - \widehat{y}_i)^2\\[0.25em] df &= n - k - 1\\[0.25em] MSE &= \frac{SSE}{n - k - 1} \end{align*}\]
Sum of Squares Decomposition for SLR
Analysis of Variance Table
Response: bill_depth_mm
Df Sum Sq Mean Sq F value Pr(>F)
bill_length_mm 1 73.47 73.473 19.884 1.12e-05 ***
Residuals 340 1256.36 3.695
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Sequential Sum of Squares Decomposition for MLR
Analysis of Variance Table
Response: bill_depth_mm
Df Sum Sq Mean Sq F value Pr(>F)
bill_length_mm 1 73.47 73.47 80.5908 <2e-16 ***
species 2 949.16 474.58 520.5569 <2e-16 ***
bill_length_mm:species 2 0.87 0.44 0.4785 0.6202
Residuals 336 306.32 0.91
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Note
lm() model statement, you will end up with a different partition of the SSMWe can use the sum of squares decomposition to conduct two main flavors of hypothesis testing for (multiple) linear regression models:
Plot Twist!
Although your textbook presents them as two separate ideas, these two tests are actually the same thing! All \(F\)-tests are nested \(F\)-tests by design.
We have a “small” model and a “large” model, where the “small” model is completely contained in the “large” model!
In general, for models to be nested we:
Warning
This last condition is especially important when we have missing data in one or more of the predictor terms \(\implies\) filter out observations that have missing data in any of the variables in the full model before fitting any of the models of interest!
If (and only if) two linear regression models are nested in one another, then the model sums of squares (SSMs) for the two models are connected:
\[\begin{align*} \underbrace{SSM(x_1, x_2, x_3)}_{\substack{\text{variability explained}\\[0.25em] \text{by the full model}}} &= \underbrace{SSM(x_1, x_2)}_{\substack{\text{variability explained}\\[0.25em] \text{by the reduced model}}} + \underbrace{SSM(x_3 | x_1, x_2)}_{\substack{\text{additional variability explained} \\[0.25em] \text{by the extra term(s)}}} \end{align*}\]
\[\begin{align*} F &= \frac{ \left( \frac{SSM_\text{reduced} - SSM_\text{full}}{p} \right) }{\left( \frac{SSE_\text{full}}{n-k-1} \right) } \\ &= \frac{(SSE_\text{reduced} - SSE_\text{full})/p}{MSE_\text{full}} \end{align*}\]
pf(F, df1, df2, lower.tail = FALSE)Conduct nested F-tests in R using anova(reduced, full):
# Removing all penguins with missing bill length, bill depth, or species
penguins_cc <- penguins |>
drop_na(contains("bill_"), species)
# Fitting the full and reduced models
interact_lm <- lm(bill_depth_mm ~ bill_length_mm * species, data = penguins_cc)
parallel_lm <- update(interact_lm, . ~ . - bill_length_mm:species)
# Conducting the nested F-test
anova(parallel_lm, interact_lm)Analysis of Variance Table
Model 1: bill_depth_mm ~ bill_length_mm + species
Model 2: bill_depth_mm ~ bill_length_mm * species
Res.Df RSS Df Sum of Sq F Pr(>F)
1 338 307.20
2 336 306.32 2 0.87243 0.4785 0.6202
anova() outputAnalysis of Variance Table
Model 1: bill_depth_mm ~ bill_length_mm + species
Model 2: bill_depth_mm ~ bill_length_mm * species
Res.Df RSS Df Sum of Sq F Pr(>F)
1 338 307.20
2 336 306.32 2 0.87243 0.4785 0.6202

Research Question
Does penguin species moderate the relationship between bill length and bill depth?
Conclusion
We fail to reject the null hypothesis and conclude that the relationship between bill length and bill depth does not significantly differ across the three penguin species (\(p = 0.62\)).
Research Question
Are any of penguin species, penguin bill length, or their interaction useful in modeling the bill depth of Antarctic penguins?
Call:
lm(formula = bill_depth_mm ~ bill_length_mm * species, data = penguins_cc)
Residuals:
Min 1Q Median 3Q Max
-2.6574 -0.6675 -0.0524 0.5383 3.5032
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.56914 1.70983 4.427 1.29e-05 ***
bill_length_mm 0.22221 0.03493 6.361 6.55e-10 ***
speciesAdelie 3.83998 2.05398 1.870 0.0624 .
speciesGentoo -2.31813 2.16945 -1.069 0.2860
bill_length_mm:speciesAdelie -0.04338 0.04558 -0.952 0.3419
bill_length_mm:speciesGentoo -0.01737 0.04480 -0.388 0.6985
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.9548 on 336 degrees of freedom
Multiple R-squared: 0.7697, Adjusted R-squared: 0.7662
F-statistic: 224.5 on 5 and 336 DF, p-value: < 2.2e-16
Conclusion
We have very strong evidence that at least one of these terms is necessary (\(p < 0.0001\)).
Analysis of Variance Table
Model 1: bill_depth_mm ~ 1
Model 2: bill_depth_mm ~ bill_length_mm * species
Res.Df RSS Df Sum of Sq F Pr(>F)
1 341 1329.83
2 336 306.32 5 1023.5 224.53 < 2.2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Conclusion
We have very strong evidence that at least one of these terms is useful (\(p < 0.0001\)).
Research Question
Does bill length provide additional necessary information about the mean bill depth of penguins after first accounting for penguin species?
\[\begin{gather*} H_0: \quad \mathbb{E}[\texttt{Depth}_i | x] = \beta_0 + \beta_1\texttt{Adelie}_i + \beta_2\texttt{Gentoo}_i\\ vs.\\ H_A: \quad \mathbb{E}[\texttt{Depth}_i | x] = \beta_0 + \beta_1\texttt{Length}_i + \beta_2\texttt{Adelie}_i + \beta_3\texttt{Gentoo}_i \end{gather*}\]


Analysis of Variance Table
Model 1: bill_depth_mm ~ species
Model 2: bill_depth_mm ~ bill_length_mm + species
Res.Df RSS Df Sum of Sq F Pr(>F)
1 339 425.87
2 338 307.20 1 118.67 130.57 < 2.2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Conclusion
There is strong evidence that bill length is associated with bill depth, even after accounting for penguin species (\(p < 0.0001\)).
Tip
The nested \(F\)-test for testing \(k = 1\) predictor terms will always yield the exact same results as the \(t\)-test for that individual term!
Estimate Std. Error t value Pr(>|t|)
(Intercept) 8.6589862 0.86206892 10.04442 6.039045e-21
bill_length_mm 0.1998943 0.01749365 11.42668 8.661124e-26
speciesAdelie 1.9331943 0.22416005 8.62417 2.545386e-16
speciesGentoo -3.1728258 0.14592926 -21.74222 3.473444e-66
Analysis of Variance Table
Model 1: bill_depth_mm ~ species
Model 2: bill_depth_mm ~ bill_length_mm + species
Res.Df RSS Df Sum of Sq F Pr(>F)
1 339 425.87
2 338 307.20 1 118.67 130.57 < 2.2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

SDS 291