Skip to main content

penguins-lm

lm
en
regression
penguins
Published

September 12, 2024

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

  1. Report the coefficients and interpret them.
  2. Plot the model and the coefficients.
  3. Report the model fit (R squared).
  4. BONUS: predict() the weight of an average flipper-sized animal. 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)
## 
## 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 Let’s go

lm1 <- lm(body_mass_g ~ flipper_length_mm, data = penguins)

Plot the model:

plot(estimate_relation(lm1))

plot of chunk unnamed-chunk-8

Alternative plotting method:

ggscatter(penguins,
          x = "flipper_length_mm",
          y = "body_mass_g",
          add ="reg.line")

plot of chunk unnamed-chunk-9

Coefficients (parameters):

parameters(lm1)
Parameter Coefficient SE CI CI_low CI_high t df_error p
(Intercept) -5781 305.8 0.95 -6382 -5179 -19 340 0
flipper_length_mm 50 1.5 0.95 47 53 33 340 0

Plot the coefficients:

plot(parameters(lm1))

plot of chunk unnamed-chunk-11

Model fit (explained variance by model):

r2(lm1)
## # R2 for Linear Regression
##        R2: 0.759
##   adj. R2: 0.758

Predict weight of average animal:

penguins |> 
  summarise(flipper_length_mm_avg = 
              mean(flipper_length_mm, na.rm = TRUE))
flipper_length_mm_avg
201

0.3 For average flipper length, what’s the expected weight?

predict(lm1, newdata = data.frame(flipper_length_mm = 200))
##    1 
## 4156

Around 4 kgs.

0.4 Centering the data

Center the data:

penguins_c <-
  penguins |> 
  mutate(flipper_length_mm_c = center(flipper_length_mm))

Now the mean value is (nearly) zero:

mean(penguins_c$flipper_length_mm_c, na.rm = TRUE)
## [1] -1.2e-14

Run the model again:

lm2 <- lm(body_mass_g ~ flipper_length_mm_c, data = penguins_c)

parameters(lm2)
Parameter Coefficient SE CI CI_low CI_high t df_error p
(Intercept) 4202 21.3 0.95 4160 4244 197 340 0
flipper_length_mm_c 50 1.5 0.95 47 53 33 340 0

Aufgabenname: penguins-lm