# install.packages("ggsignif") # only once
# install.packages("ggpubr") # only once
library(tidyverse)
library(ggsignif)
library(ggpubr)Task: Statistical tests
Get started
We will again use the penguins data included in R.
As always, before you get started with the exercise, make sure that you load the tidyverse. You also need the packages ggsignif and ggpubr. Install them if you haven’t done so yet:
Part 1: Compare two species
Question: Does the flipper length differ between Adelie and Chinstrap penguins?
1. Create a smaller version of the data
Create a table penguins_small that only contains Adelie and Chinstrap penguins.
penguins_small (unfold to see)
penguins_small <- penguins |>
filter(species %in% c("Adelie", "Chinstrap"))2. Plot the two species
Make a plot that compares the flipper length of the two species, e.g. a boxplot.
3. Check for normality
Create a vector with the flipper lengths of each species.
adelie <- filter(penguins_small, species == "Adelie")$flipper_len
chinstrap <- filter(penguins_small, species == "Chinstrap")$flipper_lenUse the Shapiro-Wilk test and QQ-plots to check if the flipper lengths of both species are roughly normally distributed.
4. Compare the means
Use Welch’s t-test to compare the mean flipper length of the two species. How would you report the result?
5. Add the test result to the plot
Add a geom_signif() layer to your plot from step 2 to show the result of the t-test.
Part 2: Compare all three species
Question: Does the flipper length differ between Adelie, Chinstrap and Gentoo penguins?
1. Pairwise comparisons
Use the full penguins table and compare all three species with pairwise.t.test(). Which species differ?
2. Plot with significance levels
Make a plot of the flipper length of all three species and add the adjusted p-values with geom_signif(). You can type the p-values by hand or extract them automatically (see slides).
For the fast ones
You can do these in any order, or skip them and just take a break.
- Use a different plot type to show the differences (e.g. mean ± standard error, barplot)
- If you typed the p-values by hand: extract them automatically from the result of
pairwise.t.test()as you saw on the slides - Make your plot look nice (colors, theme, axis labels, …)
- Compare Adelie and Chinstrap with a Wilcoxon rank-sum test. Do you come to the same conclusion as with the t-test?
- Check the flipper length of Gentoo penguins for normality with the Shapiro-Wilk test and a QQ-plot. What do you conclude?