MathsStatistics & Probability

Chi-Square
calculator.

Compare the counts you observed with the counts a chi-square test would expect.

How it's worked out

Both tests work on counts of categories, such as how many seeds were yellow. Goodness of fit uses one list of counts and asks whether they match a claimed split. A test of independence uses a two-way table and asks whether the rows and columns are related.

The statistic adds up how far each observed count O sits from its expected count E, scaled by E.

χ2\displaystyle \chi^2=∑(O−E)2E\displaystyle {}= \sum \frac{(O - E)^2}{E}

For goodness of fit, E is the total times the share you expect, and the degrees of freedom are the number of categories k minus 1. For a table with r rows and c columns, E comes from the totals.

E\displaystyle E=row total×column totalN\displaystyle {}= \frac{\text{row total} \times \text{column total}}{N}
df\displaystyle \text{df}=(r−1)(c−1)\displaystyle {}= (r - 1)(c - 1)

A large χ2\chi^2 is evidence against the claim being tested, so the p-value is the area in the right tail of the curve. An effect size then says how large the gap is, whatever the sample. That is Cohen's w for goodness of fit and Cramér's V for tables.

w\displaystyle w=χ2N\displaystyle {}= \sqrt{\frac{\chi^2}{N}}
V\displaystyle V=χ2N×min⁡(r−1, c−1)\displaystyle {}= \sqrt{\frac{\chi^2}{N \times \min(r - 1,\ c - 1)}}

The tool also gives the G statistic, a likelihood ratio that tests the same hypothesis and usually agrees closely with χ2\chi^2. On a table with one degree of freedom it adds Yates' correction, which subtracts one half from each difference before squaring.

Critical values at each significance level
dfα = 0.10α = 0.05α = 0.01
12.7063.8416.635
24.6055.9919.21
36.2517.81511.345
47.7799.48813.277
59.23611.0715.086
610.64512.59216.812
813.36215.50720.09
1015.98718.30723.209
1522.30724.99630.578
2028.41231.4137.566

At α=0.001\alpha = 0.001 the critical values are 10.828 for 1 df, 20.515 for 5 df, 29.588 for 10 df, 45.315 for 20 df. Reject the claim when χ2\chi^2 is above the value for your row.

Worked examples

Theo tests a die

Theo rolls a die 60 times and counts 8, 9, 12, 11, 10, 10 of each face. With equal shares, every face is expected 10 times.

χ2\displaystyle \chi^2=0.4+0.1+0.4+0.1+0+0\displaystyle {}= 0.4 + 0.1 + 0.4 + 0.1 + 0 + 0
χ2\displaystyle \chi^2=1\displaystyle {}= 1

With 5 degrees of freedom, p = 0.9626, and the critical value at α=0.05\alpha = 0.05 is 11.07. Theo has no evidence against the die. That is not proof it is fair: this same pattern of counts would need about 664 rolls to reach significance.

Open this example in the tool

A class checks Mendel's peas

A genetics class takes Mendel's 556 seeds: 315 round yellow, 108 round green, 101 wrinkled yellow and 32 wrinkled green. Theory predicts 9:3:3:1, so the expected counts are 312.75, 104.25, 104.25, 34.75.

The contributions add to χ2=0.47\chi^2 = 0.47 on 3 degrees of freedom, far under the critical value of 7.81, and p = 0.9254. The counts fit the ratio well. The ratio goes in as it is, because the tool scales it to the total.

Open this example in the tool

Party and gender in a large survey

A General Social Survey table sorts 2,757 adults by gender and party. The first cell, women who are Democrats, has an expected count of 703.67, against the 762 counted.

(762−703.67)2703.67\displaystyle \frac{(762 - 703.67)^2}{703.67}=4.83\displaystyle {}= 4.83

All six cells give χ2=30.07\chi^2 = 30.07 and G = 30.02 on 2 degrees of freedom, with p = 3 × 10⁻⁷. Party and gender are related. Yet Cramér's V is only 0.104, a small effect. The largest adjusted residual is 5.32, for Female × Republican.

A large sample makes a modest gap highly significant. With a tenth of the people in the same proportions, χ2\chi^2 would be 3.01 and p = 0.2223.

Open this example in the tool

Reading your result

Read p and the effect size together. The p-value says whether the gap is likely to be chance. The effect size says how big it is. Cohen's bands for w are 0.10 small, 0.30 medium and 0.50 large, and they apply to V once it is adjusted for the size of the table.

Check the expected counts first. The p-value comes from a large-sample approximation. One common rule asks for at least 80% of cells with an expected count of 5 or more, and none under 1. It is the expected count that matters, not the count you observed.

Use residuals to see where the gap is. A significant test does not say which cell differs. A residual beyond about 2 either way marks a cell worth a look. In a 2 by 2 table of 647, 622, 2 and 27, the adjusted residual is 4.7 in size in all four cells, since such a table has one degree of freedom.

A small p is not a proven cause. The test finds a link between categories. It cannot tell you why it is there, or which way it runs.

Common mistakes

  • Entering percentages. The cells must hold counts. Turn percentages back into counts with the total first, because 50% of 20 and 50% of 2,000 carry very different evidence.
  • Counting a person twice. Each person or item must fall in one cell only. Before and after answers from the same people need a test for paired data, such as McNemar's test.
  • Running it on sparse counts. Counts of 3, 1 and 2 have expected counts of 2 each, so 3 of 3 cells break the rule. Merge categories, or use Fisher's exact test on a 2 by 2 table.
  • Reading a large p as proof of a fit. A high p-value means the data give no evidence against the claim. With a small sample, that says little.

Questions people ask

Check the whole table first, since one low cell among many can still pass. Five cells with 1 under 5 meet the rule, as 20% is the limit, so long as none is under 1. If the rule fails, merge categories that make sense together. On a 2 by 2 table, Fisher's exact test is the usual choice.

Sources