Skip to main content

penguins-lm2

lm
en
regression
penguins
Published

September 12, 2024

Consider the dataset penguins. Compute a linear model with body mass as output variable (DV) and a) flipper length and b) sex as input (IV).

  1. Tidy up the data set, if and where needed.
  2. Report the coefficients and interpret them.
  3. Plot the model and the coefficients.
  4. Report the model fit (R squared).
  5. BONUS: predict() the weight of an average flipper-sized animal (male and female). Check out the internet for examples of how to do so in case you need support.

0.1 Setup

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(easystats)
## # Attaching packages: easystats 0.7.6.3 (red = needs update)
## ✖ bayestestR  0.19.0   ✔ correlation 0.8.8 
## ✖ datawizard  1.4.0    ✔ effectsize  1.0.3 
## ✖ insight     1.5.4    ✖ modelbased  0.17.0
## ✖ performance 0.18.2   ✖ parameters  0.29.3
## ✔ report      0.6.4    ✖ see         0.14.2
## 
## Restart the R-Session and update packages with `easystats::easystats_update()`.
library(ggpubr)  # visualization
## 
## Attache Paket: 'ggpubr'
## 
## Die folgenden Objekte sind maskiert von 'package:datawizard':
## 
##     mean_sd, median_mad
# import data:
penguins <- read.csv("https://vincentarelbundock.github.io/Rdatasets/csv/palmerpenguins/penguins.csv")

0.2 Tidy up

penguins_tidier <-
  penguins |> 
  select(body_mass_g, flipper_length_mm, sex) |> 
  drop_na() |> 
  filter(sex != "")  # maybe better to be excluded

Note that, strangely, there are some animals for which the sex is reported as "", an empty string value. This is not the same as NA. However, we may want the exclude such animals of unclear sex.

0.3 Let’s go

lm2 <- 
  lm(body_mass_g ~ flipper_length_mm + sex, 
          data = penguins_tidier)

Plot the model:

plot(estimate_relation(lm2))

plot of chunk unnamed-chunk-9

Aufgabenname: penguins-lm2