# 1. load the appropriate libraries
library(tidyverse)
library(broom)
library(table1)
library(car)

# 2. use the read_rds file to read the dataset
penguins <- read_rds("data/penguins.rds")

Exercise 1

Use the table1 package to replicate the table below. Use the examples provided in the following vignette. What do you notice from the table? Which species of penguin is the heaviest? Which one has the largest flippers? Which have the largest bill depth? How are the different species distributed across the three islands?

# Create table 1
table1(~ factor(sex) + factor(island) + flipper_length_mm + bill_depth_mm + bill_length_mm + body_mass_g | species, data = penguins)
Adelie
(N=152)
Chinstrap
(N=68)
Gentoo
(N=124)
Overall
(N=344)
factor(sex)
female 73 (48.0%) 34 (50.0%) 58 (46.8%) 165 (48.0%)
male 73 (48.0%) 34 (50.0%) 61 (49.2%) 168 (48.8%)
Missing 6 (3.9%) 0 (0%) 5 (4.0%) 11 (3.2%)
factor(island)
Biscoe 44 (28.9%) 0 (0%) 124 (100%) 168 (48.8%)
Dream 56 (36.8%) 68 (100%) 0 (0%) 124 (36.0%)
Torgersen 52 (34.2%) 0 (0%) 0 (0%) 52 (15.1%)
flipper_length_mm
Mean (SD) 190 (6.54) 196 (7.13) 217 (6.48) 201 (14.1)
Median [Min, Max] 190 [172, 210] 196 [178, 212] 216 [203, 231] 197 [172, 231]
Missing 1 (0.7%) 0 (0%) 1 (0.8%) 2 (0.6%)
bill_depth_mm
Mean (SD) 18.3 (1.22) 18.4 (1.14) 15.0 (0.981) 17.2 (1.97)
Median [Min, Max] 18.4 [15.5, 21.5] 18.5 [16.4, 20.8] 15.0 [13.1, 17.3] 17.3 [13.1, 21.5]
Missing 1 (0.7%) 0 (0%) 1 (0.8%) 2 (0.6%)
bill_length_mm
Mean (SD) 38.8 (2.66) 48.8 (3.34) 47.5 (3.08) 43.9 (5.46)
Median [Min, Max] 38.8 [32.1, 46.0] 49.6 [40.9, 58.0] 47.3 [40.9, 59.6] 44.5 [32.1, 59.6]
Missing 1 (0.7%) 0 (0%) 1 (0.8%) 2 (0.6%)
body_mass_g
Mean (SD) 3700 (459) 3730 (384) 5080 (504) 4200 (802)
Median [Min, Max] 3700 [2850, 4780] 3700 [2700, 4800] 5000 [3950, 6300] 4050 [2700, 6300]
Missing 1 (0.7%) 0 (0%) 1 (0.8%) 2 (0.6%)

Answer:

There are three species of penguin in the study. They live on three islands. Biscoe island has Adelie and Gentoo penguins, Dream island has Adelie and Chinstrap penguins, and Torgersen island has only Adelie penguins. The biggest penguins are the Gentoo penguins. They have the longest flippers and greatest body mass of all, followed by Chinstrap and finally Adelie penguins. The set has data about 344 penguins.

Exercise 2

Replicate the scatter plot below, coloring the points according to the species. What can you tell about the relationship between bill depth and penguin weight? Comment on the intercepts and the slopes. Do different species have similar intercepts? Slopes? Does the outcome variable appear normally distributed? Why does this matter?

species_colors <- c("Adelie" = "#fa3282", "Chinstrap" = "sky blue", "Gentoo" = "blue")

ggplot(data = penguins, aes(x = bill_depth_mm, y = body_mass_g, color = species)) +
  geom_point() +
  geom_smooth(method = lm, se = FALSE)+
  labs(y = "Body Mass (g)", x = "Bill Depth (mm)", color = "Species") +
  scale_color_manual(values = species_colors)

model <- lm(body_mass_g ~ bill_depth_mm + species, data = penguins)
residuals <- residuals(model)
hist(residuals, col = "skyblue", border = "black", main = "Histogram of Residuals", xlab = "Residuals")

Answer:

For all species, there is a linear relationship between bill depth and body mass. As the bill depth increases, the body mass also increases. The slope for Gentoo species is the steepest indicating a stronger and more direct relationship between the variables. For the Chinstrap and Adelie species, the slope and intersection is almost the same, indicating that the overall pattern or trend relating body mass to bill depth is consistent across the two species. There is a consistent relationship. The residuals are normally distributed. Hence the assumption that the data points can be normally distributed along the regression line is correct, making linear regression a good model.

Exercise 3

Now replicate the plot below, describing the relationship between flipper length and body mass, and coloring the data according to the associated species. Facet the plot by island (search online if necessary). Finally, tell a story about the relationship between flipper length and weight in these three penguin species, and the distribution of penguins across the three islands.

custom_colors <- c("#fa3282", "lightblue", "blue")

ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g, color = species, alpha = 0.5)) +
  geom_point() +
  facet_wrap(~ island) +
  labs(x = "Flipper Length (mm)", y = "Body Mass (g)", color = "Species") +
  theme_classic()+
  theme(aspect.ratio = 1) +
  coord_cartesian(clip = "off", xlim = c(150, 250))+
  scale_color_manual(values = custom_colors)

Answer:

Based on these faceted scatter plots, we can see that among all species and on all islands, there is a positive correlation between flipper length and body mass. Furthermore, it gives is insights into the populations of the three islands; Biscoe is home to the Gentoo and Adelie species, of which the Gentoo is clearly the bigger species, on the island ‘Dream’ there are Adelie and Chinstrap penguins, which are roughly the same size, and Torgersen belongs entirely to the Adelie penguins.

Exercise 4

Replicate the scatter plot below, adding vertical and horizontal lines to indicate the mean of the predictor and of the outcome variable. Show that the linear regression line passes at the intersection of the two means.

mean_flipperlength <- mean(penguins$flipper_length_mm, na.rm = TRUE)
mean_bodymass <- mean(penguins$body_mass_g, na.rm = TRUE)


ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
  geom_point(aes(color = species)) +
  labs(x = "Flipper Length (mm)", y = "Body Mass (g)", color = "Species") +
  scale_color_manual(values = custom_colors) +
  geom_vline(xintercept = mean_flipperlength, linetype = "dashed") +
  geom_hline(yintercept = mean_bodymass, linetype = "dashed") +
  geom_smooth(method = "lm", se = FALSE, color = "maroon")

Answer:

As can be seen from the plot, the regression line crosses the intercept between the mean body mass and the mean flipper length. This is an assumption of the regression line as it will minimize the sum of squares.

Exercise 5

Use the lm function in R to fit a simple linear regression with one predictor. Write up your findings using the template below. Fill in the gaps in the template with the result of your model.

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

summary(M1)
## 
## Call:
## lm(formula = body_mass_g ~ flipper_length_mm, data = penguins)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1058.80  -259.27   -26.88   247.33  1288.69 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -5780.831    305.815  -18.90   <2e-16 ***
## flipper_length_mm    49.686      1.518   32.72   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 394.3 on 340 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.759,  Adjusted R-squared:  0.7583 
## F-statistic:  1071 on 1 and 340 DF,  p-value: < 2.2e-16
tidy(M1, conf.int = TRUE)
glance(M1)

Answer:

Linear regression was used to test the association between body_mass_g (The weight of the penguins) and flipper_length_mm (Flipper Length) using data from n = 342 Penguins. 75.9% of the variation in body mass was explained by flipper length (R2= 0.759). There was a significant positive association between body mass and flipper length (B = 49.686; 95% CI = 46.7, 52.7; p < .001). On average, for every 1-mm increase in flipper length, there is an expected increase in mean body mass by 49.7 grams.

Exercise 6

Use the lm function in R to fit predict the body mass using a different explanatory variable. Write up your findings using a similar template like the one used above. Which model is better, in your view? This one or the model in the previous question? Justify your answer.

M1 <- lm(body_mass_g ~ bill_depth_mm,
                         data = penguins)

summary(M1)
## 
## Call:
## lm(formula = body_mass_g ~ bill_depth_mm, data = penguins)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1607.38  -510.10   -66.96   462.43  1819.28 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    7488.65     335.22   22.34   <2e-16 ***
## bill_depth_mm  -191.64      19.42   -9.87   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 708.1 on 340 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.2227, Adjusted R-squared:  0.2204 
## F-statistic: 97.41 on 1 and 340 DF,  p-value: < 2.2e-16
tidy(M1, conf.int = TRUE)
glance(M1)

Answer:

Linear regression was used to test the association between body_mass_g (response variable) and bill_depth_mm (explanatory variable) using data from n = 342 Penguins. 22.2% of the variation in body mass was explained by bill depth(R2= 0.222). There was a significant negative association between bill depth and body mass (B =-192 ; 95% CI = -230, -153; p < .001). On average, for every 1-mm increase in bill depth, penguins show a decrease in mean body mass by 192 grams.

The earlier model is more favorable as it exhibits a higher R^2 suggesting a superior representation of the linear relationship between body mass and flipper length. A larger R^2 ndicates that a greater proportion of the variation in body mass is explained by flipper length. Additionally, the correlation coefficient between body mass and flipper length is closer to 1, signifying a stronger and more positive correlation.

Exercise 7

We now want to fit a model with parallel slopes, one slope for each of the three species of penguins. Run the regression as you have done above, but this time, add the species variable as a predictor in your model. (1) Write the equation for the linear model (you will need to replace the coefficients in the formula b0,b1,b2 with their estimates). (2) Write up your findings in a similar manner to the template shown in the previous question. (3) Write a separate model for each of the three species. (4) Use the augment(M2) to see the list of residuals and show that the mean of the residuals is almost zero and its standard deviation is the residual standard error.

M2 <- lm(body_mass_g ~ flipper_length_mm + species, data = penguins)

summary(M2)
## 
## Call:
## lm(formula = body_mass_g ~ flipper_length_mm + species, data = penguins)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -927.70 -254.82  -23.92  241.16 1191.68 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -4031.477    584.151  -6.901 2.55e-11 ***
## flipper_length_mm    40.705      3.071  13.255  < 2e-16 ***
## speciesChinstrap   -206.510     57.731  -3.577 0.000398 ***
## speciesGentoo       266.810     95.264   2.801 0.005392 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 375.5 on 338 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.7826, Adjusted R-squared:  0.7807 
## F-statistic: 405.7 on 3 and 338 DF,  p-value: < 2.2e-16
model_Gentoo <- lm(body_mass_g ~ flipper_length_mm, data = subset(penguins, species == "Gentoo"))

model_Chinstrap <- lm(body_mass_g ~ flipper_length_mm, data = subset(penguins, species == "Chinstrap"))

model_Adelie <- lm(body_mass_g ~ flipper_length_mm, data = subset(penguins, species == "Adelie"))

summary(model_Gentoo)
## 
## Call:
## lm(formula = body_mass_g ~ flipper_length_mm, data = subset(penguins, 
##     species == "Gentoo"))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -911.18 -235.76  -51.93  170.75 1015.71 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -6787.281   1092.552  -6.212 7.65e-09 ***
## flipper_length_mm    54.623      5.028  10.863  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 360.2 on 121 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.4937, Adjusted R-squared:  0.4896 
## F-statistic:   118 on 1 and 121 DF,  p-value: < 2.2e-16
summary(model_Chinstrap)
## 
## Call:
## lm(formula = body_mass_g ~ flipper_length_mm, data = subset(penguins, 
##     species == "Chinstrap"))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -900.90 -137.45  -28.55  142.59  695.38 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -3037.196    997.054  -3.046  0.00333 ** 
## flipper_length_mm    34.573      5.088   6.795 3.75e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 297 on 66 degrees of freedom
## Multiple R-squared:  0.4116, Adjusted R-squared:  0.4027 
## F-statistic: 46.17 on 1 and 66 DF,  p-value: 3.748e-09
summary(model_Adelie)
## 
## Call:
## lm(formula = body_mass_g ~ flipper_length_mm, data = subset(penguins, 
##     species == "Adelie"))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -875.68 -331.10  -14.53  265.74 1144.81 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -2535.837    964.798  -2.628  0.00948 ** 
## flipper_length_mm    32.832      5.076   6.468 1.34e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 406.6 on 149 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.2192, Adjusted R-squared:  0.214 
## F-statistic: 41.83 on 1 and 149 DF,  p-value: 1.343e-09
library(broom)

augmented_Gentoo <- augment(model_Gentoo)
augmented_Adelie <- augment(model_Adelie)
augmented_Chinstrap <- augment(model_Chinstrap)

residuals_mean_Gentoo <- mean(augmented_Gentoo$.resid)
residuals_sd_Gentoo <- sd(augmented_Gentoo$.resid)

residuals_mean_Adelie <- mean(augmented_Adelie$.resid)
residuals_sd_Adelie <- sd(augmented_Adelie$.resid)

residuals_mean_Chinstrap <- mean(augmented_Chinstrap$.resid)
residuals_sd_Chinstrap <- sd(augmented_Chinstrap$.resid)

cat("Model Gentoo - Mean of the residuals:", residuals_mean_Gentoo, "\n")
## Model Gentoo - Mean of the residuals: -1.35167e-11
cat("Model Gentoo - Standard deviation of the residuals:", residuals_sd_Gentoo, "\n")
## Model Gentoo - Standard deviation of the residuals: 358.6884
cat("\nModel Adelie - Mean of the residuals:", residuals_mean_Adelie, "\n")
## 
## Model Adelie - Mean of the residuals: 2.514639e-12
cat("Model Adelie - Standard deviation of the residuals:", residuals_sd_Adelie, "\n")
## Model Adelie - Standard deviation of the residuals: 405.1989
cat("\nModel Chinstrap - Mean of the residuals:", residuals_mean_Chinstrap, "\n")
## 
## Model Chinstrap - Mean of the residuals: 7.958079e-13
cat("Model Chinstrap - Standard deviation of the residuals:", residuals_sd_Chinstrap, "\n")
## Model Chinstrap - Standard deviation of the residuals: 294.8132

Answer:

The formula for the regression model with body mass as the response and flipper length and species as explanatory variables is Y = -4031.477 + 40.70 * (flipper length) - 206.51 * (Chinstrap) + 266.81 * (Gentoo). Based on this, we can see that there is an overall positive slope between the flipper length and the body mass. Moreover, we can see that Gentoo penguins are, on average, 266 grams heavier than the Arie penguins (with an equal flipper length), and that the Chinstrap penguins are around 206 grams lighter on average (At equal flipper length).

Linear regression was used to test the association between flipper length and body mass using data from n = 122 Penguins. 49.37% of the variation in body mass was explained by flipper length (R² = 0.4937). There was a significant positive association between flipper length and body mass (B = 54.623; 95% CI = [44.701, 64.546]; p < .001). On average, for every 1-mm difference in flipper length, penguins differ in mean body mass by 54.623 grams.

Exercise 8

Reproduce the parallel slopes image below using the coefficients from the second model M2.

coefs <- coef(M2)

penguins |> 
  ggplot(
    aes(x = flipper_length_mm, y = body_mass_g, color = species)
    ) + 
  geom_point() + 
  # Use the intercept for the reference category (Adelie)
  geom_abline(
    intercept = coefs[1], slope = coefs[2], color = "#fa3282"
    ) +
  # Use the appropriate intercept for Chinstrap
  geom_abline(
    intercept = coefs[1] + coefs[3], slope = coefs[2], color = "sky blue"
    ) +
  # Use the appropriate intercept for Gentoo
  geom_abline(
    intercept = coefs[1] + coefs[4], slope = coefs[2], color = "blue"
    ) + 
  scale_color_manual(
    values = c("#fa3282", "sky blue", "blue")
    ) 

Answer:

This graph demonstrates the relationship between flipper length and body mass of the penguins. It shows that for every flipper length, the chinstrap penguins are the smallest, the arie penguins are in the middle, and the gentoo penguins are the biggest. The slope represents the weight increase for every additional millimeter of flipper length.

Exercise 9

Search online for an explanation of the various plots (for example, this is a good resource, but you may find others). Create a plot for each of the four diagnostic tests, and describe in words what is the purpose of each diagnostic plot. Then interpret the plot: what can we learn from it?

plot(M2, which = 1)

plot(M2, which = 2)

plot(M2, which = 3)

plot(M2, which = 4)

Answer:

Answer: Residual vs fitted plot: The purpose of this plot is to test for any systematic errors in the data. This helps us detect patterns or issues in the data. It also lets us assess the assumption of independence of errors. A good fitted-residuals plot that meets the assumptions will have residuals scattered around 0 with a constant variance.

Q-Q Plot: Shows the distribution of the residuals. They should be normally distributed to meet the assumptions of the regression model. The residuals are normally distributed if they follow the diagonal line.

Scale-Location: This plot allows us to assess homoscedasticity similarly to the residuals vs fitted plot. Ideally, the points on this plot should also be equally distributed and have no inherent patterns.

Cook’s Distance: This plot allows us to see if there are values in the model that are disproportionately affecting the model. These would be values on the x axis that are far away from the others or values with a very high cook’s distance.

Exercise 10

Run the type 3 Anova, discuss and interpret the output you see and link it to the results of the fitted model summary(M2).

# Perform Type III ANOVA
anova_result <- Anova(M2, type="III")

# Print the ANOVA table
print(anova_result)
## Anova Table (Type III tests)
## 
## Response: body_mass_g
##                     Sum Sq  Df F value    Pr(>F)    
## (Intercept)        6717050   1  47.630 2.551e-11 ***
## flipper_length_mm 24776495   1 175.687 < 2.2e-16 ***
## species            5187807   2  18.393 2.615e-08 ***
## Residuals         47666988 338                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(M2)
## 
## Call:
## lm(formula = body_mass_g ~ flipper_length_mm + species, data = penguins)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -927.70 -254.82  -23.92  241.16 1191.68 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -4031.477    584.151  -6.901 2.55e-11 ***
## flipper_length_mm    40.705      3.071  13.255  < 2e-16 ***
## speciesChinstrap   -206.510     57.731  -3.577 0.000398 ***
## speciesGentoo       266.810     95.264   2.801 0.005392 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 375.5 on 338 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.7826, Adjusted R-squared:  0.7807 
## F-statistic: 405.7 on 3 and 338 DF,  p-value: < 2.2e-16

Answer:

Main Conclusions:

Overall Model:

The overall model is highly significant (F=405.7, p<0.001),indicating that the combination of flipper length and species of the penguin explains a significant amount of variability in body mass.

Flipper Length:

The coefficient for flipper_length_mm is 40.705, and it is highly significant (p<0.001). This means that, on average, for one additional millimeter of flipper length, the body mass of the penguin increases by 40.705 grams.

Species:

The Type III ANOVA indicated the overall high significance of the species variable, and the individual coefficients confirming the specific impact of each one of the species on body mass.

Adjusted R-squared:

The adjusted R-squared is 0.7807, indicating that approximately 78.07% of the variability in body mass is explained by the combination of flipper length and species in the model.

Exercise 11

Now repeat exercises 6 - 10 but this time analyze the association between the bill depth and the body mass: body_mass_g ~ bill_depth_mm + species.

Exercise 6:

M3 <- lm(body_mass_g ~ bill_depth_mm + species,
                         data = penguins)

summary(M3)
## 
## Call:
## lm(formula = body_mass_g ~ bill_depth_mm + species, data = penguins)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -867.73 -255.61  -27.41  242.41 1190.86 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      -1007.28     323.56  -3.113  0.00201 ** 
## bill_depth_mm      256.61      17.56  14.611  < 2e-16 ***
## speciesChinstrap    13.38      52.95   0.253  0.80069    
## speciesGentoo     2238.67      73.68  30.383  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 362.4 on 338 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.7975, Adjusted R-squared:  0.7957 
## F-statistic: 443.8 on 3 and 338 DF,  p-value: < 2.2e-16
tidy(M3, conf.int = TRUE)
glance(M3)

Answer Y = -1007.28+ 256.61x1+13.38x2+ 2238.67x3+ e x1= bill depth x2= chinstrap x3= gentoo

Exercise 7:

M3 <- lm(body_mass_g ~ bill_depth_mm + species, data = penguins)

summary(M3)
## 
## Call:
## lm(formula = body_mass_g ~ bill_depth_mm + species, data = penguins)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -867.73 -255.61  -27.41  242.41 1190.86 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      -1007.28     323.56  -3.113  0.00201 ** 
## bill_depth_mm      256.61      17.56  14.611  < 2e-16 ***
## speciesChinstrap    13.38      52.95   0.253  0.80069    
## speciesGentoo     2238.67      73.68  30.383  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 362.4 on 338 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.7975, Adjusted R-squared:  0.7957 
## F-statistic: 443.8 on 3 and 338 DF,  p-value: < 2.2e-16
model_Gentoo <- lm(body_mass_g ~ bill_depth_mm, data = subset(penguins, species == "Gentoo"))

model_Chinstrap <- lm(body_mass_g ~ bill_depth_mm, data = subset(penguins, species == "Chinstrap"))

model_Adelie <- lm(body_mass_g ~ bill_depth_mm, data = subset(penguins, species == "Adelie"))

summary(model_Gentoo)
## 
## Call:
## lm(formula = body_mass_g ~ bill_depth_mm, data = subset(penguins, 
##     species == "Gentoo"))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -734.85 -248.46  -59.85  226.13 1143.49 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    -458.99     487.31  -0.942    0.348    
## bill_depth_mm   369.44      32.46  11.382   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 351.8 on 121 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.5171, Adjusted R-squared:  0.5131 
## F-statistic: 129.6 on 1 and 121 DF,  p-value: < 2.2e-16
summary(model_Chinstrap)
## 
## Call:
## lm(formula = body_mass_g ~ bill_depth_mm, data = subset(penguins, 
##     species == "Chinstrap"))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -756.27 -230.57    9.78  176.88  712.05 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     -36.22     612.56  -0.059    0.953    
## bill_depth_mm   204.62      33.19   6.165 4.79e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 308.5 on 66 degrees of freedom
## Multiple R-squared:  0.3654, Adjusted R-squared:  0.3558 
## F-statistic: 38.01 on 1 and 66 DF,  p-value: 4.795e-08
summary(model_Adelie)
## 
## Call:
## lm(formula = body_mass_g ~ bill_depth_mm, data = subset(penguins, 
##     species == "Adelie"))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -845.89 -259.67  -55.74  267.98 1161.41 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    -283.28     464.03  -0.610    0.542    
## bill_depth_mm   217.15      25.24   8.604 9.94e-15 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 376.1 on 149 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.3319, Adjusted R-squared:  0.3275 
## F-statistic: 74.03 on 1 and 149 DF,  p-value: 9.942e-15

Exercise 8:

coefs3 <- coef(M3)

penguins |> 
  ggplot(
    aes(x = bill_depth_mm, y = body_mass_g, color = species)
    ) + 
  geom_point() + 
  # Use the intercept for the reference category (Adelie)
  geom_abline(
    intercept = coefs3[1], slope = coefs3[2], color = "#fa3282"
    ) +
  # Use the appropriate intercept for Chinstrap
  geom_abline(
    intercept = coefs3[1] + coefs3[3], slope = coefs3[2], color = "sky blue"
    ) +
  # Use the appropriate intercept for Gentoo
  geom_abline(
    intercept = coefs3[1] + coefs3[4], slope = coefs3[2], color = "blue"
    ) + 
  scale_color_manual(
    values = c("#fa3282", "sky blue", "blue")
    ) 

Exercise 9:

plot(M3, which = 1)

plot(M3, which = 2)

plot(M3, which = 3)

plot(M3, which = 4)

Exercise 10:

# Perform Type III ANOVA
anova_result <- Anova(M3, type="III")

# Print the ANOVA table
print(anova_result)
## Anova Table (Type III tests)
## 
## Response: body_mass_g
##                  Sum Sq  Df  F value    Pr(>F)    
## (Intercept)     1273070   1   9.6915  0.002009 ** 
## bill_depth_mm  28043814   1 213.4883 < 2.2e-16 ***
## species       126067249   2 479.8541 < 2.2e-16 ***
## Residuals      44399670 338                       
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(M3)
## 
## Call:
## lm(formula = body_mass_g ~ bill_depth_mm + species, data = penguins)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -867.73 -255.61  -27.41  242.41 1190.86 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      -1007.28     323.56  -3.113  0.00201 ** 
## bill_depth_mm      256.61      17.56  14.611  < 2e-16 ***
## speciesChinstrap    13.38      52.95   0.253  0.80069    
## speciesGentoo     2238.67      73.68  30.383  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 362.4 on 338 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.7975, Adjusted R-squared:  0.7957 
## F-statistic: 443.8 on 3 and 338 DF,  p-value: < 2.2e-16

Answer:

Main Conclusions:

The overall model is highly significant, as indicated by the Type III ANOVA (F=443.8, p<0.001). This suggests that the combination of bill depth and species provides a significant explanation for the variability in body mass.

Both bill depth and species are significant predictors of body mass, as confirmed by both the Type III ANOVA and the coefficients in the model summary.

An increase of one millimeter in bill depth is associated with an average increase of 256.61 grams in body mass.The species variable significantly contributes to explaining the variation in body mass beyond the effect of bill depth and as a result the model provides a good fit to the data, explaining approximately 79.75% of the variability in body mass.