Counts and Chi-Squared

When the data are counts in boxes. Pearson’s statistic built from dice, tables that test independence, and Mendel’s peas, which fit his theory a little too well

Counts and Chi-Squared

A great deal of data is not measurements but counts: how many rolls came up six, how many children caught polio, how many peas were wrinkled. In 1900 Karl Pearson found one number that says how far a set of counts lies from what a theory predicts, and one curve that says how large that number gets by chance alone.

Is the Die Loaded?

The die is slightly loaded towards six, and the statistician does not know it. Press Roll again a few times at 600 rolls, then slide the number of rolls down to 60 and up to 6,000 and watch the amber stack against the sky histogram of fair dice.

The die’s true probabilities (hidden from the statistician). Moving one rescales the other five so that they still sum to 1.

9.46
χ², the six contributions added up
5
degrees of freedom: 6 faces − 1
0.092
p-value from the chi-squared curve
…
p-value by simulation: 0 of 0 fair dice did as badly
100
expected count per face (the curve is trusted from about 5 up)
Missed. The die really is loaded (the dashed white marks differ from the sky line), but with 600 rolls χ² = 9.46 looks like a fair die: p = 0.092. Not significant does not mean fair. On average the loading adds only 4.8 to χ² at this sample size; raise the number of rolls and it grows in proportion.

Teal bars are the counts in hand; the sky dashed line is what the null hypothesis of a fair die expects, n/6 for every face; the short white dashed marks are the truth you set. Each amber square has area (observed − expected)² / expected, and the stack on the right adds the six into χ². Below, sky bars are χ² for 10,000 fair dice, the white dashed curve is the chi-squared density on 5 degrees of freedom, and the rose region beyond the amber line is the p-value: the probability, if the die were fair, of a χ² at least this large. With 30 or 60 rolls only certain values of χ² can occur, so the bars are lumpy and the smooth curve is only an approximation.

Go deeper:Pearson’s Statistic and Degrees of Freedom

Karl Pearson published the test in the Philosophical Magazine in 1900. For each box, take the observed count O and the count E that the hypothesis expects, and add up the scaled squared differences: χ² = Σ (O − E)² / E. Dividing by E puts every box on the same footing, because a count that is expected to be E varies by roughly √E from one experiment to the next. If the hypothesis is true, each term is about 1 in size; if it is false, the terms grow in proportion to the number of observations.

The chi-squared distribution with k degrees of freedom is the distribution of a sum of k independent squared standard normal variables. Its mean is k and its variance is 2k. A die has six faces but only five degrees of freedom: the six counts must add up to n, so once five of them are known the sixth is fixed, and only five of the six deviations are free to vary.

The curve is an approximation to the true distribution of χ², which is discrete because counts are whole numbers. It is good when the expected counts are large and poor when they are small; the usual rule of thumb asks for an expected count of at least 5 in every box. The histogram in the demo is the honest answer in either case, since it comes from actually rolling fair dice.

Every parameter estimated from the data costs one more degree of freedom. If the expected counts come from a model whose parameters were fitted to the same counts, the fit is closer than it would be to a model fixed in advance, and the reference distribution must be shifted down to match. Pearson did not make this adjustment; R. A. Fisher showed in the 1920s that it is required, and the two men never agreed about it.

Tables and Independence

The table holds the real counts from the 1954 Salk vaccine trial. Drag the shrink slider to run the same trial with fewer children, press Simulate to see what chance alone does to χ², then type your own counts into any cell.

Randomised armPolioNo polioTotal
Vaccine
expected 99.4
residual −4.25
expected 200645.6
residual 0.09
200,745
Placebo
expected 99.6
residual 4.25
expected 201129.4
residual −0.09
201,229
Total199401,775401,974
The table as it is: area in proportion to count
57200,688Vaccine142201,087Placebo
If rows and columns were independent: the same split in every bar
99.4200645.6Vaccine99.6201129.4Placebo

Magnified: polio is so rare that only the top 0.10% of each bar is drawn. At full scale the difference between the arms would be thinner than a hair.

36.12
χ², the squared residuals added up
1
degrees of freedom: (2 − 1) × (2 − 1)
1.9 × 10⁻⁹
p-value from the chi-squared curve
1.5 × 10⁻⁹
p-value by Fisher’s exact test (two-sided)
99.4
smallest expected count (the curve is trusted from about 5 up)
28.4 vs 70.6
Polio per 100,000: Vaccine against Placebo
0.40
risk ratio; 95% interval 0.30 to 0.55
−42.2
risk difference per 100,000; 95% interval −55.9 to −28.4
0.40
odds ratio; 95% interval 0.30 to 0.55
χ² = 36.1 on 1 degree of freedom, p = 1.9 × 10⁻⁹: tables this far from independence almost never arise from chance alone with these totals. The size of the effect is a separate matter: the polio rate for vaccine is 0.40 times the rate for placebo, with a 95% interval from 0.30 to 0.55.

Type in any cell to change the table. Each cell shows its expected count if rows and columns were independent (row total × column total / grand total) and its residual, (observed − expected) / √expected; cells tinted orange hold more than expected, sky fewer, and the squared residuals add up to χ². In the mosaics the width of a bar is its row total and the sky dashed lines mark where independence would put the splits. The histogram is built from tables drawn at random with the same row and column totals; the white dashed curve is the chi-squared density, the amber line is this table, and rose marks the simulated tables at least as far from independence. The Salk counts are the real ones from the 1954 randomised trial; the 3 × 4 table is made up for you to edit.

Go deeper:Expected Counts, Fisher’s Exact Test and Effect Size

A two-way table classifies each individual twice, by row and by column. The test of independence asks whether the two classifications are unrelated: whether, for instance, the share of children who caught polio is the same in the vaccine and placebo arms. If it were, the expected count in each cell would be its row total × its column total / the grand total. Pearson’s statistic compares the observed counts with these, and for a table with r rows and c columns it is referred to the chi-squared distribution with (r − 1)(c − 1) degrees of freedom: once the margins are fixed, that many cells are free.

In the randomised part of the 1954 field trial, 57 of 200,745 vaccinated children and 142 of 201,229 children given placebo were recorded as polio cases: about 28 against 71 per 100,000. If the vaccine did nothing, each arm would expect about 99.5 of the 199 cases. That gives χ² of about 36 on one degree of freedom and a p-value near 2 in a billion. The trial enrolled over a million children in all; about 400,000 of them were in the randomised, double-blind, placebo-controlled part shown here. See also: Designing Experiments.

When counts are small the chi-squared curve cannot be trusted, and Fisher’s exact test replaces it. With the margins of a 2 × 2 table held fixed, the top-left count has a hypergeometric distribution if rows and columns are independent, so the probability of every possible table can be written down exactly. This is the same calculation as in the tea-tasting experiment, where the table is “milk first or tea first” against “what the lady said”. The Simulate button does the same thing by brute force for tables of any shape.

A tiny p-value does not measure the size of an effect. It says that chance is a poor explanation; how much the vaccine helps is a separate number, the risk ratio (0.40, with a 95% interval from 0.30 to 0.55, so about 60% fewer cases) or the risk difference (about 42 fewer cases per 100,000 children). The shrink slider shows the other side of this: with the same rates and only 4,000 children per arm, the trial would have seen about one case against three and decided nothing. Rare outcomes need enormous trials.

Too Good to Be True

Mendel’s two pea counts sit comfortably among honest experiments, and so does each one alone. Now look at the total of 84 experiments below, where the amber line is Mendel’s, and move the violet slider to see what a little tidying of the data does.

0.263, p = 0.61
seed shape: χ² on 1 degree of freedom, and its usual right-tail p-value
…
of honest experiments came closer to 3 : 1 than Mendel’s seed shape count (theory: 39%)
0.015, p = 0.90
seed colour: χ² on 1 degree of freedom, and its usual right-tail p-value
…
of honest experiments came closer to 3 : 1 than Mendel’s seed colour count (theory: 10%)
75.0
average honest total (theory: 84)
—
average total with tidying
0 of 1
honest sets with a total of 41.6 or less
—
tidied sets with a total of 41.6 or less
Eighty-four honest experiments give a total near 84 that rarely strays below 60. Fisher’s total for Mendel is 41.6. Every single experiment fits acceptably; it is all of them fitting so well at once that is remarkable. Fisher put the chance of agreement that close at 7 in 100,000; the chi-squared curve drawn here gives 2.9 × 10⁻⁵. Now move the violet slider.

Top: sky bars are the ratios from simulated experiments of Mendel’s size in which the true ratio is exactly 3 : 1 (white dashed); amber marks what Mendel published. Bottom: every experiment is simulated as 500 plants counted in five batches of 100, and scored by χ² on one degree of freedom. Sky is honest counting, with the white dashed chi-squared curve it should follow. Violet is a simulated assistant who, whenever a batch’s ratio is more than one standard error from 3 : 1, throws it out and counts a fresh one with the chance you set. The amber line is Fisher’s published total of 41.6 on 84 degrees of freedom for all of Mendel’s experiments, which were of several kinds; it is quoted, not recomputed, and the simulated sets are a simplified stand-in for them.

Go deeper:Fisher on Mendel, and the Left Tail

A chi-squared test has two tails. A large χ² says the data are too far from the theory to be chance. A very small χ² says they are closer to the theory than chance would normally leave them, and that is evidence too. If independent experiments have statistics on k1, k2, … degrees of freedom, their sum follows the chi-squared distribution on k1 + k2 + … degrees of freedom, so many small agreements can be added into one test.

In Mendel’s 1866 paper, 5,474 round seeds to 1,850 wrinkled is a ratio of 2.96 : 1, and 6,022 yellow to 2,001 green is 3.01 : 1, against a predicted 3 : 1. The chi-squared values are 0.26 and 0.015, each on one degree of freedom, and neither is remarkable alone. R. A. Fisher went through all of the experiments in the paper in “Has Mendel’s Work Been Rediscovered?” (Annals of Science, 1936) and found a total χ² of 41.6 on 84 degrees of freedom, where about 84 would be expected. He gave the probability of a worse fit as 0.99993, which leaves about 7 chances in 100,000 of a fit that good; evaluating the chi-squared distribution directly at 41.6 gives about 3 in 100,000. Either way it is far out in the left tail.

Fisher did not doubt Mendel’s laws, and he suspected an assistant who knew too well what was expected. What actually happened has been debated ever since and has never been settled. The suggestions include unconscious bias in classifying ambiguous peas, stopping the count when the numbers looked right, and nothing untoward at all once Mendel’s procedures are properly understood. The violet slider simulates one such mechanism to show that mild selection is enough; it is an illustration, not a finding about Mendel.

The demo uses only the two experiments above as raw counts. The figures 41.6 and 84 are Fisher’s published totals, quoted and not recomputed. The general lesson has outlived the argument: data that fit too well are evidence too, and checks of this kind are used today to screen for fabricated or selectively reported results.

Key Takeaways

  • Pearson’s χ² adds up (observed − expected)² / expected over the boxes: one number for how far counts are from a theory.
  • If the theory is true, χ² follows the chi-squared curve for its degrees of freedom, approximately, and well only when expected counts are not small.
  • In a table, expected counts come from the margins; a tiny p-value shows an association exists, not how large it is.
  • A fit can also be too good: a total χ² far below its degrees of freedom is evidence that something shaped the data.

Spot a mistake or have an idea?

Search