Simple linear models

Modeling penguin characteristics

Published

November 3, 2026

Purpose

How do a penguin’s body measurements relate to its body mass? In this AE you will build linear models to answer this question. In Part 1 you will use flipper length to explain and predict body mass. In Part 2 you will use a penguin’s sex instead, and see how a model handles a categorical explanatory variable.

By the end of Part 1 you should be able to:

  • Identify the explanatory and response variables for a research question
  • Describe a relationship with a scatterplot and a correlation
  • Fit a simple linear regression with linear_reg() and fit(), and read the tidy() output
  • Interpret the slope and intercept in context
  • Make predictions, and recognize extrapolation

By the end of Part 2 you should be able to:

  • Fit and interpret a model with a categorical explanatory variable, using indicator variables and a baseline level
  • Connect the estimates of that model to the group means
  • Think about who chose the model and what it leaves out

Instructions

  • Work through the parts in order, and compare your numbers with a neighbor.
  • Use of an LLM is not needed or advised on this AE.
  • Render to PDF before committing to github - once by EOD on Tuesday then again when finished by EOD Thursday

Writing math in Markdown

  • Inline math goes between single dollar signs: $\beta$ renders as \(\beta\).
  • Subscripts use an underscore, with the subscript in curly braces: $\beta_{0}$ renders as \(\beta_{0}\).
  • A hat marks an estimate: $\hat{y}$ renders as \(\hat{y}\).
  • A whole equation on its own line goes between double dollar signs, each on its own line:
$$
y = \beta_{0} + \beta_{1} x + \epsilon
$$

renders as

\[ y = \beta_{0} + \beta_{1} x + \epsilon \]

The data

The penguins data in the palmerpenguins package has measurements of 344 penguins from three species (Adelie, Chinstrap, and Gentoo) on three islands in the Palmer Archipelago, Antarctica. Dr. Kristen Gorman and the Palmer Station Long Term Ecological Research Program collected the data from 2007 to 2009.

Setup

library(tidyverse)
library(tidymodels)
library(palmerpenguins)

pen <- palmerpenguins::penguins
glimpse(pen)

Part 1: Flipper length and body mass

1. Explanatory and response

We want to know whether a penguin’s flipper length helps explain its body mass. Identify the explanatory and response variables, and label them as either \(x\) or \(y\) appropriately.

  • response
  • explanatory

2. Visualize the relationship

Describe the relationship between body mass and flipper length using a scatterplot and correlation coefficient. Discuss the direction, strength and linearity, using the correlation coefficient to justify your answer.

ggplot(pen, aes(x = _______, y = _______)) +
  geom_point() +
  labs(x = "_______", y = "_______")

_______(pen$flipper_length_mm, pen$body_mass_g, use = "complete.obs")

3. Write the model

Write the population model for body mass as a function of flipper length.

4. Fit the model

Fit the model with linear_reg() and fit(), save it as bm_fl_fit, and display the estimates with tidy().

bm_fl_fit <- linear_reg() |>
  fit(_______ ~ _______, data = pen)

_______(bm_fl_fit)

Write the estimated model.

5. Interpret the slope and intercept

Interpret the slope and the intercept in the context of the data. Is the intercept a meaningful number?

6. Visualize the model

Recreate your scatterplot from step 2 and add the regression line.

ggplot(pen, aes(x = flipper_length_mm, y = body_mass_g)) +
  geom_point() +
  _______(method = "_______") +
  labs(x = "Flipper length (mm)", y = "Body mass (g)")

7. Make a prediction

What is the predicted body mass for a penguin with a flipper length of 210 mm? Do the math by hand with the estimated model, then check it with predict().

penguin_210 <- tibble(flipper_length_mm = _______)

bm_fl_fit |>
  predict(new_data = _______)

8. Extrapolation

What is the predicted body mass for a penguin with a flipper length of 100 mm? Check the range of flipper lengths in the data. Should you trust this prediction?

penguin_100 <- tibble(_______ = _______)

_______ |>
  _______(_______ = penguin_100)
pen |>
  summarize(
    min_fl = _______(flipper_length_mm, na.rm = TRUE),
    max_fl = _______(flipper_length_mm, na.rm = TRUE)
  )

Part 2: Sex and body mass

9. A different explanatory variable

A different researcher wants to model body mass using a penguin’s sex. How is this explanatory variable different from flipper length?

10. Visualize and summarize

11 penguins have no recorded sex. Drop them first and save the result as pen_sex.

pen_sex <- pen |>
  drop_na(sex)

Make an appropriate plot of body mass by sex.

ggplot(pen_sex, aes(x = _______, y = _______, color = _______)) +
  _______() +
  labs(x = "Sex", y = "Body mass (g)")

Calculate the mean body mass for each sex.

pen_sex |>
  group_by(_______) |>
  summarize(mean_bm = _______(body_mass_g))

Change the geom in your plot to geom_point(). If a model can only predict one value for each sex, what value would you choose?

ggplot(_______, aes(x = sex, y = _______)) +
  _______() +
  labs(x = "Sex", y = "Body mass (g)")

11. Fit the model

Fit a model for body mass with sex as the explanatory variable, save it as bm_sex_fit, and display the estimates.

bm_sex_fit <- _______() |>
  fit(_______ ~ _______, data = _______)

tidy(_______)

Which sex is the baseline? What is sexmale?

Write the estimated model.

Let \(y\) be body mass (g), and let \(x = 1\) for male penguins and \(x = 0\) for female penguins.

12. Interpret the estimates

Interpret each estimate in context.

How do these estimates relate to the means you calculated in step 10?

13. Predict for each sex

Calculate the predicted body mass for a female and a male penguin by hand, then check your answers with predict().

two_penguins <- tibble(_______ = c("female", "male"))

bm_sex_fit |>
  _______(new_data = two_penguins)

14. Who chose the model?

Discuss with your neighbors:

  1. Who decided that body mass is the response, and that flipper length or sex is the explanatory variable? Could someone else have made different choices?
  2. Each model uses one explanatory variable. What does each one leave out?
  3. The sex model dropped 11 penguins. What species do they belong to, and does dropping them change who the model describes?
pen |>
  filter(_______(sex)) |>
  count(_______)
NoteAcknowledgements and references

This page adapts material from STA199 (Duke, Fall 2025) by Mine Çetinkaya-Rundel and Mary Knox, AE 11 (“Modelling penguins”). Source: sta199-f25/sta199-f25.github.io. Data: palmerpenguins by Allison Horst, Alison Hill, and Kristen Gorman. Modified: wrote prose answers and by-hand calculations; added the flipper range check, and the “Who chose the model?” discussion; used sex instead of island as the categorical explanatory variable; matched the notation to the wk11-d01 slides.