Chi-square tests compare observed category counts with the counts expected under a null model. For two-way tables, the same calculation supports two different study questions: independence within one population and homogeneity across several populations or treatments.
Let the design identify the test
A test of independence uses one random sample from one population and records two categorical variables for each individual. Its null says the variables are independent in that population.
A test of homogeneity uses independent samples from different populations, or groups assigned different treatments, and compares the distribution of one categorical response. Its null says those distributions are the same across groups.
The formulas are identical, but the hypotheses and scope of interpretation differ. Choose the label from how the data were collected, not from the visual shape of the table.
Expected counts come from the null model
For each cell, the expected count is
Under independence or a common distribution, each row should have the same category proportions as the overall table. Multiplying the row size by an overall column proportion gives the expected cell count.
Expected values are counts and may be fractional. Do not round them to integers before calculating the statistic, and do not replace the observed counts with percentages in the chi-square sum.
Example: calculate expected counts and contributions
Suppose two independent samples produce this preference table:
| Group | Prefer A | Prefer B | Total |
|---|---|---|---|
| 1 | 30 | 20 | 50 |
| 2 | 20 | 30 | 50 |
| Total | 50 | 50 | 100 |
Under homogeneity, every expected cell count is The statistic is
Each term measures a squared discrepancy relative to the expected count. Squaring prevents positive and negative discrepancies from canceling. Large statistics indicate greater disagreement with the null model.
Degrees of freedom and the P-value
For a table with rows and columns, degrees of freedom are The example has one degree of freedom, and
At a 5% significance level, reject the common-distribution null. The evidence suggests the population preference distributions differ. The test is right-tailed because departures in either direction increase the squared-discrepancy statistic; a small chi-square statistic is not evidence against the null.
A significant result identifies an overall association or distribution difference, not automatically which categories explain it. Inspect cell contributions and conditional proportions to describe the pattern, while avoiding unsupported extra significance claims from many unplanned comparisons.
Check the conditions on expected counts
Observations should be independent, with random sampling or randomized assignment supporting the intended inference. Each individual contributes to one cell. For sampling without replacement, the usual 10% condition supports approximate independence.
For the standard chi-square approximation, expected cell counts should generally all be at least 5 in the introductory procedure. This condition concerns expected counts, not observed counts. If it fails, the usual approximation may be unreliable; use an appropriate alternative rather than merging categories merely to force a desired conclusion.
Example: distinguish association from causation
A random sample of adults records exercise category and sleep quality. A significant independence test supports an association in the target population. It does not establish that exercise causes better sleep, because the study did not randomly assign exercise and other variables may influence both.
If participants were randomly assigned to exercise programs and sleep category measured afterward, a homogeneity test could assess whether treatment groups have different response distributions. A causal interpretation would then depend on the experiment's design and execution.
Try it yourself
A 3-by-4 table has grand total 240. One row totals 60 and one column totals 80. Find their intersection's expected count and the test's degrees of freedom.
Answer: The expected count is Degrees of freedom are Both quantities come from the full table structure, not just the observed cell value.
What to remember
Choose independence or homogeneity from the design, calculate expected counts from margins, sum standardized squared discrepancies, and use the right tail with the correct degrees of freedom.