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?
## Rows: 344
## Columns: 8
## $ species <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel…
## $ island <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgerse…
## $ bill_length_mm <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, …
## $ bill_depth_mm <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, …
## $ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186…
## $ body_mass_g <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, …
## $ sex <fct> male, female, female, NA, female, male, female, male…
## $ year <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007…
| Adelie (N=152) |
Chinstrap (N=68) |
Gentoo (N=124) |
|
|---|---|---|---|
| factor(sex) | |||
| female | 73 (48.0%) | 34 (50.0%) | 58 (46.8%) |
| male | 73 (48.0%) | 34 (50.0%) | 61 (49.2%) |
| Missing | 6 (3.9%) | 0 (0%) | 5 (4.0%) |
| factor(island) | |||
| Biscoe | 44 (28.9%) | 0 (0%) | 124 (100%) |
| Dream | 56 (36.8%) | 68 (100%) | 0 (0%) |
| Torgersen | 52 (34.2%) | 0 (0%) | 0 (0%) |
| body_mass_g | |||
| Mean (SD) | 3700 (459) | 3730 (384) | 5080 (504) |
| Median [Min, Max] | 3700 [2850, 4780] | 3700 [2700, 4800] | 5000 [3950, 6300] |
| Missing | 1 (0.7%) | 0 (0%) | 1 (0.8%) |
| flipper_length_mm | |||
| Mean (SD) | 190 (6.54) | 196 (7.13) | 217 (6.48) |
| Median [Min, Max] | 190 [172, 210] | 196 [178, 212] | 216 [203, 231] |
| Missing | 1 (0.7%) | 0 (0%) | 1 (0.8%) |
| bill_length_mm | |||
| Mean (SD) | 38.8 (2.66) | 48.8 (3.34) | 47.5 (3.08) |
| Median [Min, Max] | 38.8 [32.1, 46.0] | 49.6 [40.9, 58.0] | 47.3 [40.9, 59.6] |
| Missing | 1 (0.7%) | 0 (0%) | 1 (0.8%) |
| bill_depth_mm | |||
| Mean (SD) | 18.3 (1.22) | 18.4 (1.14) | 15.0 (0.981) |
| Median [Min, Max] | 18.4 [15.5, 21.5] | 18.5 [16.4, 20.8] | 15.0 [13.1, 17.3] |
| Missing | 1 (0.7%) | 0 (0%) | 1 (0.8%) |
Answer: As the table shows, the Adelie penguins are distributed across all three islands, while the Chinstrap and Gentoo penguins can only be found on Dream and Biscoe, respectively. The heaviest penguins are the Gentoo species, and they also have the longest flippers. However, the Adelie and Chinstrap penguins have a larger bill depth. While the mean is higer for the Chinstrap penguins, the deepest bill belongs to an Adelie specimen.
Answer: There seems to be a positive relationship between bill depth and penguin weight. The Adelie and chinstrap species seemingly have similar values in both, while the Gentoo have a higher weight, but flatter beaks. However the increase in weight per mm bill depth is higher in the Gentoo, leading to a steeper slope. Both the predictor and outcome variable seem normally distributed from the data summary above (mean and median are almost identical with a reasonably small)
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.
Answer: Flipper length also seems to be related to Weight positively and linearly. While you can see the species have different distributions and occur on different islands, the relationship seems to be consistent both within the Adelie apecies across islands and between the species.
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.
Answer: Why is it piiiiiiiiink?
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.
##
## 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
## # A tibble: 2 × 7
## term estimate std.error statistic p.value conf.low conf.high
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) -5781. 306. -18.9 5.59e- 55 -6382. -5179.
## 2 flipper_length_mm 49.7 1.52 32.7 4.37e-107 46.7 52.7
## # A tibble: 1 × 12
## r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.759 0.758 394. 1071. 4.37e-107 1 -2528. 5063. 5074.
## # ℹ 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>
Answer: Linear regression was used to test the association between weight (body_mass_g) and flipper length (flipper_length_mm) using data from n = 344 Penguins. 75.9% of the variation in weight was explained by the model (R2= 0.758). There was a significant positive association between weight and flipper length (B = 49.7; 95% CI = [46.7,52.7]; p < .001). On average, for every 1-mm difference in flipper length, penguins differ in mean weight by 49.7 grams.
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.
##
## Call:
## lm(formula = body_mass_g ~ bill_length_mm, data = penguins)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1762.08 -446.98 32.59 462.31 1636.86
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 362.307 283.345 1.279 0.202
## bill_length_mm 87.415 6.402 13.654 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 645.4 on 340 degrees of freedom
## (2 observations deleted due to missingness)
## Multiple R-squared: 0.3542, Adjusted R-squared: 0.3523
## F-statistic: 186.4 on 1 and 340 DF, p-value: < 2.2e-16
## # A tibble: 2 × 7
## term estimate std.error statistic p.value conf.low conf.high
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 362. 283. 1.28 2.02e- 1 -195. 920.
## 2 bill_length_mm 87.4 6.40 13.7 3.81e-34 74.8 100.
## # A tibble: 1 × 12
## r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.354 0.352 645. 186. 3.81e-34 1 -2697. 5400. 5411.
## # ℹ 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>
##
## 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
## # A tibble: 2 × 7
## term estimate std.error statistic p.value conf.low conf.high
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 7489. 335. 22.3 1.13e-68 6829. 8148.
## 2 bill_depth_mm -192. 19.4 -9.87 2.28e-20 -230. -153.
## # A tibble: 1 × 12
## r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.223 0.220 708. 97.4 2.28e-20 1 -2729. 5463. 5475.
## # ℹ 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>
##
## Call:
## lm(formula = body_mass_g ~ year, data = penguins)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1500.5 -648.4 -192.0 549.5 2140.9
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -78977.94 106777.95 -0.740 0.460
## year 41.42 53.18 0.779 0.437
##
## Residual standard error: 802.4 on 340 degrees of freedom
## (2 observations deleted due to missingness)
## Multiple R-squared: 0.001782, Adjusted R-squared: -0.001154
## F-statistic: 0.6068 on 1 and 340 DF, p-value: 0.4365
## # A tibble: 2 × 7
## term estimate std.error statistic p.value conf.low conf.high
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) -78978. 106778. -0.740 0.460 -289006. 131051.
## 2 year 41.4 53.2 0.779 0.437 -63.2 146.
## # A tibble: 1 × 12
## r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.00178 -0.00115 802. 0.607 0.437 1 -2771. 5549. 5560.
## # ℹ 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>
Answer: After exploring the other numerical variables, flipper length seems to be the best predictor of weight. The respective explained variations are lower for bill length(R2=0.352;p=), bill depth(R2=0.22;) however both of them are significantly associated (p<0.001). Interestingly, the relationships are of different valence. For every millimeter of bill length, the weight increases by 87.4 grams, while for every millimeter of depth it decreases by 192 grams.
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.
##
## 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
## # A tibble: 4 × 7
## term estimate std.error statistic p.value conf.low conf.high
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) -4031. 584. -6.90 2.55e-11 -5181. -2882.
## 2 flipper_length_mm 40.7 3.07 13.3 1.40e-32 34.7 46.7
## 3 speciesChinstrap -207. 57.7 -3.58 3.98e- 4 -320. -93.0
## 4 speciesGentoo 267. 95.3 2.80 5.39e- 3 79.4 454.
## # A tibble: 1 × 12
## r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.783 0.781 376. 406. 1.25e-111 3 -2511. 5032. 5051.
## # ℹ 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>
## # A tibble: 342 × 10
## .rownames body_mass_g flipper_length_mm species .fitted .resid .hat .sigma
## <chr> <int> <int> <fct> <dbl> <dbl> <dbl> <dbl>
## 1 1 3750 181 Adelie 3336. 414. 0.0120 375.
## 2 2 3800 186 Adelie 3540. 260. 0.00767 376.
## 3 3 3250 195 Adelie 3906. -656. 0.00833 374.
## 4 5 3450 193 Adelie 3825. -375. 0.00724 376.
## 5 6 3650 190 Adelie 3703. -52.5 0.00662 376.
## 6 7 3625 181 Adelie 3336. 289. 0.0120 376.
## 7 8 4675 195 Adelie 3906. 769. 0.00833 374.
## 8 9 3475 193 Adelie 3825. -350. 0.00724 376.
## 9 10 4250 190 Adelie 3703. 547. 0.00662 375.
## 10 11 3300 186 Adelie 3540. -240. 0.00767 376.
## # ℹ 332 more rows
## # ℹ 2 more variables: .cooksd <dbl>, .std.resid <dbl>
## [1] -1.618022e-14
## [1] 373.8795
Answer: Adding the species improved the accounted variance of the model (R2 = 0.781). The new model takes the Adelie penguins as a baseline and adds 267 grams for a Gentoo or subtracts 207 grams for a Chinstrap penguin. Then the weight increases by 40.7 grams per millimeter flipper length.
Reproduce the parallel slopes image below using the coefficients from the second model M2.
Answer:
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?
Answer: Firstly, the residuals vs fitted plot shows if there are patterns in our residuals that do not seem to fit a linear model. While there is a slight bend in our data I do not believe it to be large enough to justify a quadratic model. Secondly, there is the QQ plot, which graphically displays if the residuals are normally distributed. This seems to be the case for our model as all the dots follow the line without systematic deviations. Thirdly, we observe the scale location plot to assess homoscedasticity. In our data the residuals seem to be normally distributed along the values, though there is a hole in the center between 4000-4500 grams, likely resembling the different species. Lastly we see a plot of cooks distance, which shows influential values. The observations 40,170, and 315 stand out and we should investigate if these are measurement errors.
Run the type 3 Anova, discuss and interpret the output you see and link it to the results of the fitted model summary(M5)
##
## 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
## Df Sum Sq Mean Sq F value Pr(>F)
## flipper_length_mm 1 166452902 166452902 1180.29 < 2e-16 ***
## species 2 5187807 2593904 18.39 2.62e-08 ***
## Residuals 338 47666988 141027
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 2 observations deleted due to missingness
Answer: The Anova indicates that for both flipper length and species the difference between the groups is significantly larger than the variation within groups. This supports our previous results from the linear regression.
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
##
## 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
## # A tibble: 4 × 7
## term estimate std.error statistic p.value conf.low conf.high
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) -1007. 324. -3.11 2.01e- 3 -1644. -371.
## 2 bill_depth_mm 257. 17.6 14.6 8.10e-38 222. 291.
## 3 speciesChinstrap 13.4 52.9 0.253 8.01e- 1 -90.8 118.
## 4 speciesGentoo 2239. 73.7 30.4 1.16e-98 2094. 2384.
## # A tibble: 1 × 12
## r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.798 0.796 362. 444. 7.72e-117 3 -2499. 5007. 5026.
## # ℹ 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>
## # A tibble: 342 × 10
## .rownames body_mass_g bill_depth_mm species .fitted .resid .hat .sigma
## <chr> <int> <dbl> <fct> <dbl> <dbl> <dbl> <dbl>
## 1 1 3750 18.7 Adelie 3791. -41.4 0.00692 363.
## 2 2 3800 17.4 Adelie 3458. 342. 0.00873 362.
## 3 3 3250 18 Adelie 3612. -362. 0.00690 362.
## 4 5 3450 19.3 Adelie 3945. -495. 0.00876 362.
## 5 6 3650 20.6 Adelie 4279. -629. 0.0185 361.
## 6 7 3625 17.8 Adelie 3560. 64.5 0.00732 363.
## 7 8 4675 19.6 Adelie 4022. 653. 0.0103 361.
## 8 9 3475 18.1 Adelie 3637. -162. 0.00677 363.
## 9 10 4250 20.2 Adelie 4176. 73.7 0.0147 363.
## 10 11 3300 17.1 Adelie 3381. -80.8 0.0103 363.
## # ℹ 332 more rows
## # ℹ 2 more variables: .cooksd <dbl>, .std.resid <dbl>
## [1] -1.03489e-14
## [1] 360.8383
##
## 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
## Df Sum Sq Mean Sq F value Pr(>F)
## bill_depth_mm 1 48840779 48840779 371.8 <2e-16 ***
## species 2 126067249 63033624 479.9 <2e-16 ***
## Residuals 338 44399670 131360
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 2 observations deleted due to missingness
Answer: Here there seems to be a much bigger difference between the species. The Gentoo species runs entirely seperately from the other two. Thus it might be a better model to combine Adelie and Chinstrap penguins while excluding Gentoo. The 4 assessment plots also indicate this difference showing even clearer bimodal distributions of the residuals, likely violating homoscedasticity. Lastly, observation 170 is an outlier in both models and likely represents a measurement error.