5  Statistical Inference

Week 5: Comparing groups with t-tests, one-way ANOVA and two-way ANOVA

In this workshop you will learn the three most common tests for comparing groups: the two-sample t-test, the one-way ANOVA and the two-way ANOVA. They might look like three different tests, but they are all the same idea, a linear model with categorical predictors, so learning one makes the next one easy. Workshop materials are available in the GitHub repository ECS200.

TipLearning outcomes

By the end of this workshop you should be able to:

  1. Choose between a t-test, one-way ANOVA and two-way ANOVA for a research question.
  2. Visualise group data and check the assumptions of these tests.
  3. Run each test in R and find the important numbers in the output.
  4. Write a results sentence that reports the direction, size and statistical support of an effect.

Before we start

Download this week’s data and save it in the data folder of your workshop folder (see Introduction to R for how the folder is set up).

lizard_sprint.csv

Create a new R script with the usual header and load the packages. We will use one new package this week, performance, which draws all the assumption-checking plots for a model in one line (the plots themselves come from its partner package, see).

Code
# Week 5 workshop - Statistical inference
# Written by Nicholas Wu, 1234578, 25/03/2027

# Install new packages (only needed once, run in the console)
# install.packages("performance")
# install.packages("see")

# Load packages
library(tidyverse)   # data import, wrangling and plotting
library(performance) # checking model assumptions
library(see)         # draws the plots for the performance package

# Set your working directory to your workshop folder
# setwd("C:/Users/YOUR-NAME/Documents/ECS200")

Study context

Lizards are ectotherms, so their body temperature, and therefore how fast they can run, depends on the temperature around them. Performance usually increases with temperature up to an optimum and then drops off quickly (a thermal performance curve). Populations from hotter places often have a higher optimum.

To test this, 72 adult dragons were collected from a coastal (mild) and an inland (hot) population. In the lab, each lizard was randomly assigned to one test temperature (20, 28 or 36 °C), warmed to that temperature, and raced once along a 1 m racetrack to measure its maximum sprint speed (m/s). Each lizard was used only once, so every row is an independent lizard.

A mountain heath dragon (Rankinia diemensis).
Code
# Load the data
lizard_data <- readr::read_csv("data/lizard_sprint.csv")

# Check the structure: temperature is stored as a number, but in this
# experiment it is a treatment group, so we convert it to a factor.
lizard_data <- lizard_data %>%
  dplyr::mutate(temperature = factor(temperature, levels = c(20, 28, 36)),
                population  = factor(population))

dplyr::glimpse(lizard_data)
Rows: 72
Columns: 5
$ lizard_id    <chr> "L01", "L02", "L03", "L04", "L05", "L06", "L07", "L08", "…
$ population   <fct> inland, coastal, coastal, coastal, inland, coastal, coast…
$ temperature  <fct> 36, 28, 28, 28, 28, 36, 20, 20, 20, 20, 36, 28, 36, 36, 2…
$ mass_g       <dbl> 13.5, 16.1, 10.1, 10.1, 12.3, 10.9, 12.5, 12.4, 12.4, 14.…
$ sprint_speed <dbl> 1.60, 1.53, 1.81, 1.92, 1.44, 1.92, 0.95, 1.49, 1.09, 1.2…

Which test do I use?

All three tests ask the same question: is the difference between group means bigger than we would expect from random variation alone? They differ only in how many groups and how many predictors you have.

Research question Predictor(s) Test R function
Do two groups differ? 1 categorical variable with 2 levels Two-sample t-test t.test()
Do three or more groups differ? 1 categorical variable with 3+ levels One-way ANOVA aov()
Do two factors affect the response, and does the effect of one depend on the other? 2 categorical variables Two-way ANOVA aov(y ~ A * B)
Note

Functions without a package name in front, such as t.test(), aov() and lm(), come with R itself (the stats package), so you never need to install or load them.

Part A: Two groups (t-test)

Question: Do lizards sprint faster at 28 °C than at 20 °C?

1. Background

A two-sample t-test compares the means of two independent groups. It calculates a t value:

\[ t = \frac{\text{difference between the two group means}}{\text{standard error of that difference}} = \frac{\text{signal}}{\text{noise}} \]

A large t (a big difference relative to the variation within groups) gives a small p-value. Three things make a difference easier to detect:

  • a bigger difference between the means (more signal);
  • less variation within each group (less noise);
  • a larger sample size (the standard error shrinks as n grows).

R uses Welch’s t-test by default. It does not assume the two groups have equal variances, so it is a safe default for ecological data.

Use the app below to see how these three ingredients change the result. Every time you move a slider, a new experiment is simulated in your browser.

App: signal versus noise in a t-test

Try this: set the true difference to 0 and press Resample many times. About 1 in 20 experiments will still be “significant” just by chance. That is what a significance level of α = 0.05 means.

Hypotheses:

  • Null hypothesis (H0): mean sprint speed is the same at 20 °C and 28 °C.
  • Alternative hypothesis (H1): mean sprint speed differs between 20 °C and 28 °C.
  • Significance level: α = 0.05.

2. Visualise

First keep only the two temperatures we want to compare, then plot the raw data. A boxplot with the raw points on top shows the centre, spread, shape and any extreme values of each group.

Code
# Keep only the 20 and 28 degree lizards for the t-test
lizard_two <- lizard_data %>%
  dplyr::filter(temperature %in% c("20", "28")) %>%
  dplyr::mutate(temperature = droplevels(temperature)) # drop the unused 36 level

# Boxplot with the raw data
ggplot2::ggplot(lizard_two, ggplot2::aes(x = temperature, y = sprint_speed)) +
  ggplot2::geom_boxplot(outlier.shape = NA, fill = "grey90") + # hide boxplot outliers (raw points show them)
  ggplot2::geom_jitter(width = 0.1, alpha = 0.6) +             # raw data, spread sideways
  ggplot2::labs(x = "Test temperature (°C)", y = "Sprint speed (m/s)") +
  ggplot2::theme_classic()

Code
# Descriptive statistics for each group
lizard_two %>%
  dplyr::group_by(temperature) %>%
  dplyr::summarise(mean = mean(sprint_speed),
                   sd   = sd(sprint_speed),
                   n    = dplyr::n())
# A tibble: 2 × 4
  temperature  mean    sd     n
  <fct>       <dbl> <dbl> <int>
1 20           1.21 0.226    24
2 28           1.82 0.273    24

Lizards at 28 °C look faster than lizards at 20 °C, both groups have a similar spread and there are no extreme values.

3. Check assumptions

Assumption How to check Here
Independence: each observation is from a different, unrelated unit Think about the study design ✅ Each lizard was raced once at one temperature.
Normality: the data in each group are roughly bell-shaped Histogram or Q-Q plot for each group ✅ See below.
Equal variances (only for the classic t-test) Compare the SDs. If the largest SD is less than 2 × the smallest, they are similar enough ✅ Similar SDs, and Welch’s test does not need this anyway.
Code
# Q-Q plot for each group: points should fall close to the line
ggplot2::ggplot(lizard_two, ggplot2::aes(sample = sprint_speed)) +
  ggplot2::stat_qq() +
  ggplot2::stat_qq_line() +
  ggplot2::facet_wrap(~ temperature) +
  ggplot2::labs(x = "Theoretical quantiles", y = "Sprint speed (m/s)") +
  ggplot2::theme_classic()

What you see Why it is a problem What to do instead
Strongly skewed data or big outliers, especially with small samples The mean is pulled around by extreme values Log-transform the response (Week 2), or use the non-parametric Wilcoxon rank-sum test, wilcox.test(y ~ group, data = ...)
The same individuals measured twice (before/after, left/right) Observations are not independent Paired t-test, t.test(before, after, paired = TRUE), or a mixed model (Week 12)
The response is a count (e.g. number of seedlings) with many zeros Counts are not normally distributed Generalised linear model for counts (Week 8)
The response is yes/no (e.g. alive/dead) Not a continuous measurement Logistic regression (Week 11)
More than two groups Running many t-tests inflates false positives One-way ANOVA (Part B)

For example, the Wilcoxon test compares the ranks of the values instead of the values themselves, so a single extreme value has little influence:

Code
wilcox.test(sprint_speed ~ temperature, data = lizard_two)

    Wilcoxon rank sum exact test

data:  sprint_speed by temperature
W = 25.5, p-value = 6.085e-10
alternative hypothesis: true location shift is not equal to 0

4. Model

The formula response ~ group reads “sprint speed explained by temperature”.

Code
# Welch two-sample t-test (two-sided)
lizard_ttest <- t.test(sprint_speed ~ temperature, data = lizard_two)
lizard_ttest

    Welch Two Sample t-test

data:  sprint_speed by temperature
t = -8.3763, df = 44.412, p-value = 1.088e-10
alternative hypothesis: true difference in means between group 20 and group 28 is not equal to 0
95 percent confidence interval:
 -0.7515599 -0.4601067
sample estimates:
mean in group 20 mean in group 28 
        1.212083         1.817917 

5. Interpret

The output gives everything you need:

  • t = ...: the t value. Its sign only tells you which group was first alphabetically.
  • df = ...: degrees of freedom (Welch’s test gives a decimal value).
  • p-value = ...: the probability of seeing a difference this large if the null hypothesis were true.
  • 95 percent confidence interval: the likely range for the true difference in means (group 1 minus group 2). If it does not include 0, the difference is significant at α = 0.05.
  • sample estimates: the mean of each group.

Methods sentence: Sprint speed of lizards tested at 20 °C and 28 °C was compared using a two-sided Welch’s two-sample t-test in R (version 4.5.1). Significance was set at α = 0.05.

Results sentence (direction + size + statistical support + figure):

Lizards sprinted faster at 28 °C (mean ± SD = 1.82 ± 0.27 m/s) than at 20 °C (1.21 ± 0.23 m/s), an average increase of 0.61 m/s (95% CI 0.46 to 0.75 m/s; Welch’s t44.4 = 8.38, p < 0.001; Fig. 1).

Part B: Three or more groups (one-way ANOVA)

Question: Does sprint speed differ among the three test temperatures (20, 28 and 36 °C)?

1. Background

With three groups you could run three t-tests (20 vs 28, 20 vs 36, 28 vs 36), but every extra test adds another 5% chance of a false positive. A one-way analysis of variance (ANOVA) asks a single question first: do all groups share the same mean, or does at least one differ?

ANOVA compares two kinds of variation:

  • Between-group variation: how far the group means are from the overall mean (signal).
  • Within-group variation: how spread out individuals are inside each group (noise).

\[ F = \frac{\text{variation between groups}}{\text{variation within groups}} \]

A large F means the group means are further apart than the noise would suggest. If the ANOVA is significant, a post hoc test (Tukey’s HSD) then tells you which groups differ, while keeping the overall false-positive rate at 5%.

App: between- versus within-group variation

Hypotheses:

  • H0: mean sprint speed is the same at all three temperatures.
  • H1: at least one temperature has a different mean sprint speed.

2. Visualise

Code
# Boxplot with raw data for all three temperatures
ggplot2::ggplot(lizard_data, ggplot2::aes(x = temperature, y = sprint_speed)) +
  ggplot2::geom_boxplot(outlier.shape = NA, fill = "grey90") +
  ggplot2::geom_jitter(width = 0.1, alpha = 0.6) +
  ggplot2::labs(x = "Test temperature (°C)", y = "Sprint speed (m/s)") +
  ggplot2::theme_classic()

Code
# Mean, SD and sample size per temperature
lizard_data %>%
  dplyr::group_by(temperature) %>%
  dplyr::summarise(mean = mean(sprint_speed),
                   sd   = sd(sprint_speed),
                   n    = dplyr::n())
# A tibble: 3 × 4
  temperature  mean    sd     n
  <fct>       <dbl> <dbl> <int>
1 20           1.21 0.226    24
2 28           1.82 0.273    24
3 36           1.80 0.249    24

Speed looks lowest at 20 °C and highest at 28 °C, and the spread is similar across groups.

3. Check assumptions

The assumptions are the same as for the t-test (independence, normality and equal variances), but for ANOVA we check them on the residuals. A residual is the distance between an observation and its group mean, the part of the data the model cannot explain.

The performance::check_model() function draws the main diagnostic plots, and each panel has a subtitle telling you what a “good” plot looks like.

Code
# Fit the one-way ANOVA first, then check its residuals
lizard_aov <- aov(sprint_speed ~ temperature, data = lizard_data)

performance::check_model(lizard_aov, check = c("homogeneity", "qq"))

Assumption Plot Here
Equal variances Homogeneity of variance: the reference line should be flat and horizontal ✅ Roughly flat.
Normal residuals Normality of residuals: dots should fall along the line ✅ Dots follow the line.
Independence Study design ✅ One lizard per measurement.
What you see What to do instead
Very unequal spread among groups (largest SD > 2 × smallest) Welch’s ANOVA, oneway.test(y ~ group, data = ...), which does not assume equal variances
Strongly skewed data or outliers with small samples Log-transform, or the non-parametric Kruskal–Wallis test, kruskal.test(y ~ group, data = ...)
Repeated measurements on the same individuals or sites Mixed-effects model (Week 12)
Counts or yes/no responses Generalised linear models (Weeks 8 and 11)
A numeric predictor (e.g. temperature measured as 18.2, 22.7, 25.1 °C…) Linear regression (Week 7)
Code
# Example: Kruskal-Wallis test, the non-parametric alternative
kruskal.test(sprint_speed ~ temperature, data = lizard_data)

    Kruskal-Wallis rank sum test

data:  sprint_speed by temperature
Kruskal-Wallis chi-squared = 38.754, df = 2, p-value = 3.843e-09

4. Model

We already fitted the model above with aov(). summary() gives the ANOVA table.

Code
# ANOVA table
summary(lizard_aov)
            Df Sum Sq Mean Sq F value   Pr(>F)    
temperature  2  5.670  2.8350   45.33 2.69e-13 ***
Residuals   69  4.315  0.0625                     
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Code
# Which temperatures differ? Tukey's honestly significant difference test
TukeyHSD(lizard_aov)
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = sprint_speed ~ temperature, data = lizard_data)

$temperature
             diff        lwr       upr     p adj
28-20  0.60583333  0.4329134 0.7787533 0.0000000
36-20  0.58416667  0.4112467 0.7570866 0.0000000
36-28 -0.02166667 -0.1945866 0.1512533 0.9515937

You get exactly the same F test with lm(). The lm() summary also shows the difference of each group from the first (reference) group and the R2, the proportion of variation in sprint speed explained by temperature. This is the link to linear regression in Week 7.

Code
lizard_lm <- lm(sprint_speed ~ temperature, data = lizard_data)
summary(lizard_lm)

Call:
lm(formula = sprint_speed ~ temperature, data = lizard_data)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.55625 -0.17771 -0.00208  0.13583  0.60375 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept)    1.21208    0.05105  23.745  < 2e-16 ***
temperature28  0.60583    0.07219   8.392 3.83e-12 ***
temperature36  0.58417    0.07219   8.092 1.36e-11 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.2501 on 69 degrees of freedom
Multiple R-squared:  0.5678,    Adjusted R-squared:  0.5553 
F-statistic: 45.33 on 2 and 69 DF,  p-value: 2.689e-13

5. Interpret

Reading the output:

  • ANOVA table: temperature has 2 and 69 degrees of freedom, F = 45.33 and p < 0.001, so at least one temperature differs.
  • Tukey’s HSD: each row is one pair of temperatures. diff is the difference in means, lwr and upr are its 95% confidence interval, and p adj is the p-value adjusted for making three comparisons.

Methods sentence: Differences in sprint speed among three test temperatures (20 °C, 28 °C, 36 °C) were tested with a one-way ANOVA using the aov() function, followed by Tukey’s HSD post hoc comparisons with the TukeyHSD() function in R. Model assumptions were checked with residual diagnostic plots using the check_model() function from the performance R package (Lüdecke et al., 2021). Significance was set at α = 0.05.

Results sentence:

Sprint speed differed among test temperatures (one-way ANOVA: F2,69 = 45.33, p < 0.001; R2 = 0.57; Fig. 2). Lizards were 0.61 m/s faster at 28 °C than at 20 °C (95% CI 0.43–0.78, Tukey p < 0.001) and 0.58 m/s faster at 36 °C than at 20 °C (p < 0.001), but speed at 28 °C and 36 °C did not differ (difference = -0.02 m/s, p = 0.95).

Part C: Two factors (two-way ANOVA)

Question: Does the effect of temperature on sprint speed differ between the coastal and inland populations?

1. Background

A two-way ANOVA tests two categorical predictors at once (here temperature and population) and, most importantly, their interaction. Three questions are tested:

  1. Main effect of temperature: averaged over both populations, does speed change with temperature?
  2. Main effect of population: averaged over all temperatures, is one population faster?
  3. Interaction (temperature × population): does the effect of temperature depend on which population the lizard came from?

If there is no interaction, the lines on an interaction plot (mean of each group joined by population) are parallel. If the lines are not parallel or they cross, the effect of one factor depends on the other. Use the app to build some interaction plots.

App: what does an interaction look like?

A visual explanation of interactive effects.

2. Visualise

Code
# Mean and standard error for every temperature x population combination
lizard_summary <- lizard_data %>%
  dplyr::group_by(temperature, population) %>%
  dplyr::summarise(mean = mean(sprint_speed),
                   se   = sd(sprint_speed) / sqrt(dplyr::n()),
                   n    = dplyr::n(),
                   .groups = "drop")  # remove the grouping after summarising
lizard_summary
# A tibble: 6 × 5
  temperature population  mean     se     n
  <fct>       <fct>      <dbl>  <dbl> <int>
1 20          coastal     1.2  0.0728    12
2 20          inland      1.22 0.0595    12
3 28          coastal     1.94 0.0720    12
4 28          inland      1.70 0.0718    12
5 36          coastal     1.68 0.0664    12
6 36          inland      1.91 0.0628    12
Code
# Interaction plot: raw data (faint) with group means +/- SE joined by lines
ggplot2::ggplot() +
  ggplot2::geom_jitter(data = lizard_data,
                       ggplot2::aes(x = temperature, y = sprint_speed, colour = population),
                       position = ggplot2::position_jitterdodge(jitter.width = 0.1, dodge.width = 0.4),
                       alpha = 0.3) +
  ggplot2::geom_pointrange(data = lizard_summary,
                           ggplot2::aes(x = temperature, y = mean, ymin = mean - se, ymax = mean + se,
                                        colour = population),
                           position = ggplot2::position_dodge(width = 0.4)) +
  ggplot2::geom_line(data = lizard_summary,
                     ggplot2::aes(x = temperature, y = mean, colour = population, group = population),
                     position = ggplot2::position_dodge(width = 0.4)) +
  ggplot2::scale_colour_manual(values = c("grey40", "#e12744")) +
  ggplot2::labs(x = "Test temperature (°C)", y = "Sprint speed (m/s)", colour = "Population") +
  ggplot2::theme_classic()

The lines cross: coastal lizards are fastest at 28 °C, while inland lizards keep getting faster up to 36 °C. This suggests an interaction.

3. Check assumptions

The assumptions are the same as for the one-way ANOVA. Fit the model first, then check the residuals.

Code
# Two-way ANOVA with interaction. temperature * population is shorthand for
# temperature + population + temperature:population
lizard_aov2 <- aov(sprint_speed ~ temperature * population, data = lizard_data)

performance::check_model(lizard_aov2, check = c("homogeneity", "qq"))

Both plots look fine and every lizard was tested only once, so the assumptions are met.

  • Very unequal sample sizes among the combinations, or missing combinations: the order of terms then changes the results. Keep designs balanced where possible (see Research Design).
  • A numeric predictor (e.g. body mass) as well as a factor: use a linear model with both (Week 7).
  • Repeated measures, e.g. the same lizard raced at all three temperatures: observations from one lizard are not independent, so use a mixed-effects model with lizard as a random effect (Week 12).
  • Counts or yes/no responses: use generalised linear models (Weeks 8 and 11).

4. Model

Code
# ANOVA table for the two-way model
summary(lizard_aov2)
                       Df Sum Sq Mean Sq F value   Pr(>F)    
temperature             2  5.670  2.8350  51.482 3.37e-14 ***
population              1  0.000  0.0004   0.007  0.93222    
temperature:population  2  0.680  0.3401   6.177  0.00348 ** 
Residuals              66  3.634  0.0551                     
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Because the interaction is significant, the next step is to compare temperatures within each population. TukeyHSD() on the interaction term compares all 15 pairs of the six groups. You only need the rows where the population is the same on both sides, e.g. 28:coastal-20:coastal (coastal lizards at 28 °C minus coastal lizards at 20 °C).

Code
# All pairwise comparisons of the 6 temperature x population groups
TukeyHSD(lizard_aov2, which = "temperature:population")
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = sprint_speed ~ temperature * population, data = lizard_data)

$`temperature:population`
                             diff         lwr         upr     p adj
28:coastal-20:coastal  0.73916667  0.45797958  1.02035375 0.0000000
36:coastal-20:coastal  0.48000000  0.19881292  0.76118708 0.0000618
20:inland-20:coastal   0.02416667 -0.25702042  0.30535375 0.9998544
28:inland-20:coastal   0.49666667  0.21547958  0.77785375 0.0000322
36:inland-20:coastal   0.71250000  0.43131292  0.99368708 0.0000000
36:coastal-28:coastal -0.25916667 -0.54035375  0.02202042 0.0877563
20:inland-28:coastal  -0.71500000 -0.99618708 -0.43381292 0.0000000
28:inland-28:coastal  -0.24250000 -0.52368708  0.03868708 0.1299626
36:inland-28:coastal  -0.02666667 -0.30785375  0.25452042 0.9997638
20:inland-36:coastal  -0.45583333 -0.73702042 -0.17464625 0.0001561
28:inland-36:coastal   0.01666667 -0.26452042  0.29785375 0.9999768
36:inland-36:coastal   0.23250000 -0.04868708  0.51368708 0.1621037
28:inland-20:inland    0.47250000  0.19131292  0.75368708 0.0000825
36:inland-20:inland    0.68833333  0.40714625  0.96952042 0.0000000
36:inland-28:inland    0.21583333 -0.06535375  0.49702042 0.2283272

5. Interpret

Always read the interaction row first. If the interaction is significant, the main effects can be misleading on their own. Here the main effect of population is not significant, yet the populations clearly respond differently to temperature.

Results sentence:

The effect of temperature on sprint speed depended on population (two-way ANOVA, temperature × population: F2,66 = 6.18, p = 0.003; Fig. 3). Coastal lizards were fastest at 28 °C and slowed down at 36 °C, whereas inland lizards continued to increase their speed up to 36 °C. Averaged over populations, speed increased with temperature (F2,66 = 51.48, p < 0.001), and there was no overall difference between populations (F1,66 = 0.01, p = 0.932). This is consistent with inland lizards having a higher thermal optimum.

Writing results

Every results statement should include:

  • the direction of the effect (higher, lower, increase, decrease);
  • the magnitude (how big is the difference, ideally with a 95% CI);
  • the statistical support (test statistic, degrees of freedom, p-value);
  • a figure showing the raw data and the means;
  • any assumptions or caveats.

For example: “Seedlings in the drought treatment grew half as fast as the well-watered control seedlings (control = 3.0 ± 0.45 mm/day, drought = 1.5 ± 0.31 mm/day; t53 = 5.06, p < 0.001; Fig. 2).”

Assessment

Task to complete before the end of the workshop. Download the template script and the two datasets, save them in your data folder, and complete the questions in the script.

week05_assessment.R invaded.csv altitude.csv

TipFrom hypothesis to variables

Before writing any code, find the two variables your hypothesis is about:

  1. Response variable (y): the thing you measured and expect to change. It goes on the y-axis and on the left of the ~ in a formula.
  2. Predictor variable (x): the groups or treatment you think cause the change. It goes on the x-axis and on the right of the ~.
  3. Unit of replication: what one row of the data represents (one quadrat, one plot, one lizard). Each row should be an independent replicate.

A quick way to find them is to fill in this sentence: “Does [response] differ between/among [groups of the predictor]?“ For the lizard example: “Does sprint speed differ among test temperatures?“ gives sprint_speed ~ temperature.

Then check the type of each variable with dplyr::glimpse(). For these tests the response must be numeric (<dbl> or <int>) and the predictor must be categorical (convert it with factor()).

Question 1. Invasive plants and native species (t-test). An exotic grass has spread into part of a native grassland. Ecologists suspect the grass crowds out native plants. They placed 1 m2 quadrats in the invaded area and in a nearby uninvaded area and counted the number of native plant species in each quadrat.

Hypothesis: native plant species richness differs between invaded and uninvaded areas.

Variable in invaded.csv Role Type Description
Native_count Response (y) Numeric (count) Number of native plant species in a 1 m2 quadrat
Group Predictor (x) Categorical, 2 levels Whether the quadrat was in the Invaded or Uninvaded area

Each row is one quadrat, so quadrats are the replicates.

  1. Name the response and predictor variables, and say what one row of the data represents. Write the null and alternative hypotheses using these variables.
  2. Convert Group to a factor. Visualise Native_count for each Group (boxplot with the raw points) and calculate the mean, SD and n of each group.
  3. Check the assumptions (independence, normality, equal variances) and say whether each one is met.
  4. Run a Welch’s t-test with the formula Native_count ~ Group and write a results sentence (direction, size of the difference, t, df and p).

Question 2. Plant species richness along an altitude gradient (one-way ANOVA). Temperature and growing-season length change with altitude, which may affect how many plant species can live in a place. Plots were surveyed at low, mid and high altitude on a mountain and the number of plant species in each plot was recorded.

Hypothesis: plant species richness differs among altitudes.

Variable in altitude.csv Role Type Description
Richness Response (y) Numeric Number of plant species recorded in a plot (estimated, so it has decimals)
Altitude Predictor (x) Categorical, 3 levels Altitude band of the plot: Low, Mid or High

Each row is one plot, so plots are the replicates.

  1. Name the response and predictor variables, say what one row represents, and write the null and alternative hypotheses.
  2. Convert Altitude to a factor in the order Low, Mid, High (otherwise R puts them in alphabetical order: High, Low, Mid).
  3. Visualise Richness for each Altitude and calculate descriptive statistics.
  4. Fit a one-way ANOVA with the formula Richness ~ Altitude and check its assumptions.
  5. Run Tukey’s HSD and write a results sentence that says which altitudes differ.

Submit one R script (.R) before the end of the class. Write your answers to written questions as # comments next to the code that produced them.

Score What it looks like
0 No submission or no attempt.
0.25 Submitted, but a very poor attempt (e.g. incomplete, or lots of gaps).
0.5 Most questions answered, or all answered but most annotations missing.
0.75 All questions answered, but some annotations missing or some answers incorrect.
1 Everything completed, with annotations on all code so the marker can follow it.

Tip: an annotation is a short # comment that says what the line does and why. For example: # remove the one plant with a stem dry mass of 7.4 g (data entry error).

Additional resources

  • Everything is a linear model: a short, visual explanation of why the t-test, ANOVA and regression are the same model.
  • Coding Club: ANOVA from A to (XY)Z.
  • Estimation statistics: a web app for reporting effect sizes with confidence intervals instead of p-values alone.
  • Steel et al. (2013) Applied statistics in ecology: common pitfalls and simple solutions. Reading pages 1–4 covers the most common mistakes with these tests.
  • Quinn & Keough (2002) Experimental Design and Data Analysis for Biologists, chapters 8 (one-factor ANOVA) and 9 (multifactor ANOVA), for more depth.
  • CrashCourse Statistics: ANOVA (14 min video).