Two small studies both end in a 2 by 2 table. In the first, all four people in group A have an outcome and none of the four people in group B do, so every expected count from the margins is only 2 and a chi-square approximation is doubtful. In the second, 20 people answer the same yes or no question before and after a change, and their 40 answers are not 40 independent observations. This lesson builds Fisher’s exact test for the first design and McNemar’s test for the second, and it shows why the design, not the shape of the table, decides which test applies.
Key terms
Fixed margins and conditional inference
Definition The margins of a 2 by 2 table are its row totals and column totals. Suppose the rows are two groups of independent units with and members, , the columns record whether an outcome occurred, and units in total have the outcome. Fixing the margins means comparing the observed table only with the tables that have the same row totals and the same column totals. With the margins fixed, the count of outcomes in group A determines the other three cells, and its possible values are the integers with
Conditional inference bases a test or an interval on the distribution of the data given the observed value of some statistic, here the margins. Suppose each unit in group A has the outcome with probability and each unit in group B with probability , all independently, and the null hypothesis is . Under , given the margins,
which is the hypergeometric distribution with population size , marked items and sample size . The unknown common probability cancels, so this null distribution contains no unknown parameter.
In plain words The margins say how many people were in each group and how many had the outcome overall. Conditioning on them sets those numbers aside and asks only how the outcomes were split between the groups. If the groups do not differ, then once we know that of the people had the outcome, every set of people is equally likely to be that set, so group A is like a random sample of people from the . In a two-group study the group sizes are usually fixed by the design while the outcome total is random, and the test conditions on that total anyway. That choice is what makes the inference conditional.
Example In the first study , and , so can be any integer from to . There are sets of four people that could carry the outcome, all equally likely under , and the set gives in of them.
| Count of sets | |||||
The probabilities add to . The observed table has , and its conditional probability under is .
Fisher’s exact test
Definition Fisher’s exact test applies to a table of counts from independent units, and this lesson treats the 2 by 2 case. Its null hypothesis depends on the design. For two independent groups it states that both groups have the same outcome probability, and for a single sample classified in two ways it states that the two classifications are independent. Under either null, the count has the hypergeometric distribution of the previous card given the margins. In a randomized experiment that assigns of the units to group A completely at random, the same distribution follows from the null hypothesis that the treatment changes no unit’s outcome, and then the randomness comes from the assignment. The p-value is the conditional probability under of the tables with the observed margins that are at least as extreme as the observed table. Against the one-sided alternative that group A has the higher outcome probability it is
and the next card gives the two-sided rules. The word exact means that the null distribution is computed exactly instead of being approximated by a large-sample distribution. A test that rejects when then has a probability of a type I error of at most , and because the distribution is discrete this probability is often below .
In plain words The test asks how surprising the observed split of outcomes would be if group membership had nothing to do with the outcome. Exact describes the arithmetic, not the study. The test still needs independent units, a design that matches the stated null and a stated rule for two-sided p-values, and it cannot repair a biased sample. It is not the only exact method either. Unconditional exact tests such as Barnard’s test do not condition on the outcome total and can give different p-values for the same table.
Example In the first study the observed count is . The one-sided p-value is . The two-sided p-value also counts the equally improbable table in the other direction, giving . For comparison, every expected count is , the Pearson chi-square statistic equals , and the approximation gives a p-value of about , roughly six times smaller than the exact value. The sample odds ratio has a zero denominator and is infinite. The finite p-value measures how surprising the table is under , and it does not say how large the difference between the groups is.
Two-sided exact p-value
Definition For a discrete null distribution there is more than one reasonable two-sided p-value, so a report names the rule it uses. Write under . The probability-ordering rule, a widely used rule for Fisher’s test, adds the null probabilities of every table with the same margins that is no more probable than the observed table,
The doubling rule takes twice the smaller one-sided tail probability and caps the result at ,
with both tails computed from the same conditional null distribution. When that distribution is symmetric, as in the first study, the two rules give the same value. In general they can differ. Whether to use a one-sided or a two-sided p-value is decided before looking at the data.
In plain words Two-sided means that a lopsided split in either direction counts as evidence. The rules differ in how they decide which tables on the other side are at least as extreme. The probability-ordering rule uses the null probability itself as the measure of extremeness, while the doubling rule treats the other side as if it carried as much probability as the observed tail. Picking whichever rule gives the smaller p-value after seeing the data can make the reported p-value too small.
Example All three people in group A have the outcome and none of the four people in group B do, so , , and . There are sets of three people.
Only the observed table has probability at most . The most lopsided table in the other direction, with all three outcomes in group B, has probability , so the probability-ordering rule adds nothing from that side and gives . The doubling rule gives . At the level the two rules reach different conclusions for the same table, which is why the rule is fixed in advance and reported.
Paired binary data and discordant pairs
Definition Paired binary data consist of independent units, each giving two binary outcomes that belong together, such as one person’s answer before and after a change or the outcomes of the two members of a matched pair. The data are summarized by classifying each pair by both outcomes, so the table counts pairs, not single answers.
| Second yes | Second no | Total | |
|---|---|---|---|
| First yes | |||
| First no | |||
| Total |
A pair is concordant if its two outcomes agree, as in cells and , and discordant if they differ, as in cells and . The sample proportions of yes at the two measurements are and , so
In plain words The unit is the pair. Twenty people measured twice give twenty independent pairs, not forty independent observations. The two proportions come from the same people, so the design does not provide two independent samples, and the standard error for two independent proportions ignores the pairing. A concordant pair adds the same amount to both proportions, so the concordant cells drop out of the difference, although they still count in .
Example In the second study the rows are the answers before the change and the columns the answers after it.
| After yes | After no | Total | |
|---|---|---|---|
| Before yes | |||
| Before no | |||
| Total |
The independent units are the 20 people. There are concordant pairs and discordant pairs. The yes rate is before and after, and the difference comes entirely from the discordant pairs.
Marginal homogeneity
Definition For paired binary data, let , , and be the population probabilities that a pair falls in each cell, where the first index refers to the first measurement, the second index to the second measurement, and index 1 means yes. Marginal homogeneity means that the probability of yes is the same at both measurements,
In a 2 by 2 table this holds exactly when , so the two kinds of discordant pair are equally likely. It is a statement about the margins and is a different property from independence of the two outcomes within a pair.
In plain words Marginal homogeneity means no net change. Some units may switch from no to yes and others from yes to no, but in the population the two kinds of switch are equally common, so the overall yes rate stays the same. Paired tables often show association, because a person who says yes once often says yes again, and that association is a separate question.
Example A population with , , and has a yes rate of at both measurements, so marginal homogeneity holds. Its outcomes are strongly associated, since while . A population with , , and is the opposite case. Each cell equals the product of its margins, so the two outcomes are independent, yet the yes rate rises from to . For the 20 people above, Fisher’s exact test applied to the paired table asks the association question and gives a two-sided p-value of about under the probability-ordering rule. That test does not address whether the population yes rate changed, which is the marginal homogeneity question. Fisher’s test conditions on both margins, so it takes the observed yes rates and as given.
McNemar test
Definition McNemar’s test uses independent pairs, each falling in the four cells with the same probabilities , to test , which is marginal homogeneity. Let and be the random counts in the yes then no cell and the no then yes cell, with observed values and , and let be the number of discordant pairs. Given , each discordant pair is independently of the yes then no kind with probability , which equals under . So under
and the exact two-sided p-value is
with the probability computed from this binomial distribution. Because this distribution is symmetric, the doubling rule and the probability-ordering rule give the same value. The concordant counts and do not enter, and with no discordant pairs the p-value is . A large-sample version compares
with the distribution, and some versions first subtract from as a continuity correction. That approximation needs many discordant pairs.
In plain words Only the units that changed carry information about the direction of change. Under no net change, each unit that changed was equally likely to change either way, like one fair coin toss per changer, and the test asks whether the split of the changers is too lopsided for fair coins. The units that did not change affect the size of the difference in proportions but not the test.
Example For the 20 people, and , so . Under the count is given the eight discordant pairs, and , so
At a 5 percent level chosen in advance, the test does not reject marginal homogeneity. That is not evidence of no change. The observed yes rate rose by , and only eight pairs carry information about the direction. The large-sample statistic would give a p-value of about , and the continuity-corrected value gives about . With eight discordant pairs the approximations disagree with each other and with the exact value, so the exact value is the one to report.
Two changes show what the test responds to. If the eight discordant pairs split 2 and 6 instead of 1 and 7, the exact p-value becomes . If instead the original table gains 100 more people who say yes both times, the exact p-value stays , while the difference in yes rates falls to .
Check yourself
- All three people in group A have an outcome and none of the three people in group B do. Find the conditional distribution of the count in group A given the margins, the one-sided and two-sided Fisher p-values, and the smallest two-sided p-value that any table with these margins could give.
Show the answer
The margins are and , so and there are equally likely sets of three people to carry the outcome under . The count arises in of them, which gives , , and sets for . The conditional probabilities are , , and .
The observed table has . The one-sided p-value is . The tables no more probable than the observed one are and , so the two-sided p-value is , and doubling gives the same value because the distribution is symmetric. The observed table is already the most extreme one, so no table with these margins gives a two-sided p-value below . A two-sided test at the level can never reject with these margins, and a nonsignificant result here says very little about whether the groups differ.
- Thirty people answer a yes or no question before and after a training session. Twelve say yes both times, 6 say no both times, 10 change from no to yes and 2 change from yes to no. Which counts enter the exact McNemar test? Find the two yes rates, their difference and the exact two-sided p-value.
Show the answer
The 30 people are the independent units. Only the discordant pairs enter the test. The yes rate is before and after, and the difference is . Under the count of yes then no pairs is given the 12 discordant pairs, and the smaller count is , so
At a 5 percent level chosen in advance, the test rejects marginal homogeneity. The 12 people who said yes both times and the 6 who said no both times change the yes rates and , but not the p-value. The test is about whether the population yes rate changed. A before and after design without a comparison group cannot separate the effect of the training from anything else that happened between the two answers.
- A report on 20 people gives only the before and after yes counts, 9 and 15, and analyzes them with Fisher’s exact test as two independent groups of 20. What is wrong, and can McNemar’s test be computed from these counts?
Show the answer
Each of the 20 people appears in both groups, so the 40 answers are 20 pairs. Fisher’s hypergeometric distribution assumes 40 independent units, and the two-sided p-value of about that this analysis produces belongs to a design that did not happen.
McNemar’s test needs the discordant counts, and the yes counts do not determine them. With , and , the counts satisfy , and every value of from to fits.
| Exact |
The same yes counts are compatible with exact p-values from about to about . The analysis needs the paired table, which classifies each person by both answers.