1 Cuckoo dataset

The common cuckoo does not build its own nest: it prefers to lay its eggs in another birds’ nest. It is known, since 1892, that the type of cuckoo bird eggs are different between different locations. In a study from 1940, it was shown that cuckoos return to the same nesting area each year, and that they always pick the same bird species to be a “foster parent” for their eggs.

Over the years, this has lead to the development of geographically determined subspecies of cuckoos. These subspecies have evolved in such a way that their eggs look as similar as possible as those of their foster parents.

The cuckoo dataset contains information on 120 Cuckoo eggs, obtained from randomly selected “foster” nests. For these eggs, researchers have measured the length (in mm) and established the type (species) of foster parent. The type column is coded as follows:

  • type=1: Meadow pipit
  • type=2: Tree pipit
  • type=3: Dunnock
  • type=4: European robin
  • type=5: White wagtail
  • type=6: Eurasian wren

2 Goal

The researchers want totest if the type of foster parent has an effect on the average length of the cuckoo eggs.

In theory, they want to study this for all six species. However, a t-test can only be used to study mean differences between two groups. If we want to analyze multiple groups, there are two options.

  1. We perform t-tests on all pairwise combinations of types. This mean we need to perform n*(n-1)/2 = 15 t-tests.

  2. We perform an ANOVA analysis.

The second strategy is much more efficient and has a higher statistical power. We will learn all about ANOVA in a later stage of this course week.

In this tutorial, we will assess a single pairwise comparison, between the European robin and the European wren. In a following tutorial, we will come back to this dataset and make a full analysis with ANOVA.

Load the required libraries

library(tidyverse)

3 Import data

Cuckoo <- read_tsv("https://raw.githubusercontent.com/statOmics/PSLS21/data/Cuckoo.txt")
head(Cuckoo)

4 Tidy data

For this exercise, we only care about the European robin and the Eurasian wren. Therefore, we can remove the observations of the other types. In addition, it seems that the type column rather than a factor. Let’s fix this:

Cuckoo <- Cuckoo %>%
  filter(type %in% c("4","6")) %>%
  mutate(type = as.factor(type))

5 Data exploration

How many birds do we have for each type?

Cuckoo %>%
  count(type)

Visualize the data using a suitable strategy

Cuckoo %>%
  ggplot(aes(x=type,y=length,fill=type)) +
    geom_boxplot() +
    theme_bw() +
    geom_boxplot(outlier.shape=NA) + 
    geom_jitter(width = 0.2) +
    scale_fill_manual(values=c("dimgrey","firebrick")) +
    ggtitle("Boxplot of the length of eggs per type") +
    ylab("length (mm)") + 
    stat_summary(fun.y=mean, geom="point", shape=5, size=3, color="black", fill="black")
## Warning: `fun.y` is deprecated. Use `fun` instead.

We clearly see that, on average, the eggs laid in the nest of the European robin (type=4) are larger than those laid in the nest of the Eurasian wren. But is this difference significant?

6 Analysis

We can test this with an unpaired, two-sample t-test. But before we can start the analysis, we must check if all assumptions to perform a t-test are met.

6.1 Check the assumptions

  1. The observations are independent of each other (in both groups)

  2. The data (length) must be normally distributed (in both groups)

Additionally, we should check if the variability within both groups is similar or not (in the lattter case we should use a Welch t-test).

  1. The variability within both groups is similar

The first assumption is met, as we may assume that there are no specific patterns of correlation randomly selected nests.

To check the normality assumption, we will use QQ plots.

Cuckoo %>% 
  ggplot(aes(sample=length)) +
  geom_qq() +
  geom_qq_line() + 
  facet_grid(~type)

There seems to be no clear deviation from normality.

The third assumption seems to be met based on our visualization with boxplots. Indeed the interquartile range of the boxes are very comparable. As all assumptions are met, we may proceed with the analysis.

6.2 Hypothesis testing

output <- t.test(length ~ type, data = Cuckoo, var.equal = TRUE)
output
## 
##  Two Sample t-test
## 
## data:  length by type
## t = 5.633, df = 29, p-value = 4.378e-06
## alternative hypothesis: true difference in means between group 4 and group 6 is not equal to 0
## 95 percent confidence interval:
##  0.9203528 1.9696472
## sample estimates:
## mean in group 4 mean in group 6 
##          22.575          21.130

6.3 Conclusion

There is an extremely significant difference in mean length of Cuckoo eggs fostered by the European robin and those fostered by the Eurasian wren (p << 0.001). The eggs fostered by the European wren are on average 1.45 mm longer (95% CI [0.92, 1.97]).

LS0tCnRpdGxlOiAiRXhlcmNpc2UgNS4zOiBIeXBvdGhlc2lzIHRlc3Rpbmcgb24gdGhlIGN1Y2tvbyBkYXRhc2V0IC0gc29sdXRpb24iICAgCmF1dGhvcjogIkxpZXZlbiBDbGVtZW50IGFuZCBKZXJvZW4gR2lsaXMiCmRhdGU6ICJzdGF0T21pY3MsIEdoZW50IFVuaXZlcnNpdHkgKGh0dHBzOi8v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