Chi-square test, goodness of fit and independence
χ² = Σ (O − E)² / E
Two tests share the chi-square name and this tool keeps them apart: goodness of fit, which confronts observed counts with a stated distribution, and independence, which crosses two characters in a table and derives the expected counts from its margins. Each run returns the statistic, its degrees of freedom, the p-value, the critical value at the chosen level, the contribution of every cell and its standardised residual, Cramér’s V for the strength of the relationship, and the check of Cochran’s rule that decides whether the approximation is admissible at all.
Scientific dossier
What the tool computes, what it assumes, where it stops being valid, and where its data comes from.
Method & formulasχ² = Σ (O − E)² / E
χ² = Σ (O − E)² / E
E(independence) = (row total × column total) / total
df(goodness of fit) = k − 1 − m
df(independence) = (r − 1)(c − 1)
V = √(χ² / (N × min(r − 1, c − 1)))
The statistic adds up, cell by cell, the squared departure between observed and expected count, divided by the expected count. That division is what makes departures comparable across cells: ten too many against an expectation of a thousand does not weigh like ten too many against an expectation of ten. Under the null hypothesis the sum approximately follows a chi-square law whose degrees of freedom count what the data have not already been used to fix.
- O
- · observed count in a cell.
- E
- · expected count under the null hypothesis: given for goodness of fit, derived from the margins for independence.
- k
- · number of categories in the goodness-of-fit test.
- m
- · number of parameters estimated from the data to build the expected distribution.
- r, c
- · number of rows and columns of the contingency table.
- N
- · total count across all cells.
- Standardised residual
- · (O − E) / √E: it says which cell carries the departure, and in which direction. Beyond 2 in absolute value the local departure is strong.
- Cramér’s V
- · strength of the relationship between two characters, from 0 to 1, independent of total sample size.
- p-value
- · probability of a departure at least this large if the null hypothesis were true.
Validity domainThe chi-square law is only an approximation to the true law of the statistic, valid when expected counts are large enough: Cochran’s rule asks that all be at least 1, and that at most a fifth be below 5.
The chi-square law is only an approximation to the true law of the statistic, valid when expected counts are large enough: Cochran’s rule asks that all be at least 1, and that at most a fifth be below 5. The tool checks that condition and says so, without quietly correcting the result: when it fails, the p-value is optimistic and the test rejects too readily, an exact Fisher test being preferable on a 2×2. The test also assumes independent observations and counts, never percentages: entering percentages amounts to declaring a sample of 100 individuals and distorts the p-value. The Yates correction is defined for a 2×2 table only; requested elsewhere it is ignored, and the tool says so.
Reading the resultA p-value below the level α leads to rejecting the null hypothesis: the counts do not follow the stated distribution, or the two characters are not independent.
A p-value below the level α leads to rejecting the null hypothesis: the counts do not follow the stated distribution, or the two characters are not independent. A p-value above the level does not prove the null hypothesis; it only records that this data is not enough to contradict it, which may reflect too small a sample. The test never says which cell makes the difference: that is the job of the cell-by-cell detail and the standardised residual. Nor does it say which character causes the other, since an association may come from a third common factor.