← All topics

Step 3 · Methods · Topic 12 of 17 · 8 min

Categorical data: contingency tables

How to compare proportions and categories: χ², Fisher’s exact test, odds ratios and paired data.

In short#

  • An r × c table contains integer frequencies of independent observations. H₀: row and column characteristics are independent.
  • Pearson’s test: χ² = Σ(O − E)² / E, df = (r − 1)(c − 1). With small expected frequencies, the χ² approximation may be inaccurate.
  • Fisher’s exact test for a 2 × 2 table does not use the χ² approximation. Yates’ correction accounts for continuity when approximating χ² in a 2 × 2 table.
  • Case-control data allow estimation of an odds ratio. A risk ratio requires cohort sampling with denominators that support group-specific risks.
  • Exact McNemar compares two binary outcomes across independent pairs and uses only pairs with different outcomes. Measurements within each pair are linked; the pairs themselves must be independent.

In plain words#

Sometimes the outcome is a category, not a number: worked or did not, red or blue. Then you count how many observations fall into each cell of a table.

The χ² test compares observed frequencies with those expected if the categories were independent. For the same degrees of freedom, a larger χ² statistic means a smaller p-value.

For a sparse 2 × 2 table, Fisher’s exact test does not rely on the χ² approximation.

In a paired 2 × 2 table, the diagonal counts are pairs with no change. McNemar conditions on the two off-diagonal counts and tests whether the two directions of change are equally frequent. Its odds ratio is the forward count divided by the reverse count.

In Linza#

  1. Open the practice: the first row has 18 events among 50 observations, and the second has 10 among 50. These are counts, not percentages. Check the row and column labels.
  2. For a 2 × 2 table, explicitly set the exposed row and event column. They determine the orientation of the odds and risk ratios.
  3. Compare the proportions 36% and 20%, then read the overall test and effect interval. As a check, swap the exposed and reference rows: the ratios should become reciprocals.
  4. For paired binary measurements, select Matched pairs. Exact McNemar uses the discordant pairs and reports a matched odds ratio. A large p-value does not prove that the outcomes are equal.
Try it in Linza Opens in a new tab with synthetic data. Work in the original tab remains unchanged.

Common mistakes#

  • Entering percentages instead of frequencies.
  • Calculating a risk ratio in a case-control study.

Check yourself#

In a 2 × 2 table one expected frequency is 2. What is sensible?

Optional. Your answer is not saved.

Sources#

  • Pearson K. (1900) On the criterion that a given system of deviations from the probable in the case of a correlated system of variables is such that it can be reasonably supposed to have arisen from random sampling. Philosophical Magazine, Series 5, 50. Open publication Opens in a new tab
  • Fisher R.A. (1922) On the interpretation of χ² from contingency tables, and the calculation of P. Journal of the Royal Statistical Society, 85(1). Search for publication Opens in a new tab
  • Agresti A. (2013) Categorical Data Analysis. 3rd ed. Wiley. Search for publication Opens in a new tab
  • McNemar Q. (1947) Note on the sampling error of the difference between correlated proportions or percentages. Psychometrika, 12. Search for publication Opens in a new tab

References are for further reading. A method’s suitability depends on the study design and its assumptions.