McNemar Test: Read the Two Discordant Pair Counts
2026-11-07

McNemar Test: Read the Two Discordant Pair Counts

The McNemar test examines two binary results recorded for each of the same set of items. A robotics example is comparing two inspection classifiers on identical labeled images. For each image, both versions can be correct, both can be wrong, or only one can be correct. The test focuses on how often the two types of disagreement occur.

The unit in the table is a matched pair

Put the outcome from classifier A on the rows and the outcome from classifier B on the columns. Each shared image contributes one count to the cell for its pair of outcomes. If A is correct and B is wrong, it goes into a different cell from an image where A is wrong and B is correct. An image on which both agree is still one pair, even though two results were recorded.

This construction requires a match between the inputs given to the two versions. Two independent test collections may have the same number of images and still provide no pairings. A row in an analysis file can carry both results alongside one image identifier so that the pairing is visible before any totals are taken.

Each pair should also represent the intended observational unit. A hundred near-identical frames of one object have a different dependence structure from a hundred independently sampled inspection cases. Matching the versions by frame accounts for their shared inputs within each pair. It does not make the different pairs independent of one another.

Agreement contributes equally to both marginal totals

Suppose 55 images are correct for both versions and 25 are wrong for both. Of the remaining 20, A alone is correct on 15 and B alone on five. A is correct on 70 images and B on 60. The 55 joint successes add the same number to both totals. Joint failures add to neither. The difference is therefore the 15 wins for A minus the five wins for B.

A pair in which the two outcomes differ is called discordant. The two discordant cells answer the question about the direction of change. A large number of images that both classifiers handle the same way can be important for overall performance. It leaves the difference between their paired success counts unchanged.

Separate totals would reveal the ten-image difference, but they would hide how many inputs changed outcome. The same difference could come from ten discordant pairs in one direction and zero in the other, or from the fifteen and five in this example. Those tables provide different information for the exact calculation.

Two-by-two table with 55 both correct, 25 both wrong, and discordant counts 15 and 5.
The two discordant counts record the directions of disagreement. Source: Zhilianhai editorial desk, original AI-assisted concept; no real product, event or measured data.

The exact calculation conditions on disagreement

For a binary pair, the null hypothesis of equal marginal success probabilities corresponds to equal probabilities for the two directions of disagreement. Conditional on having a discordant pair, either direction has probability one half under that null. With independent pairs, the count in one direction among the discordant pairs then follows a binomial distribution with probability one half.

In the example there are 20 discordant pairs. The smaller direction count is five. The exact two-sided calculation implemented in the statsmodels source doubles the probability of five or fewer outcomes in a binomial distribution with 20 trials and probability one half, with the result capped at one. Here the two tails together give about 0.0414. That value follows from the counts in the example rather than from the number of agreements in the diagonal cells.

Binomial mass from zero to twenty, with counts five or fewer and fifteen or more highlighted.
The highlighted probabilities sum to about 0.0414 for this example. Source: Zhilianhai editorial desk, original AI-assisted concept; no real product, event or measured data.

Software options change how the reference is calculated

The statsmodels implementation also offers an approximate version using a chi-squared distribution. Its continuity correction applies to that approximate version. The R function documented as mcnemar.test performs a chi-squared test and uses its correction option for a two-by-two table. Two reports can therefore name the same test but use different methods to obtain their reported probabilities.

Keep the method with the table. In the statsmodels exact branch, the reported statistic is the smaller discordant count. In the approximate branch, it is a chi-squared statistic. Comparing the statistic fields as if they used the same scale would obscure which calculation each report performed.

The result describes a paired comparison

The null addresses the balance of opposite outcomes for these pairs. It does not identify which image features explain the imbalance. In the example, the observed difference is ten correct results out of one hundred paired images, while the exact probability summarizes how unusual that discordant split would be under the null. Recording both gives the reader the size and direction of the observed difference as well as the test result.

That record also makes the meaning of the comparison recoverable after a table is copied. Reversing the row and column labels swaps the direction counts and reverses the signed performance difference. In the two-sided exact test it leaves the p-value unchanged.

The statsmodels 0.14.5 implementation shows the exact binomial and approximate branches. The R stats documentation states the symmetry hypothesis and continuity correction behavior. Browse the statistics reference section and technical articles for related methods.

18028484626 CONTACT US

公司:智联海(惠州)科技有限公司

地址:惠州仲恺高新区陈江街道白云山工业大道 5 号六楼 601

Q Q:18028484626

Copyright © 2002-2026

粤xxx

咨询 在线留言 电话:18028484626
微信 微信扫码添加我