Menu

Chi-Square Calculator

Independence on a contingency table or goodness of fit, with every expected count exact.

By Nethanel Bar, Co-founder & CEO

Last updated

Want to solve these without the calculator?

The Coddy math course teaches the method itself - you work each step on an interactive board and get told exactly where a move went wrong.

A chi-square test measures how far the counts are from what you expected

Every chi-square test compares a table of observed counts with the counts some hypothesis predicts. For a test of independence the hypothesis is that the row variable and the column variable have nothing to do with each other, so each expected count is the row total times the column total over the grand total. For a goodness-of-fit test the hypothesis is a set of shares, equal shares for a fair die or 9:3:3:1 for Mendel's peas, scaled to your total. Either way the statistic is the same sum: for every cell, observed minus expected, squared, divided by expected.

That sum is exact. A 2 by 2 table with counts 20, 30, 30, 20 has every expected count equal to 25 and a chi-square of exactly 4; a messier table has expected counts like 350/3, and this page keeps them as 350/3 rather than 116.67, so the statistic you read is the true one and not a sum of five rounded terms. What cannot be exact is the p-value, which is the area under the chi-square curve beyond the statistic; the page draws that tail and labels the number as the area it is.

The table under the answer is the part a student is marked on and the part most calculators skip: each cell's observed count, its expected count and its contribution to the statistic. It also shows where the association lives. In a large table one or two cells often carry most of the chi-square, and the contribution column points at them. Cramér's V, given for contingency tables, turns the statistic into an effect size between 0 and 1 that can be compared across tables of different sizes.

What to know about chi-square tests

  • Degrees of freedom are (rows − 1)(columns − 1) for a table and categories − 1 for goodness of fit. A 2 by 2 table has one degree of freedom whatever the counts.
  • Expected counts under 5 make the approximation rough. The page warns when it sees one; with a 2 by 2 table Fisher's exact test is the usual fallback, and merging sparse categories is the usual fix elsewhere.
  • The test is always right-tailed. A small chi-square means the counts fit the hypothesis; only a large one is evidence against it.
  • Significant is not large. With enough data a tiny association is significant; Cramér's V says how strong it is, and 0.1 is weak, 0.3 moderate, 0.5 strong.
  • Counts, not percentages. The statistic depends on the actual number of observations; a table of percentages with the total lost cannot be tested.

How to run a chi-square test by hand

  1. Lay out the observed counts

    Rows for one variable's categories, columns for the other's. Total each row, each column, and the whole table.

  2. Compute each expected count

    Row total times column total, divided by the grand total. Keep the fractions; they are about to be squared.

  3. Compute each cell's contribution

    Observed minus expected, squared, over expected. Add them all up; the total is the chi-square statistic.

  4. Find the degrees of freedom and the p-value

    (rows − 1)(columns − 1). The p-value is the right-tail area of the chi-square curve with that many degrees of freedom; compare with alpha, or the statistic with the critical value.

Chi-square critical values

The statistic must exceed these for the result to be significant at the given level.

Degrees of freedomα = 0.10α = 0.05α = 0.01
12.7063.8416.635
24.6055.9919.210
36.2517.81511.345
47.7799.48813.277
59.23611.07015.086
914.68416.91921.666

Worked examples

A 2 by 2 table

plain
20, 30 / 30, 20

Row totals 50 and 50, column totals 50 and 50, grand total 100, so every expected count is 50 × 50 / 100 = 25. Each cell contributes 25/25 = 1, the chi-square is exactly 4 on one degree of freedom, and the p-value is about 0.046. Significant at 5%, just; Cramér's V is 0.2, a weak association.

Is the die fair?

plain
10, 12, 8, 11, 9, 10 over six faces

Sixty rolls, so ten expected per face. The contributions are 0, 0.4, 0.4, 0.1, 0.1 and 0, giving a chi-square of exactly 1 on five degrees of freedom and a p-value of about 0.96. Nothing here suggests the die is loaded.

Mendel's peas

plain
315, 108, 101, 32 against 9 : 3 : 3 : 1

The total is 556, so the expected counts are 556 × 9/16 = 312.75, 104.25, 104.25 and 34.75. The chi-square comes to about 0.47 on three degrees of freedom, p about 0.93: the counts fit the 9:3:3:1 prediction almost suspiciously well, which is a famous argument in its own right.

Chi-square mistakes

  • Testing percentages. Convert them back to counts first; the statistic scales with the number of observations.
  • Rounding the expected counts. An expected count of 116.67 squared and divided is not the same as 350/3 squared and divided; small errors in every cell add up across the table.
  • Ignoring an expected count under 5. The chi-square curve is an approximation that gets bad in sparse cells; merge categories or use an exact test.
  • Reading a large chi-square as a strong association. It grows with the sample size; Cramér's V is the measure of strength.
  • Using the wrong degrees of freedom. A 3 by 4 table has 6, not 11 and not 12.
  • Running a two-tailed chi-square. The test is one-tailed by construction; a very small statistic is not evidence of anything except a good fit.

Chi-square FAQ

How do I calculate a chi-square statistic?
For every cell, subtract the expected count from the observed count, square the result, and divide by the expected count. Add those terms over all cells. For a contingency table the expected count is the row total times the column total over the grand total; for goodness of fit it is the total times the expected share of that category.
What is the difference between the test of independence and goodness of fit?
The test of independence works on a two-way table and asks whether the row variable and the column variable are related; its expected counts come from the table's own margins. Goodness of fit works on a single list of counts and asks whether they match shares you specify in advance, such as equal shares or a genetic ratio.
How do I find the p-value for a chi-square test?
It is the area under the chi-square curve, with the test's degrees of freedom, to the right of the statistic. A chi-square of 3.841 on one degree of freedom gives p = 0.05; larger statistics give smaller p-values. The page computes the area directly and shades it on the curve.
What if an expected count is less than 5?
The chi-square approximation becomes unreliable. The usual advice is that every expected count should be at least 5, or at worst that no more than a fifth of them are below 5 and none below 1. For a 2 by 2 table use Fisher's exact test instead; for a larger table, merge sparse categories. The page flags the condition when it sees it.
What is Cramér's V?
An effect size for a contingency table: the square root of the chi-square over the sample size times the smaller of (rows − 1) and (columns − 1). It runs from 0, no association, to 1, a perfect one, and unlike the chi-square itself it does not grow just because the sample did.
Can I use a chi-square test on a 2 by 2 table with small counts?
Only with care. With expected counts of 5 or more it is fine; Yates's continuity correction is sometimes applied, though it tends to be conservative. Below that, Fisher's exact test computes the exact probability of the table and is the standard choice.
Why does this page keep the expected counts as fractions?
Because the statistic is a sum of squared differences divided by those counts, and rounding each expected count before squaring puts a small error into every term. An expected count of 350/3 is exactly that; 116.67 is not. Keeping the fractions makes the chi-square you read the true value of the table you typed.

More math tools

Coddy programming languages illustration

Learn math with Coddy

GET STARTED