Skip to main content

Biostatistics in Practice: Principles and Procedures - A MedCalc companion e-book

Chi-Squared Test

The chi-squared test ($\chi^2$ test) assesses whether two categorical variables are statistically independent. Given a contingency table of observed frequencies, it compares those frequencies to the counts that would be expected if the two variables were unrelated. A large discrepancy between observed and expected counts produces a large $\chi^2$ statistic and a small $P$ value, providing evidence against independence. It is among the most widely used tests in clinical research, applied to questions ranging from whether treatment allocation is associated with outcome to whether two diagnostic categories are distributed differently between groups.

The Contingency Table and Expected Counts

The data are summarised in an $r \times c$ contingency table — $r$ rows and $c$ columns — where each cell contains the observed count of subjects falling into that combination of categories. The marginal totals (row sums and column sums) are the counts for each category considered separately.

Under the null hypothesis of independence, the expected count for the cell in row $i$ and column $j$ is:

$$ E_{ij} = \frac{R_i \times C_j}{n} $$

where $R_i$ is the total for row $i$, $C_j$ is the total for column $j$, and $n$ is the grand total. This follows directly from the multiplication rule for independent events: if the row and column classifications are independent, the probability of a subject falling in cell $(i, j)$ equals the product of the two marginal probabilities, $R_i/n$ and $C_j/n$, and the expected count is that probability multiplied by $n$.

The Test Statistic

The $\chi^2$ statistic summarises the discrepancy between observed and expected counts across all cells:

$$ \chi^2 = \sum_{i,j} \frac{(O_{ij} - E_{ij})^2}{E_{ij}} $$

Each cell contributes a squared standardised deviation: the difference $(O_{ij} - E_{ij})$ is squared so that positive and negative deviations do not cancel, then divided by $E_{ij}$ to weight the contribution relative to the size of the expected count. A cell where the expected count is small will contribute more per unit of deviation than a cell with a large expected count.

Under $H_0$, the statistic follows a $\chi^2$ distribution with

$$ \text{df} = (r - 1)(c - 1) $$

degrees of freedom. For a $2 \times 2$ table df = 1; for a $2 \times 3$ table df = 2; and so on. The $P$ value is the probability, under the null hypothesis, of observing a $\chi^2$ statistic as large as or larger than the computed value.

Assumptions and Limitations

The $\chi^2$ distribution is a large-sample approximation. It is adequate when the expected count in every cell is at least 5. When one or more expected counts fall below this threshold, the approximation can give misleading $P$ values — typically too small, overstating the evidence against independence.

For a $2 \times 2$ table with small expected counts, Fisher's exact test should be used instead. Fisher's test computes an exact $P$ value directly from the hypergeometric distribution, without any approximation, and is valid regardless of cell size. Many statisticians now recommend Fisher's exact test as the default for any $2 \times 2$ table: it is always valid and modern software makes the computation trivial. For larger tables ($r > 2$ or $c > 2$) with sparse cells, exact tests are available but computationally demanding; combining sparse categories where that is clinically defensible, or collecting more data, is often more practical.

The chi-squared test of independence described here is distinct from the chi-squared goodness-of-fit test, which compares an observed frequency distribution to a theoretical one (such as a Mendelian ratio or a Poisson distribution). The test statistic has the same form, but the degrees of freedom and interpretation differ.

Yates' Continuity Correction

In modern statistics, the consensus on Yates' continuity correction is clear: its use has significantly declined in professional practice, and many statisticians actively advise against it.

While it remains a staple in introductory textbooks — often taught as a "rule of thumb" for small sample sizes — it is frequently viewed by researchers today as an outdated or even detrimental practice.

The primary criticism is that Yates' correction "over-corrects." It makes the test too conservative, significantly increasing the risk of a Type II error (failing to detect a real effect). In many cases, it pushes the $p$-value above the traditional 0.05 threshold even when an association is genuinely present. This can lead to missed opportunities for discovery and underestimation of important associations, particularly in clinical research where sample sizes are often limited.

Worked Example

A randomised controlled trial compares two antibiotic regimens for community-acquired pneumonia. A total of 220 patients are allocated equally between antibiotic A and antibiotic B ($n = 110$ each). The primary outcome is clinical cure at 14 days:

Cured Not cured Total
Antibiotic A8723110
Antibiotic B7337110
Total16060220

The cure rate was 79.1% (87/110) with antibiotic A and 66.4% (73/110) with antibiotic B. The expected counts under independence are:

$$ E(\text{A, cured}) = \frac{110 \times 160}{220} = 80, \qquad E(\text{A, not cured}) = \frac{110 \times 60}{220} = 30 $$

By symmetry the expected counts for antibiotic B are the same. All four expected counts (80, 30, 80, 30) exceed 5, so the $\chi^2$ approximation is appropriate.

The test statistic is:

$$ \chi^2 = \frac{(87-80)^2}{80} + \frac{(23-30)^2}{30} + \frac{(73-80)^2}{80} + \frac{(37-30)^2}{30} = \frac{49}{80} + \frac{49}{30} + \frac{49}{80} + \frac{49}{30} = 4.49 $$

With df = 1 this gives $P = 0.034$. The difference in cure rates is statistically significant at the conventional 5% level.

MedCalc computes the chi-squared test and expected counts in its Chi-squared test procedure, together with Fisher's exact test for comparison.