library(tidyverse)
library(car) # leveneTest()
library(perm) # permTS() permutation test
library(effectsize) # cohens_d()
pine_df <- read_csv("data/pine_data.csv")
lt_df <- read_csv("data/lake_trout.csv")Non-Parametric Two-Group Tests
Mann-Whitney U, Wilcoxon signed-rank, permutation
📥 Download the R script 📊 pine_data.csv 📊 lake_trout.csv
What these tests do
When the data are badly skewed, have outliers, or variances differ a lot, the t-test’s assumptions fail. Non-parametric tests drop the normality assumption by working on ranks (or by shuffling the data) instead of the raw values.
| Parametric test | Non-parametric partner | R call |
|---|---|---|
| Two-sample t-test | Mann-Whitney U (Wilcoxon rank-sum) | wilcox.test(y ~ group) |
| Paired t-test | Wilcoxon signed-rank | wilcox.test(a, b, paired = TRUE) |
| Either | Permutation test | perm::permTS(y ~ group) |
\(H_0\): the two groups have the same distribution. \(H_A\): one group tends to have larger values.
Non-parametric is not assumption-free: observations must still be independent, and the Mann-Whitney test assumes the two distributions have a similar shape.
Step 1: Load packages and data
Step 2: Ranks are all these tests see
Rank the pine data (one value per tree-side, as in the t-test vignette):
p_df <- pine_df %>%
group_by(team, side) %>%
summarise(needle_length_mm = mean(needle_length_mm, na.rm = TRUE),
.groups = "drop")
p_df %>%
mutate(rank = rank(needle_length_mm)) %>%
arrange(rank)# A tibble: 8 × 4
team side needle_length_mm rank
<chr> <chr> <dbl> <dbl>
1 waterbugs shady 11.7 1
2 one shady 13.2 2
3 waterbugs sunny 13.8 3
4 one sunny 14.2 4
5 grace_jace shady 15.4 5
6 grace_jace sunny 15.5 6
7 botany shady 16.1 7
8 botany sunny 17.3 8
If one side holds most of the high ranks, the test will find a difference.
Step 3: Mann-Whitney U and Wilcoxon signed-rank (pine data)
# Unpaired: Mann-Whitney U (partner of the two-sample t-test)
wilcox.test(needle_length_mm ~ side, data = p_df)
Wilcoxon rank sum exact test
data: needle_length_mm by side
W = 5, p-value = 0.4857
alternative hypothesis: true location shift is not equal to 0
p_wide_df <- p_df %>%
pivot_wider(names_from = side, values_from = needle_length_mm)
# Paired: Wilcoxon signed-rank (partner of the paired t-test)
wilcox.test(p_wide_df$shady, p_wide_df$sunny, paired = TRUE)
Wilcoxon signed rank exact test
data: p_wide_df$shady and p_wide_df$sunny
V = 0, p-value = 0.125
alternative hypothesis: true location shift is not equal to 0
With only 4 pairs, the smallest possible two-sided p is 2 / 2^4 = 0.125. The test cannot reach p < 0.05 no matter how big the effect. Rank tests need more data than t-tests.
Step 4: Effect size (Cohen’s d)
A p-value says whether; an effect size says how big.
cohens_d(p_wide_df$shady, p_wide_df$sunny, paired = TRUE)For paired samples, 'repeated_measures_d()' provides more options.
Cohen's d | 95% CI
-------------------------
-1.36 | [-2.74, 0.10]
Rough guide: 0.2 small, 0.5 medium, 0.8 large.
Step 5: A real skewed example (lake trout mass)
Compare trout mass in NE 12 and Island Lake.
isl_ne12_df <- lt_df %>%
filter(lake %in% c("NE 12", "Island Lake")) %>%
filter(!is.na(mass_g))
isl_ne12_df %>%
group_by(lake) %>%
summarize(n = n(),
median = median(mass_g),
mean = mean(mass_g),
sd = sd(mass_g))# A tibble: 2 × 5
lake n median mean sd
<chr> <int> <dbl> <dbl> <dbl>
1 Island Lake 10 2475 3165 1617.
2 NE 12 322 336. 534. 520.
isl_ne12_df %>%
ggplot(aes(lake, mass_g)) +
geom_boxplot() +
theme_bw()
isl_ne12_df %>%
ggplot(aes(sample = mass_g, color = lake)) +
stat_qq() +
stat_qq_line() +
theme_bw()
Step 6: Check the t-test assumptions
shapiro.test(isl_ne12_df$mass_g[isl_ne12_df$lake == "NE 12"])
Shapiro-Wilk normality test
data: isl_ne12_df$mass_g[isl_ne12_df$lake == "NE 12"]
W = 0.85148, p-value < 2.2e-16
leveneTest(mass_g ~ factor(lake), data = isl_ne12_df)Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 1 25.997 5.775e-07 ***
330
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
NE 12 is strongly right-skewed and the variances are very unequal, and the sample sizes are very unbalanced (322 vs 10). A transformation is one fix; a non-parametric test is another.
# Does a log transformation fix the variances?
leveneTest(log10(mass_g) ~ factor(lake), data = isl_ne12_df)Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 1 11.77 0.0006784 ***
330
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Step 7: Mann-Whitney U on the trout
wilcox_test_result <- wilcox.test(mass_g ~ lake, data = isl_ne12_df)
wilcox_test_result
Wilcoxon rank sum test with continuity correction
data: mass_g by lake
W = 3205.5, p-value = 9.506e-08
alternative hypothesis: true location shift is not equal to 0
W is the Mann-Whitney U statistic: how many times an Island Lake fish outweighs an NE 12 fish. The warning about ties just means some fish had the same mass, so R uses a normal approximation.
Step 8: Permutation test
A permutation test shuffles the group labels thousands of times and asks how often a shuffled difference is as big as the real one. It works on the actual means, not ranks. We balance the design first by sampling 10 NE 12 fish.
set.seed(123)
perm_df <- bind_rows(
isl_ne12_df %>% filter(lake == "NE 12") %>% slice_sample(n = 10),
isl_ne12_df %>% filter(lake == "Island Lake")
)
permTS(mass_g ~ lake,
data = perm_df,
alternative = "two.sided",
method = "exact.mc",
control = permControl(nmc = 10000))
Exact Permutation Test Estimated by Monte Carlo
data: mass_g by lake
p-value = 2e-04
alternative hypothesis: true mean lake=Island Lake - mean lake=NE 12 is not equal to 0
sample estimates:
mean lake=Island Lake - mean lake=NE 12
2519.9
p-value estimated from 10000 Monte Carlo replications
99 percent confidence interval on p-value:
0.000000000 0.001059383
Step 9: Write it up
Lake trout from Island Lake were heavier than those from NE 12 (median 2475 g vs. 335.5 g; Mann-Whitney U test: W = 3205.5, p < 0.001, n = 10 and 322).
For rank tests, report medians (not means), the test statistic, p, and both sample sizes.