Skip to content

Chi-Square Test of Independence

Test whether two categorical variables are associated, using a contingency table of observed counts.

One row per line, comma-separated counts. Rows must all have the same number of columns.

What this answers

This test answers "are these two categorical variables associated, or does the data look consistent with them being independent?" Given a table of observed counts (for example, treatment group by outcome category), it compares what you actually observed against what you would expect if the two variables had no relationship at all.

How it is calculated

For each cell in the table, an expected count is computed from that cell's row total times its column total, divided by the overall total, the count you would expect under independence. The chi-square statistic sums, across every cell, the squared difference between observed and expected counts divided by the expected count. Larger differences from independence produce a larger statistic. Degrees of freedom equal the number of rows minus 1 times the number of columns minus 1, and the p-value is the probability of a chi-square statistic at least this large under genuine independence.

Worked example

For the table [[10, 20], [20, 10]]: row totals are 30 and 30, column totals are 30 and 30, and the overall total is 60, so every cell has an expected count of 15. The chi-square statistic works out to about 6.667 on 1 degree of freedom, which is significant at the .05 level. Cramer's V, a magnitude measure bounded between 0 and 1, comes out to about 0.333 here, a moderate association given this table's size.

Assumption audit

Calculated from your data: row and column totals, every cell's expected count under independence, and the smallest expected count in the table.
Evidence to review: whether any expected count falls below 5, shown as a warning below the results when it applies. The chi-square approximation becomes less reliable with small expected counts, and Fisher's Exact Test is the usual alternative for a small 2 by 2 table.
You must verify: that each observation contributes to exactly one cell (no individual counted twice across cells) and that observations are independent of one another, which the counts alone cannot establish.

What this result does not mean

A significant chi-square result means the data provide evidence against independence at your chosen threshold, not that one variable causes the other. Association in a contingency table can arise from a shared underlying cause, from how categories were defined, or from the specific sample collected, not only from a direct causal link between the two variables you tested.

Limitations

This test only detects association, and Cramer's V only quantifies its strength; neither identifies which specific cells drive the result. Inspecting the observed-versus-expected counts directly, cell by cell, is often needed to explain what an overall significant result actually reflects.