Skip to main content

9  Fisher’s exact test for small tables

The chi-square test of Chapter 8 compares observed and expected counts in a table. It uses a normal approximation, and the approximation fails when an expected count is less than 5. Fisher’s exact test uses no approximation. It counts all the tables that the design can give. Fisher described it in 1935 with a test of a colleague who said that she could taste the difference in a cup of tea (Chapter 1).

9.1 The problem

Panorama of the Rothamsted Research site: modern buildings on two sides of a lawn.

Rothamsted Experimental Station (now Rothamsted Research) in Harpenden, England, where Fisher worked from 1919 to 1933. Photo: HMN, CC BY-SA 4.0, Wikimedia Commons.

Black and white photograph of Muriel Bristol, seated. Muriel Bristol

Portrait of the young Ronald Fisher in a suit and round glasses. Ronald Fisher, 1913
Public domain, Wikimedia Commons

In the 1920s, Fisher offered a cup of tea to the algae biologist Muriel Bristol at the afternoon tea of Rothamsted. She refused it, because he poured the milk in after the tea. She said that she could tell if the milk or the tea went into the cup first. Fisher prepared 8 cups in random order: 4 with milk first and 4 with tea first. She knew that there were 4 of each, and she had to name the 4 milk-first cups.

tea <- matrix(c(4, 0, 0, 4), nrow = 2,
              dimnames = list(poured  = c("milk first", "tea first"),
                              guessed = c("milk first", "tea first")))
tea
            guessed
poured       milk first tea first
  milk first          4         0
  tea first           0         4

Milk poured from a jug into a cup of black tea, with clouds of milk in the tea. Milk into tea. Muriel Bristol said that the order of the two changes the taste.

The usual story says that she named all 4 milk-first cups correctly. Fisher never reported her result (Chapter 1), so we use the table of the story.

Can she tell the difference, or was she lucky?

Under the null hypothesis, each expected count is 2. All four cells are below 5, so we cannot use the chi-square test.

9.2 Intuitive solution

9.2.1 List all the choices

Suppose that she cannot taste the difference. Then her 4 “milk first” cups are a random set of 4 of the 8 cups. There are \(\binom{8}{4} =\) 70 such sets, and each set has the same probability. We list all the sets and count the correct cups in each set.

milk_first <- 1:4                    # cups 1 to 4 had the milk first
choices <- combn(8, 4)               # one column for each set of 4 cups
correct <- apply(choices, 2, \(cups) sum(cups %in% milk_first))
table(correct)
correct
 0  1  2  3  4 
 1 16 36 16  1 
Show code
n_choices <- ncol(choices)
p_all4 <- mean(correct >= 4)
p_3 <- mean(correct >= 3)
n_3plus <- sum(correct >= 3)

Of the 70 sets, 1 has all 4 cups correct. If she guesses, the probability of a perfect score is 1/70 = 0.014. This is the one-sided p-value. It is less than 0.05, so we reject the hypothesis that she guessed.

Suppose that she named only 3 cups correctly. 17 of the 70 sets give 3 or more correct cups. The p-value is then 17/70 = 0.24, and this result gives no evidence of a skill.

Show code
tibble(correct = correct) |>
  count(correct) |>
  mutate(observed = correct == 4) |>
  ggplot(aes(factor(correct), n, fill = observed)) +
  geom_col(width = 0.7) +
  geom_text(aes(label = n), vjust = -0.4) +
  scale_fill_manual(values = c(`FALSE` = "steelblue", `TRUE` = "firebrick"), guide = "none") +
  labs(x = "cups named correctly", y = "number of sets")
Figure 9.1: The number of correct cups for each of the 70 sets of 4 cups that she can name. Under the null hypothesis, all 70 sets have the same probability. The red bar is her result: all 4 correct, in 1 set of 70.

This is the shuffle idea of Chapter 6. There, we shuffled the group labels and recalculated the rank sum. Here, we shuffle her “milk first” labels over the 8 cups.

9.2.2 Count the sets without a list

We can count the bars of Figure 9.1 without the list. We call the milk-first cups M1 to M4 and the tea-first cups T1 to T4. A set with exactly 3 correct cups has two parts: 3 of the 4 M cups, and 1 of the 4 T cups.

cups <- c(paste0("M", 1:4), paste0("T", 1:4))
three <- choices[, correct == 3]
matrix(apply(three, 2, \(i) paste(cups[i], collapse = " ")), nrow = 4)
     [,1]          [,2]          [,3]          [,4]         
[1,] "M1 M2 M3 T1" "M1 M2 M4 T1" "M1 M3 M4 T1" "M2 M3 M4 T1"
[2,] "M1 M2 M3 T2" "M1 M2 M4 T2" "M1 M3 M4 T2" "M2 M3 M4 T2"
[3,] "M1 M2 M3 T3" "M1 M2 M4 T3" "M1 M3 M4 T3" "M2 M3 M4 T3"
[4,] "M1 M2 M3 T4" "M1 M2 M4 T4" "M1 M3 M4 T4" "M2 M3 M4 T4"

There are \(\binom{4}{3} =\) 4 ways to choose the 3 M cups. Each one leaves out one M cup. For each of these choices, there are \(\binom{4}{1} =\) 4 ways to choose the 1 T cup. Each column of the output above has the same 3 M cups with each of the 4 T cups. This gives \(\binom{4}{3} \times \binom{4}{1} =\) 4 \(\times\) 4 \(=\) 16 sets.

The same rule gives every bar. A set with \(k\) correct cups has \(k\) of the 4 M cups and \(4 - k\) of the 4 T cups:

\[ \text{number of sets with } k \text{ correct} = \binom{4}{k} \times \binom{4}{4 - k}. \]

k <- 0:4
tibble(k, M_ways = choose(4, k), T_ways = choose(4, 4 - k),
       sets = M_ways * T_ways, probability = sets / choose(8, 4))
# A tibble: 5 × 5
      k M_ways T_ways  sets probability
  <int>  <dbl>  <dbl> <dbl>       <dbl>
1     0      1      1     1      0.0143
2     1      4      4    16      0.229 
3     2      6      6    36      0.514 
4     3      4      4    16      0.229 
5     4      1      1     1      0.0143

For 2 correct cups, there are 6 \(\times\) 6 \(=\) 36 sets. The column sets agrees with the list of 70 sets in Figure 9.1. Each probability is the number of sets divided by 70. This is the hypergeometric distribution of the formal solution below.

9.2.3 The number of cups

The design sets the smallest p-value that the test can give. With 6 cups, 3 of each kind, there are \(\binom{6}{3} =\) 20 sets. A perfect score then has the probability 1/20 = 0.05. This value does not go below 0.05. With 8 cups, the smallest p-value is 0.014. With equal numbers of each kind, 8 is the smallest number of cups that can give a p-value below 0.05.

tibble(cups = c(4, 6, 8, 10), smallest_p = 1 / choose(cups, cups / 2))
# A tibble: 4 × 2
   cups smallest_p
  <dbl>      <dbl>
1     4    0.167  
2     6    0.05   
3     8    0.0143 
4    10    0.00397

9.3 Formal solution

9.3.1 The probability of one table

We write a 2 × 2 table with the four counts \(a\), \(b\), \(c\) and \(d\).

Guessed milk first Guessed tea first Total
Milk first \(a\) \(b\) \(a + b\)
Tea first \(c\) \(d\) \(c + d\)
Total \(a + c\) \(b + d\) \(n\)

The design fixes all the margins. There are 4 cups of each kind, and she must name 4 cups as “milk first”. When the margins are fixed, the count \(a\) sets the full table. Under the null hypothesis of no association, the probability of the table is

\[ P(a, b, c, d) = \frac{(a+b)!\,(c+d)!\,(a+c)!\,(b+d)!}{n!\,a!\,b!\,c!\,d!}. \]

The formula is the count of the intuitive solution in a different form: \(\binom{a+b}{a}\binom{c+d}{c} / \binom{n}{a+c}\). For 3 correct cups, \(a = 3\) and \(c = 1\), and the formula gives \(\binom{4}{3}\binom{4}{1} / \binom{8}{4} = 16/70\). This is the hypergeometric distribution. It gives the probability of \(a\) successes in \(a + c\) draws without replacement from \(n\) objects, of which \(a + b\) are successes. In R, the function is dhyper(a, a + b, c + d, a + c).

For the tea, \(a = d = 4\) and \(b = c = 0\):

f <- factorial
p_table <- f(4) * f(4) * f(4) * f(4) / (f(8) * f(4) * f(0) * f(0) * f(4))
c(formula = p_table, dhyper = dhyper(4, 4, 4, 4), enumeration = p_all4)
    formula      dhyper enumeration 
 0.01428571  0.01428571  0.01428571 

The formula, dhyper() and the list of 70 sets give the same value.

9.3.2 The p-value

The one-sided p-value is the sum of the probabilities of the observed table and of all tables that are more extreme in the same direction. The two-sided p-value in R is the sum of the probabilities of all tables that have a probability no larger than the observed table.

tibble(a = 0:4, probability = dhyper(a, 4, 4, 4))
# A tibble: 5 × 2
      a probability
  <int>       <dbl>
1     0      0.0143
2     1      0.229 
3     2      0.514 
4     3      0.229 
5     4      0.0143
Show code
probs <- dhyper(0:4, 4, 4, 4)   # probability of a = 0, ..., 4
p_two <- fisher.test(tea)$p.value

For the tea, the tables with \(a = 0\) and \(a = 4\) both have the probability 0.0143. The two-sided p-value is 0.0286. Her claim is a skill in one direction, so the one-sided test answers her question.

9.3.3 The test in R

ft <- fisher.test(tea, alternative = "greater")
ft

    Fisher's Exact Test for Count Data

data:  tea
p-value = 0.01429
alternative hypothesis: true odds ratio is greater than 1
95 percent confidence interval:
 2.003768      Inf
sample estimates:
odds ratio 
       Inf 
  • p-value: 0.0143. This is 1/70, the value from our list of sets.
  • alternative hypothesis: true odds ratio is greater than 1: we did a one-sided test. An odds ratio above 1 means that her “milk first” names agree with the cups.
  • 95 percent confidence interval: 2 to infinity. The data cannot give an upper limit, because a table with two zero cells has an infinite odds ratio.
  • odds ratio: Inf. R gives the conditional maximum likelihood estimate. It is infinite for the same reason as the upper limit.

9.3.4 A clinical example: ECMO for newborns

Newborns with persistent pulmonary hypertension have a high risk of death. Extracorporeal membrane oxygenation (ECMO) oxygenates the blood outside the body. A trial at Boston compared ECMO with conventional medical therapy (CMT) (O’Rourke and others, Pediatrics, 1989). In the randomised phase, 4 of 10 infants on CMT died, and all 9 infants on ECMO survived. Ware discussed the statistics of this trial in Statistical Science (1989).

ecmo <- matrix(c(9, 6, 0, 4), nrow = 2,
               dimnames = list(therapy = c("ECMO", "CMT"),
                               outcome = c("survived", "died")))
ecmo
       outcome
therapy survived died
   ECMO        9    0
   CMT         6    4

Does ECMO increase the survival of these newborns?

cs_ecmo <- suppressWarnings(chisq.test(ecmo, correct = FALSE))
ecmo_expected <- cs_ecmo$expected
ecmo_expected
       outcome
therapy survived     died
   ECMO 7.105263 1.894737
   CMT  7.894737 2.105263
ft_ecmo <- fisher.test(ecmo)
ft_ecmo_one <- fisher.test(ecmo, alternative = "greater")
c(fisher_two_sided = ft_ecmo$p.value, fisher_one_sided = ft_ecmo_one$p.value,
  chisq = cs_ecmo$p.value)
fisher_two_sided fisher_one_sided            chisq 
      0.08668731       0.05417957       0.03272707 

The expected number of deaths is 1.89 with ECMO and 2.11 with CMT. Both are below 5. For this table, chisq.test() gives the message “Chi-squared approximation may be incorrect”. The chi-square p-value is 0.033, which is below 0.05.

Fisher’s test gives a two-sided p-value of 0.087 and a one-sided p-value of 0.054. Both are above 0.05. Without the Yates correction, the chi-square approximation makes the evidence look stronger than the exact test does. With the default correction, chisq.test() gives p = 0.116. The two versions of the approximation disagree, and neither is reliable here. With 19 infants, the exact test is the correct choice.

9.4 A conservative test

Fisher’s test is conservative. The test uses only the tables with the observed margins, and there are few such tables. The p-value can take only a small number of values. For the tea, the possible one-sided p-values are 1, 0.986, 0.757, 0.243, 0.014. A test at the 5% level then rejects a true null hypothesis in less than 5% of experiments.

pvals <- rev(cumsum(rev(probs)))       # one-sided p-value for a = 0, ..., 4
size <- sum(probs[pvals <= 0.05])      # probability of p <= 0.05 under the null
size
[1] 0.01428571

With 8 cups, the true probability of a false rejection is 0.014, below 0.05. In a clinical trial, a conservative test has less power to find a real effect. One correction is the mid-p-value. It counts only half the probability of the observed table. For the tea, the one-sided mid-p-value is 0.0071.

The test also assumes that both margins are fixed. In the tea experiment, the design fixes them. In the ECMO trial, nobody fixed the number of deaths in advance. Fisher’s test is common for such tables, and the condition on both margins then makes it more conservative.

For a large table, the exact calculation is slow, because the number of possible tables is large. The chi-square test of Chapter 8 then gives almost the same p-value.

9.5 Summary

  • Fisher’s exact test lists all the tables with the observed margins and adds the probabilities of the tables that are at least as extreme.
  • The probability of one table comes from the hypergeometric distribution. The R function is fisher.test().
  • Fisher’s test is the correct test for a 2 × 2 table with an expected count below 5. The chi-square p-value is then not accurate, in either direction.
  • The test is conservative because the p-value can take only a few values. The mid-p-value is one correction.