Linear Regression

student

2026-10-19

Simple Linear Regression Equation

Simple linear regression shows the relationship between two variables

The equation is as shown: ŷ = b₀ + b₁x

Penguin Regression Line

data("penguins")
filtered = filter(penguins, !is.na(body_mass))
mod <- lm(filtered$body_mass~filtered$flipper_len)
mod
## 
## Call:
## lm(formula = filtered$body_mass ~ filtered$flipper_len)
## 
## Coefficients:
##          (Intercept)  filtered$flipper_len  
##             -5780.83                 49.69

we can see that y = -5780.83 + 49.69x

so for every length in a penguin’s flipper it’s body mass increases by 49.69

Penguin Body Mass vs. Flipper Length

We can see there seems to be a positive relationship with penguin flippers and their body mass

Predictions and Residual

The Residual formula is e = y - ŷ, where e is the residual, y is the actual value, and ŷ is the predicted value

We can make a prediction of what a penguins body mass will be with the linear regression equation, if we see a penguin with a 207mm flipper length

y = -5780.83 + 49.69x

y = 4504.081

the recorded mass of a penguin with a 207mm flipper was actually 4000

so the residual is e = 4000 - 4925.384

e = -925.384

while there is a high residual error it could be because body mass is based on other factors…

Body Mass vs Flipper Length based on Sex

## `geom_smooth()` using formula = 'y ~ x'

We can see for here males tend to have a higher body mass than female penguins

Body Mass vs Flipper Length based on Species

## `geom_smooth()` using formula = 'y ~ x'

We can see that the Gentoo penguins have a longer flipper length and a higher body mass than the Adelies and the Chinstraps. Adelies tend to have a higher body mass than Chinstrips too while they maintain similar flipper lengths

ggplot of Body mass and Flipper Length

ggplot(penguins, aes(x = flipper_len, y = body_mass, colour = sex)) +
  geom_point(aes(shape = species)) + geom_smooth(method = "lm", se = F) + 
  labs(
    x = "Flipper Length",
    y = "Body Mass",
    title = "Body Mass vs Flipper Length"
  )
## `geom_smooth()` using formula = 'y ~ x'

Based on island and species

## `geom_smooth()` using formula = 'y ~ x'

We can see that with island, the Biscoe have a larger body mass and are mainly gentoo, with penguins from Dream and Torgersen their body mass is similar with Dream being a little lower due to penguins of the Chinstraps species being located there.