Skip to main content

T-Tests: One, Two, and Paired

Parametric t-tests

One-sample, two-sample (Student’s and Welch’s), and paired t-tests on the pine needle data from lecture.
Author

Bill Perry

📥 Download the R script 📊 Download the data

Put the script and a data/ folder holding pine_data.csv in your project, then run the script from top to bottom.

What these tests do

A t-test compares a mean to something: a fixed number (one-sample), another group’s mean (two-sample), or the same unit measured twice (paired). Each one asks whether a difference is bigger than chance would produce.

Test Question R call
One-sample Is the mean different from a number? t.test(x, mu = 15)
Two-sample Do two group means differ? t.test(y ~ group, var.equal = TRUE)
Welch Same, but variances may differ t.test(y ~ group, var.equal = FALSE)
Paired Is the mean difference within pairs zero? t.test(a, b, paired = TRUE)

Data: pine needle lengths (mm) from the sunny and shady sides of trees, 8 needles per side per tree, one tree per team.

Step 1: Load packages and data

library(tidyverse)
library(car)   # leveneTest(), qqPlot()

pine_df <- read_csv("data/pine_data.csv")

pine_df
# A tibble: 64 × 6
   source_file            team  replicate side  needle_length_mm needle_width_mm
   <chr>                  <chr>     <dbl> <chr>            <dbl>           <dbl>
 1 Bartell_Shingledecker… grac…         1 sunny             18.4            0.79
 2 Bartell_Shingledecker… grac…         2 sunny             12.5            0.99
 3 Bartell_Shingledecker… grac…         3 sunny             17.7            1.12
 4 Bartell_Shingledecker… grac…         4 sunny             14.0            0.94
 5 Bartell_Shingledecker… grac…         5 sunny             14.2            1   
 6 Bartell_Shingledecker… grac…         6 sunny             19.2            0.99
 7 Bartell_Shingledecker… grac…         7 sunny             10.9            0.81
 8 Bartell_Shingledecker… grac…         8 sunny             17.3            1.29
 9 Bartell_Shingledecker… grac…         1 shady             16.6            0.75
10 Bartell_Shingledecker… grac…         2 shady             17.0            0.84
# ℹ 54 more rows

Step 2: Average away the pseudoreplicates

The 8 needles from one side of one tree are not independent. Average them so each tree-side is one data point.

p_df <- pine_df %>%
  group_by(team, side) %>%
  summarise(needle_length_mm = mean(needle_length_mm, na.rm = TRUE),
            .groups = "drop")

p_df
# A tibble: 8 × 3
  team       side  needle_length_mm
  <chr>      <chr>            <dbl>
1 botany     shady             16.1
2 botany     sunny             17.3
3 grace_jace shady             15.4
4 grace_jace sunny             15.5
5 one        shady             13.2
6 one        sunny             14.2
7 waterbugs  shady             11.7
8 waterbugs  sunny             13.8

Step 3: Summary statistics

stats_df <- p_df %>%
  group_by(side) %>%
  summarize(
    n    = sum(!is.na(needle_length_mm)),
    mean = mean(needle_length_mm, na.rm = TRUE),
    sd   = sd(needle_length_mm, na.rm = TRUE),
    se   = sd / sqrt(n),
    .groups = "drop"
  )
stats_df
# A tibble: 2 × 5
  side      n  mean    sd    se
  <chr> <int> <dbl> <dbl> <dbl>
1 shady     4  14.1  2.02 1.01 
2 sunny     4  15.2  1.57 0.786

Step 4: Plot the data

Always plot before testing. The boxplot shows spread and outliers; the slope plot shows the pairing (one line per tree).

box_plot <- p_df %>%
  ggplot(aes(side, needle_length_mm)) +
  geom_boxplot() +
  geom_point() +
  theme_bw()
box_plot

slope_plot <- p_df %>%
  ggplot(aes(side, needle_length_mm, group = team)) +
  geom_line() +
  geom_point() +
  theme_bw()
slope_plot

Step 5: One-sample t-test

Question: is the mean shady-side needle length different from 15 mm?

\(H_0: \mu = 15\) vs. \(H_A: \mu \neq 15\)

shady_df <- p_df %>% filter(side == "shady")
sunny_df <- p_df %>% filter(side == "sunny")

# assumption: the shady values are roughly normal
qqPlot(shady_df$needle_length_mm)

[1] 4 1
shapiro.test(shady_df$needle_length_mm)

    Shapiro-Wilk normality test

data:  shady_df$needle_length_mm
W = 0.94326, p-value = 0.6743
one_sample_model <- t.test(shady_df$needle_length_mm, mu = 15)
one_sample_model

    One Sample t-test

data:  shady_df$needle_length_mm
t = -0.87183, df = 3, p-value = 0.4475
alternative hypothesis: true mean is not equal to 15
95 percent confidence interval:
 10.91210 17.32978
sample estimates:
mean of x 
 14.12094 

For a one-tailed test, add alternative = "less" or "greater":

t.test(shady_df$needle_length_mm, mu = 15, alternative = "less")

    One Sample t-test

data:  shady_df$needle_length_mm
t = -0.87183, df = 3, p-value = 0.2237
alternative hypothesis: true mean is less than 15
95 percent confidence interval:
     -Inf 16.49382
sample estimates:
mean of x 
 14.12094 

Step 6: Two-sample t-test

Question: does mean needle length differ between sides?

\(H_0: \mu_{shady} = \mu_{sunny}\) vs. \(H_A: \mu_{shady} \neq \mu_{sunny}\)

Check the assumptions — normality within each group and equal variances:

p_df %>%
  group_by(side) %>%
  summarize(shapiro_p = shapiro.test(needle_length_mm)$p.value,
            .groups = "drop")
# A tibble: 2 × 2
  side  shapiro_p
  <chr>     <dbl>
1 shady     0.674
2 sunny     0.516
leveneTest(needle_length_mm ~ factor(side), data = p_df)
Levene's Test for Homogeneity of Variance (center = median)
      Df F value Pr(>F)
group  1  0.6355 0.4557
       6               

Both p > 0.05 means no evidence against normality or equal variances. With only 4 points per side, these tests have very little power, so look at the plots too.

two_sample_model <- t.test(needle_length_mm ~ side,
                           data = p_df, var.equal = TRUE)
two_sample_model

    Two Sample t-test

data:  needle_length_mm by side
t = -0.85529, df = 6, p-value = 0.4252
alternative hypothesis: true difference in means between group shady and group sunny is not equal to 0
95 percent confidence interval:
 -4.221663  2.034788
sample estimates:
mean in group shady mean in group sunny 
           14.12094            15.21438 

Step 7: Welch’s t-test

Welch’s version does not assume equal variances. It is R’s default (var.equal = FALSE) and is a safe choice whenever Levene’s test or the boxplot suggests unequal spread.

welch_model <- t.test(needle_length_mm ~ side,
                      data = p_df, var.equal = FALSE)
welch_model

    Welch Two Sample t-test

data:  needle_length_mm by side
t = -0.85529, df = 5.6627, p-value = 0.4271
alternative hypothesis: true difference in means between group shady and group sunny is not equal to 0
95 percent confidence interval:
 -4.267394  2.080519
sample estimates:
mean in group shady mean in group sunny 
           14.12094            15.21438 

Note the fractional df. That is the Welch correction.

Step 8: Paired t-test

Both sides were measured on the same tree, so the right test pairs them. First make one column per side:

p_wide_df <- p_df %>%
  pivot_wider(names_from = side,
              values_from = needle_length_mm)
p_wide_df
# A tibble: 4 × 3
  team       shady sunny
  <chr>      <dbl> <dbl>
1 botany      16.1  17.3
2 grace_jace  15.4  15.5
3 one         13.2  14.2
4 waterbugs   11.7  13.8
paired_model <- t.test(p_wide_df$shady,
                       p_wide_df$sunny,
                       paired = TRUE)
paired_model

    Paired t-test

data:  p_wide_df$shady and p_wide_df$sunny
t = -2.7227, df = 3, p-value = 0.07238
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
 -2.3714897  0.1846147
sample estimates:
mean difference 
      -1.093438 

A paired t-test is the same as a one-sample t-test on the differences:

diff_df <- p_wide_df %>%
  mutate(difference = sunny - shady)

t.test(diff_df$difference, mu = 0)

    One Sample t-test

data:  diff_df$difference
t = 2.7227, df = 3, p-value = 0.07238
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 -0.1846147  2.3714897
sample estimates:
mean of x 
 1.093438 

Step 9: Reading the output

  • t: the difference divided by its standard error. Bigger |t| means stronger evidence.
  • df: n − 1 (one-sample, paired), n₁ + n₂ − 2 (two-sample), or a fractional value (Welch).
  • p-value: the chance of a t this extreme if \(H_0\) were true. Below 0.05, reject \(H_0\).
  • 95% CI: the plausible range for the mean (or difference). If it excludes the null value, p < 0.05.

Pairing removes tree-to-tree variation, so the paired test has a much smaller standard error than the two-sample test on the same numbers. Compare the two p-values.

Step 10: Write it up

Sunny-side needles were on average 1.09 mm longer than shady-side needles, but the difference was not significant (paired t-test: t(3) = -2.72, p = 0.072, n = 4 trees; 95% CI for shady − sunny: -2.37 to 0.18 mm).

Give the test, t, df, p, n, and the size of the effect with its CI. Report means ± SE from Step 3 in a table or figure. With only 4 trees, a non-significant result says little; the CI shows how wide the uncertainty is.