Permutation tests in R: let your data build its own null distribution

[This article was first published on R on Stats and R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Note: This post has been written in collaboration with Waqar Ahmad, founder of CalcMega.

Introduction

Every R user meets the same small anxiety early on. You have two groups, you want to know whether they really differ, and the t-test relies on a normal-model reference distribution for small-sample inference. With 200 observations per group, that approximation is often robust. With 10 plants, it deserves scrutiny: a normality test on 10 points has little power to rule out departures, and the central limit theorem is no automatic guarantee.

Permutation tests avoid a normal reference distribution, but they do not remove the need for assumptions. They were worked out in the 1930s, in the early days of modern statistics (Fisher’s The Design of Experiments appeared in 1935), and the idea is disarmingly simple: if the treatment did nothing, then the group labels are arbitrary, and shuffling them changes nothing that matters.

That last clause is the key assumption, and it has a name: exchangeability. In a randomized experiment, permuting assignments allowed by the design gives an exact test of the sharp null that treatment affects no individual. Without randomization, validity instead requires exchangeability under the null; equality of means alone is not enough when group distributions differ. A permutation test turns exchangeability into a recipe:

  1. Compute your test statistic on the data as observed, say, the difference in group means.
  2. Shuffle the group labels at random and recompute the statistic.
  3. Repeat step 2 many times. The collection of shuffled statistics is the permutation distribution: what your statistic looks like when the labels mean nothing.
  4. The p-value is the proportion of shuffles producing a statistic at least as extreme as the one you actually observed.

No normal reference distribution is invoked. When the design or exchangeability assumptions hold, this can provide valid inference with small or skewed samples; small sample size does not make arbitrary shuffling safe, and it can limit power and p-value resolution. One clarification before we start, because it is the most common misunderstanding of the method: a permutation p-value is not “the probability that the null hypothesis is true.” It is the proportion of allowed rearrangements that produce a statistic at least as extreme as yours, a statement about how surprising your data would be under the specified null. That is the same careful interpretation a t-test’s p-value demands; permutation tests replace a distributional assumption with design or exchangeability assumptions rather than removing assumptions.

In this article we work two complete examples: an independent two-sample test on the PlantGrowth dataset, and a paired test on the sleep dataset, where the shuffling turns into something even neater: flipping signs.

Example 1: two independent groups (PlantGrowth)

PlantGrowth is one of R’s built-in datasets. Thirty plants were grown under three conditions, a control group and two treatments, and their dried weights recorded. We compare the control group against the first treatment, trt1, ten plants each.

The label shuffling below is design-exact only if these group assignments were randomized under the sharp no-effect null. Otherwise, it relies on the groups being exchangeable under the null; the built-in dataset description does not specify the assignment procedure.

data(PlantGrowth)

ctrl <- PlantGrowth$weight[PlantGrowth$group == "ctrl"]
trt1 <- PlantGrowth$weight[PlantGrowth$group == "trt1"]

length(ctrl)
## [1] 10
length(trt1)
## [1] 10
mean(ctrl)
## [1] 5.032
mean(trt1)
## [1] 4.661
sd(ctrl)
## [1] 0.5830914
sd(trt1)
## [1] 0.7936757

The control plants average 5.032 grams against 4.661 for trt1. (If you would like to double-check the group means outside R, a statistics calculator confirms them in seconds.) Our test statistic is the difference in means, treated minus control:

observed_diff <- mean(trt1) - mean(ctrl)
observed_diff
## [1] -0.371

The treated plants came out 0.371 grams lighter on average. Is that gap unusual under the exchangeable-label null? To find out, pool all 20 weights, shuffle the labels, and see what differences the null model generates. One shuffle, step by step:

weights <- c(ctrl, trt1)
labels <- rep(c("ctrl", "trt1"), each = 10)

set.seed(1)
shuffled <- sample(labels)
mean(weights[shuffled == "trt1"]) - mean(weights[shuffled == "ctrl"])
## [1] -0.349

That shuffle produced a difference of -0.349, nearly as large as the gap we actually observed, from exactly the same 20 numbers with nothing changed but the labels. A single shuffle proves nothing, of course; it simply shows the mechanism. The evidence comes from doing it thousands of times and asking how often chance alone produces a gap like ours:

set.seed(123)
n_perm <- 9999

perm_diffs <- replicate(n_perm, {
  shuffled <- sample(labels)
  mean(weights[shuffled == "trt1"]) - mean(weights[shuffled == "ctrl"])
})

# install.packages("ggplot2")
library(ggplot2)

ggplot(data.frame(diff = perm_diffs), aes(x = diff)) +
  geom_histogram(bins = 40, fill = "grey85", colour = "white") +
  geom_vline(xintercept = observed_diff, colour = "red", linewidth = 1) +
  labs(
    title = "Permutation distribution of the difference in means",
    x = "Difference in means (trt1 - ctrl) after shuffling labels",
    y = "Frequency"
  ) +
  theme_minimal()

The shuffled differences centre on zero under the exchangeable-label null, with most lying within about half a gram of zero. Our observed difference, marked in red, sits well inside that spread: noticeable, but not out in the tail where chance rarely ventures. The p-value counts how much of the distribution is at least as far from zero as what we saw, in either direction:

p_perm <- (sum(abs(perm_diffs) >= abs(observed_diff) - 1e-10) + 1) / (n_perm + 1)
p_perm
## [1] 0.2513

The + 1 in the numerator and denominator treats the observed arrangement as one additional member alongside the random shuffles. This standard Monte Carlo correction prevents a zero estimate. Here it barely matters. The permutation p-value is about 0.251, so differences this large turn up in roughly a quarter of the simulated shuffles. The data do not provide enough evidence to conclude that trt1 changes the weight.

A word on how many shuffles are enough. A simulated p-value is itself an estimate, with Monte Carlo error attached: with 9,999 permutations and a true p-value near 0.25, its standard error is about \(\sqrt{0.25 \times 0.75 / 9999} \approx 0.0043\). Treat the last few thousandths as simulation noise, not as exact digits. If an estimate is close to a threshold you care about, 0.048 against a 0.05 cut-off, say, use more permutations (or enumerate exactly when feasible) and report the number of permutations; do not overinterpret a decision when the Monte Carlo uncertainty spans the cutoff.

Because 20 plants split into two groups of 10 in only \(\binom{20}{10}\) = 184,756 ways, we can even skip the simulation and enumerate every split:

combos <- combn(20, 10)

exact_diffs <- apply(combos, 2, function(idx) {
  mean(weights[idx]) - mean(weights[-idx])
})
# the tiny tolerance catches splits that tie with the observed difference
# but land a hair below it through floating-point rounding
p_exact <- mean(abs(exact_diffs) >= abs(observed_diff) - 1e-10)
p_exact
## [1] 0.247927

The exact p-value, 0.248, matches the simulated one to two decimal places, a good reminder that 9,999 shuffles already pin the answer down tightly.

One more cross-check, this time against a packaged implementation. The coin package runs permutation tests of exactly this kind, so its answer on the same two groups should land in the same place as our hand-rolled shuffle:

# install.packages("coin")
library(coin)

set.seed(123)
pg2 <- droplevels(PlantGrowth[PlantGrowth$group != "trt2", ])

coin_test <- oneway_test(weight ~ group,
  data = pg2,
  distribution = approximate(nresample = 9999)
)

coin_test
## 
## 	Approximative Two-Sample Fisher-Pitman Permutation Test
## 
## data:  weight by group (ctrl, trt1)
## Z = 1.1783, p-value = 0.2449
## alternative hypothesis: true mu is not equal to 0

It does: the packaged p-value, 0.245, agrees with our exact 0.248 to within simulation noise, which is reassuring about both.

How does the classical test compare?

welch <- t.test(trt1, ctrl)
welch
## 
## 	Welch Two Sample t-test
## 
## data:  trt1 and ctrl
## t = -1.1913, df = 16.524, p-value = 0.2504
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -1.0295162  0.2875162
## sample estimates:
## mean of x mean of y 
##     4.661     5.032

The Welch t-test gives a p-value of about 0.25, close to the permutation results here. The two methods rely on different conditions: Welch’s test uses an approximate t reference distribution and allows unequal variances, while the unstudentized permutation test shown here relies on exchangeability. Neither test is automatically more trustworthy when they disagree. In particular, both tests based on means can be sensitive to outliers, and a raw permutation test need not be valid for a null hypothesis of equal means alone when variances differ. The next section compares their behaviour in several specific scenarios.

A small simulation study

Claims about which test to trust are usually traded as folklore. They do not have to be: a simulation can put both tests in situations where the truth is known, because we generated the data ourselves. Here is a small one. Each scenario below generates 10,000 datasets; on every dataset, Welch’s t-test and a permutation test on the difference in means (999 shuffles, the same recipe as Example 1) each get one attempt, and we count how often each rejects at the 5% level. The permutation p-value is itself estimated from those 999 shuffles, so the results describe this Monte Carlo implementation rather than an exact permutation test.

perm_p <- function(x, y, B = 999) {
  pooled <- c(x, y)
  n1 <- length(x)
  N <- length(pooled)
  total <- sum(pooled)
  obs <- mean(y) - mean(x)
  shuffles <- replicate(B, sample(pooled))
  s1 <- colSums(shuffles[seq_len(n1), , drop = FALSE])
  diffs <- (total - s1) / (N - n1) - s1 / n1
  (sum(abs(diffs) >= abs(obs) - 1e-10) + 1) / (B + 1)
}

run_scenario <- function(gen, n_sim = 10000) {
  hits_perm <- 0
  hits_welch <- 0
  for (i in seq_len(n_sim)) {
    d <- gen()
    if (perm_p(d$x, d$y) < 0.05) hits_perm <- hits_perm + 1
    if (t.test(d$x, d$y)$p.value < 0.05) hits_welch <- hits_welch + 1
  }
  c(permutation = hits_perm / n_sim, welch = hits_welch / n_sim)
}

Scenario 1: no difference, skewed data. Both groups are drawn from the same lognormal distribution, a right-skewed shape seen in incomes, weights and waiting times, with 10 observations each. The null hypothesis is true, so a well-behaved test should reject close to 5% of the time.

set.seed(20261118)

gen_same <- function() {
  list(
    x = rlnorm(10, meanlog = 0, sdlog = 1),
    y = rlnorm(10, meanlog = 0, sdlog = 1)
  )
}

sim_same <- run_scenario(gen_same)
sim_same
## permutation       welch 
##      0.0508      0.0272

Scenario 2: a real difference. The same skewed distribution, but the second group is shifted up by 1.5. Now we count power: how often each test finds a difference that is really there.

set.seed(20261119)

gen_shift <- function() {
  list(
    x = rlnorm(10, meanlog = 0, sdlog = 1),
    y = rlnorm(10, meanlog = 0, sdlog = 1) + 1.5
  )
}

sim_shift <- run_scenario(gen_shift)
sim_shift
## permutation       welch 
##      0.5654      0.5221

Scenario 3: no difference in means, but unequal spreads and sizes. This is the case the end of Example 1 warned about. To isolate it, the skew goes away: both groups are normal with the same mean of zero, but the small group (10 observations) has standard deviation 2 and the large group (30 observations) has standard deviation 1. The means are identical. Welch’s test is designed to maintain its level in this setting, but the raw permutation test need not: the labels carry information about spread, even though they carry none about location.

set.seed(20261120)

gen_unequal <- function() {
  list(
    x = rnorm(10, mean = 0, sd = 2),
    y = rnorm(30, mean = 0, sd = 1)
  )
}

sim_unequal <- run_scenario(gen_unequal)
sim_unequal
## permutation       welch 
##      0.1536      0.0507
sim_table <- data.frame(
  scenario = c(
    "1. No difference, skewed, 10 vs 10",
    "2. Real shift of 1.5, skewed, 10 vs 10",
    "3. No mean difference, sd 2 vs 1, 10 vs 30"
  ),
  permutation = c(
    sim_same[["permutation"]], sim_shift[["permutation"]],
    sim_unequal[["permutation"]]
  ),
  welch = c(
    sim_same[["welch"]], sim_shift[["welch"]],
    sim_unequal[["welch"]]
  )
)

knitr::kable(sim_table,
  digits = 3,
  col.names = c("Scenario", "Permutation", "Welch")
)
Scenario Permutation Welch
1. No difference, skewed, 10 vs 10 0.051 0.027
2. Real shift of 1.5, skewed, 10 vs 10 0.565 0.522
3. No mean difference, sd 2 vs 1, 10 vs 30 0.154 0.051

These rejection rates are estimates. With 10,000 simulated datasets, the Monte Carlo standard error of a rate \(\hat p\) is approximately \(\sqrt{\hat p(1-\hat p)/10000}\); near 5%, that is about 0.22 percentage points. Differences of only a few tenths of a percentage point should not be overread.

Read the table from the top. In scenario 1 the permutation test rejects in 5.1% of runs, close to its nominal level and consistent with exchangeability under the common lognormal distribution. Welch’s test rejects in 2.7% of runs, showing conservatism in this particular small-sample, skewed setting. In scenario 2, the estimated power for the chosen shift of 1.5 is 56.5% for the permutation test and 52.2% for Welch’s test. These are scenario-specific estimates; the lower null rejection rate in scenario 1 does not by itself explain or predict the power difference.

Scenario 3 is the reversal. The raw permutation test rejects a true equal-means null in 15.4% of runs, while Welch rejects in 5.1%. Shuffling pools the two spreads, making the null distribution of the unstudentized difference narrower than its sampling distribution under these unequal variances and group sizes; the observed differences therefore look too extreme. Welch estimates each group’s variance separately and adjusts for the unequal sizes. In this simulation it stays close to 5%, but that result is specific to these normal populations and sample sizes.

Taken together, the scenarios show why neither method is universally preferable. Under the common-distribution null in scenario 1, the permutation test is calibrated by exchangeability; in scenario 3, the same unstudentized test is liberal for an equal-means null with unequal variances and sizes. For that latter setting, Welch or a studentized permutation procedure is preferable to the raw shuffle used here.

The difference in medians

Every test so far has shuffled one statistic: the difference in means. Nothing in the recipe ties it to means. Go back to the PlantGrowth weights and swap in a different statistic: the difference in medians, treated minus control, exactly as before:

obs_med <- median(trt1) - median(ctrl)
obs_med
## [1] -0.605
set.seed(123)

perm_meds <- replicate(n_perm, {
  shuffled <- sample(labels)
  median(weights[shuffled == "trt1"]) - median(weights[shuffled == "ctrl"])
})

p_med <- (sum(abs(perm_meds) >= abs(obs_med) - 1e-10) + 1) / (n_perm + 1)
p_med
## [1] 0.1168

The observed difference in medians is -0.605 grams, further from zero than the difference in means, because a median ignores how far the heaviest and lightest plants sit from the middle of their group. Its permutation p-value is 0.117, so this statistic lands further into the simulated tail than the difference in means, without crossing the 5% line. The two statistics are answering slightly different questions about the same twenty plants, so they are under no obligation to agree exactly.

Two things are worth noticing. The machinery did not change at all: the same pooled weights, the same shuffles, the same counting; only the statistic moved. And the question changed with it. A difference in medians describes whether the typical plant in one group outweighs the typical plant in the other, and it is less affected by extreme values than a difference in means. With ten plants per group it is also a coarser instrument: the median of ten values is the average of the middle two, so the shuffled differences take relatively few distinct values and ties are common. This permutation calibration is exact under exchangeability; it does not, in general, test equality of population medians alone when the group distributions otherwise differ. Choose a statistic that matches the question you want answered, and check that the permutation null matches it too.

Example 2: paired data (sleep)

The second classic dataset is smaller and stranger. sleep records the effect of two soporific drugs on 10 patients: each patient has an observation under both drugs, and extra is the increase in hours of sleep over control. Because the same patient appears in both groups, the observations come in pairs, and pairing changes the permutation logic completely.

data(sleep)
head(sleep)
##   extra group ID
## 1   0.7     1  1
## 2  -1.6     1  2
## 3  -0.2     1  3
## 4  -1.2     1  4
## 5  -0.1     1  5
## 6   3.4     1  6
sleep_wide <- reshape(sleep,
  idvar = "ID", timevar = "group",
  direction = "wide"
)

The right way to look at paired data is through the within-patient differences, drug 2 minus drug 1:

d <- sleep_wide$extra.2 - sleep_wide$extra.1
d
##  [1] 1.2 2.4 1.3 1.3 0.0 1.0 1.8 0.8 4.6 1.4
mean(d)
## [1] 1.58

Drug 2 had 1.58 more extra hours of sleep on average in this sample. A sign-flip test needs its own justification: if drug order was randomized within each patient, under the sharp no-effect null the two labels could be swapped with equal probability. Otherwise, the test requires the paired differences to be symmetric around zero under the null. The sleep dataset documentation confirms repeated measurements on the same patients but does not state that drug order was randomized, so we use sign symmetry as the working assumption here. Instead of shuffling labels between patients, we flip the signs of the differences at random, keeping each patient’s pair intact. One random flip:

set.seed(2)
d * sample(c(-1, 1), size = length(d), replace = TRUE)
##  [1] -1.2 -2.4  1.3  1.3  0.0  1.0 -1.8 -0.8 -4.6  1.4

With 10 patients there are \(2^{10}\) = 1,024 possible sign combinations, few enough to enumerate completely without simulation:

signs <- expand.grid(rep(list(c(-1, 1)), length(d)))
all_sums <- apply(signs, 1, function(s) sum(s * d))

observed_sum <- sum(d)
# the same small tolerance as in Example 1, so that a sum which ties with
# the observed one is not lost to floating-point rounding
n_extreme <- sum(abs(all_sums) >= abs(observed_sum) - 1e-10)
p_paired_exact <- n_extreme / length(all_sums)
p_paired_exact
## [1] 0.00390625
n_extreme
## [1] 4

The observed sum of differences is 15.8. Of all 1,024 sign combinations, only 4 produce a sum at least that extreme in absolute value. The p-value is exactly 4/1024, about 0.0039. This is evidence against the sign-symmetry null (or, with randomized order, the sharp no-effect null); it is not an exact test of a zero mean alone without those conditions. One detail is worth noticing: patient 5’s difference is exactly 0, and flipping the sign of zero changes nothing, so every distinct sum in this enumeration appears twice. Zero differences carry no information about which drug is better; some implementations drop them before testing (leaving \(2^9\) combinations here), which is defensible, but keeping them, as above, is exact and simpler to explain.

A picture, and then the simulation version for comparison:

ggplot(data.frame(sum = all_sums), aes(x = sum)) +
  geom_histogram(bins = 40, fill = "grey85", colour = "white") +
  geom_vline(
    xintercept = c(-observed_sum, observed_sum),
    colour = "red", linewidth = 1
  ) +
  labs(
    title = "Exact permutation distribution of signed differences",
    x = "Sum of differences after sign-flipping",
    y = "Frequency"
  ) +
  theme_minimal()

set.seed(123)

n_perm <- 9999
perm_sums <- replicate(n_perm, sum(d * sample(c(-1, 1), length(d), replace = TRUE)))

p_paired_sim <- (sum(abs(perm_sums) >= abs(observed_sum) - 1e-10) + 1) / (n_perm + 1)
p_paired_sim
## [1] 0.0043

The simulation lands at about 0.0043, right next to the exact 0.0039, as it should, since it is sampling from the very distribution we enumerated. And the paired t-test?

paired_tt <- t.test(sleep_wide$extra.2, sleep_wide$extra.1,
  paired = TRUE
)

paired_tt
## 
## 	Paired t-test
## 
## data:  sleep_wide$extra.2 and sleep_wide$extra.1
## t = 4.0621, df = 9, p-value = 0.002833
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
##  0.7001142 2.4598858
## sample estimates:
## mean difference 
##            1.58

The paired t-test estimates a mean difference of 1.58 hours and gives a p-value of about 0.0028. Its small-sample reference is exact if the paired differences are independent and normally distributed; with only 10 patients, that assumption is difficult to assess. Both procedures point to a positive difference here, but the paired t-test tests a zero mean under its model, whereas the sign-flip test uses the symmetry or randomization null described above. Patient-to-patient variability is removed by taking within-patient differences, and both analyses preserve that pairing.

When to use permutation tests, and when not to

Permutation tests earn their place when:

  • The sample is small and normality is uncertain. Permutation methods avoid a normal reference distribution, but small sample size alone does not make a shuffle valid and can limit power and p-value resolution.
  • The data are skewed or heavy-tailed, and a normal reference distribution is questionable. Exchangeability can still justify permutation calibration without normality; choose a statistic that answers the question of interest. For ordinal data, a rank-based test such as the Wilcoxon test may be more appropriate, but it does not necessarily test the same hypothesis as a test of means.
  • Your statistic has no convenient classical test. You can permute a difference in medians, a trimmed mean, a ratio, or another summary, but the statistic must be paired with a defensible assignment or exchangeability null.
  • The data come from a randomized experiment. Permuting assignments allowed by the design gives an exact test under the sharp null of no treatment effect on any unit.

And their limits deserve equal honesty:

  • Exchangeability is an assumption, not a magic word. If observations are dependent, as with repeated measures shuffled as if independent, clustered data or time series, naive shuffling breaks the structure and invalidates the reference distribution. Preserve the design (for example, flip signs within pairs only when justified), or use a different method.
  • Permutation does not cure confounding. In observational data, unrestricted label shuffling is generally not justified unless exchangeability under the null is defensible, possibly conditional on covariates. Even a valid test of association does not establish that the grouping caused the outcome.
  • A raw permutation test on means is not generally an exact test of equal means alone. Its usual finite-sample justification is exchangeability (or a sharp randomization null). If spreads differ, especially with unbalanced group sizes, a significant result may reflect differences beyond the means; a studentized procedure or Welch’s test may be more suitable.
  • Tiny samples have a floor. For a two-sided sign-flip test with \(n\) pairs, the theoretical minimum is \(2/2^n\), reached only when the two extreme sign patterns are the only ones as extreme as observed; ties or zero differences can make the minimum larger. With 10 pairs the floor is 2/1024 ≈ 0.00195. With 5 pairs it is 2/32 = 0.0625, so a p-value below 0.05 is impossible. Know the resolution before you start.
  • Huge enumerations need simulation. When exact enumeration is impractical, sample 9,999 or 99,999 shuffles, use the (+1)/(B+1) correction, and report the number of shuffles and Monte Carlo uncertainty. Set a seed for reproducibility within the same random-number-generator setup.

Conclusion

A permutation test builds a null reference from the assignments or sign changes justified by the design or exchangeability assumptions, then asks where the observed statistic falls inside it. On PlantGrowth it agreed with Welch’s t-test; the data do not provide enough evidence to conclude that trt1 changed the weight, but that is not evidence that the effect is zero. On sleep, the exact sign-flip calculation found only four of 1,024 assignments as extreme as the one observed. This is evidence against the sign-symmetry null (or a randomized sharp no-effect null), not an exact test of zero mean alone. Both examples set seeds for their Monte Carlo steps; results are reproducible with the same R and random-number-generator setup. For a new dataset, first check whether the design and null justify the shuffle, then choose a statistic that answers the scientific question.

As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.

To leave a comment for the author, please follow the link and comment on their blog: R on Stats and R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)