Palmer Penguins and regression with a single predictor

Application exercise
Modified

March 19, 2024

In this application exercise we will be studying penguins. The data can be found in the palmerpenguins package and we will use tidyverse and tidymodels for data exploration and modeling, respectively.

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

# drop rows with missing values
data("penguins")
penguins <- penguins |>
  drop_na(flipper_length_mm, body_mass_g)

Please read the following context and take a skim of the data set before we get started.

This data set comprising various measurements of three different penguin species, namely Adelie, Gentoo, and Chinstrap. The rigorous study was conducted in the islands of the Palmer Archipelago, Antarctica. These data were collected from 2007 to 2009 by Dr. Kristen Gorman with the Palmer Station Long Term Ecological Research Program, part of the US Long Term Ecological Research Network. The data set is called penguins.

skim(penguins)

Our goal is to understand better how various body measurements and attributes of penguins relate to their body mass. First, we are going to investigate the relationship between a penguins’ flipper lengths and their body masses.

ggplot(data = penguins,
       mapping = aes(x = flipper_length_mm, y = body_mass_g)) +
  geom_point()

Correlation

  • Demo: What is the correlation between flipper length and body mass of penguins?
# option 1
summarize(penguins, r = cor(flipper_length_mm, body_mass_g))

# option 2
cor(penguins$flipper_length_mm, penguins$body_mass_g)

Defining, fitting, and summarizing a model

  • Demo: Write the population model below that explains the relationship between body mass and flipper length.
$$
body~mass = \beta_0 + \beta_1 \times flipper~length
$$

\[ body~mass = \beta_0 + \beta_1 \times flipper~length \]

  • Demo: Fit the linear regression model and display the results. Write the estimated model output below.
Tip

Use tidy() to print the model output in a readable, tabular format.

bm_fl_fit <- TODO() |>
  fit(TODO ~ TODO, data = penguins)

tidy(bm_fl_fit)

\[ \widehat{body~mass} = TODO + TODO \times flipper~length \]

  • Your turn: Interpret the slope and the intercept in the context of the data.

    • Intercept:

    • Slopes:

  • Your turn: Recreate the visualization from above, this time adding a regression line to the visualization geom_smooth(method = "lm").

ggplot(penguins,
       aes(x = flipper_length_mm, y = body_mass_g)) +
  geom_point() +
  TODO
  • Your turn: What is the estimated body mass for a penguin with a flipper length of 210?
Tip

Use predict() to generate predicted values from a fitted model. Provide the new data in a data frame as the new_data argument.

# add code here
  • *Your turn: What is the estimated body mass for a penguin with a flipper length of 100?
# add code here

Another model

  • Your turn: A different researcher wants to look at body weight of penguins based on the island they were recorded on. How are the variables involved in this analysis different?

    Add response here.

  • Demo: Make an appropriate visualization to investigate this relationship below. Additionally, calculate the mean body mass by island.

Note

Choose a visualization appropriate for a categorical and continuous variable.

# add code here
# add code here
  • Demo: Change the geom of your previous plot to geom_point(). Use this plot to think about how R models these data.
# add code here
  • Your turn: Fit the linear regression model and display the results. Print the estimated model output below.
bm_island_fit <- linear_reg() |>
  fit(TODO ~ TODO, data = penguins)

# add code here
  • Your turn: Interpret each coefficient in context of the problem.

    • Intercept:

    • Slopes:

  • Demo: What is the estimated body weight of a penguin on Biscoe island? What are the estimated body weights of penguins on Dream and Torgersen islands?

predict(bm_island_fit, new_data = tibble(island = c("Biscoe", "Dream", "Torgersen")))