Chi-square test calculator

Chi-square test calculator for goodness of fit and independence in a contingency table: χ², p-value, expected counts and Cramér's V.

Drafted with AI writing toolsUpdated Checked examples: 5

One count per category, e.g. how often each face of a die came up.
More options
Each one removes a degree of freedom (e.g. 1 when fitting a Poisson mean).
Try
p-value
p-value: 0.578555
Maximum decimal places: 6; Nearest, ties away from zero
Decision
Fail to reject H₀
χ² statistic
3.8
Degrees of freedom
5
Critical value
11.0705
Smallest expected count
20

Fail to reject H₀ at α = 0.05. If every category were equally likely, a result at least this extreme would turn up with probability 0.5786. The p-value is not the probability that H₀ is true, and not rejecting H₀ does not show it is true: the sample may be too small to detect a real effect. The largest contribution comes from category 1 (observed 15, expected 20).

p-value area under χ² with 5 df

00.050.10.1505101520χ²Densityχ² = 3.8critical 11.07

χ² contribution per category; labels show observed / expected (blue: above, orange: below)

1: 15 / 201.252: 22 / 200.23: 18 / 200.24: 25 / 201.255: 17 / 200.456: 23 / 200.45
Observed, expected and contribution to χ² Rows: 6
CategoryObservedExpected(O − E)²/E
115201.25
222200.2
318200.2
425201.25
517200.45
623200.45
How it's calculated S
  1. Hypotheses

    H₀: all category probabilities are equal. H₁: at least one differs.

  2. Expected counts

    Ei=N wi∑w⇒E=(20, 20, 20, 20, 20, 20)E_i = N\,\frac{w_i}{\sum w} \Rightarrow E = (20,\ 20,\ 20,\ 20,\ 20,\ 20)
  3. Statistic

    χ2=∑(O−E)2E=1.25+0.2+0.2+1.25+0.45+0.45=3.8\chi^2 = \sum \frac{(O-E)^2}{E} = 1.25 + 0.2 + 0.2 + 1.25 + 0.45 + 0.45 = 3.8
  4. Degrees of freedom

    ν=k−1−m=5\nu = k - 1 - m = 5
  5. p-value

    p=P(χ52≥3.8)=0.57855529p = P(\chi^2_{5} \ge 3.8) = 0.57855529
  6. Decision

    p>α=0.05p > \alpha = 0.05

    Fail to reject H₀

    Equivalently, compare χ² with the critical value 11.0705.

About the chi-square test calculator

Pearson's chi-square test compares observed counts with the counts a hypothesis predicts: χ² = Σ(O − E)²/E. The goodness-of-fit test takes the expected counts from equal or given proportions and has k − 1 degrees of freedom, one fewer for each parameter estimated from the data. The test of independence sets E = row total × column total ÷ grand total for each cell of a contingency table and has (r − 1)(c − 1) degrees of freedom.

It answers questions such as whether a die is fair or whether a preference depends on region. The default 120 rolls (15, 22, 18, 25, 17, 23) give χ² = 3.8 on 5 df and p = 0.579, far below the 11.07 needed at α = 0.05, so the counts are consistent with a fair die.

The χ² approximation needs expected counts of about 5 or more. Cramér's V, from 0 to 1, measures how strong an association in a table is.

How to calculate the chi-square statistic

Pearson's chi-square compares observed counts O with expected counts E: χ² = Σ (O − E)² ÷ E. A fair die rolled 120 times expects 20 of each face, so counts of 15, 22, 18, 25, 17 and 23 give χ² = 3.8 on 5 degrees of freedom and p = 0.579: no evidence of bias.

Worked by hand:

  1. Expected count per face: 120 ÷ 6 = 20.
  2. Contributions (O − E)² ÷ E: 1.25, 0.2, 0.2, 1.25, 0.45 and 0.45.
  3. Sum: χ² = 3.8.
  4. Degrees of freedom: 6 categories − 1 = 5.
  5. Compare with the 5% critical value for 5 df, 11.07, or read the upper-tail p-value, 0.579.

Only the upper tail counts, because a large χ² means a poor fit. A fair die would give counts at least this uneven in about 58% of 120-roll experiments.

How to find expected counts

For goodness of fit, each expected count is the total times the category's hypothesized share: Eᵢ = N × pᵢ. Mendel's 556 pea plants, tested against a 9:3:3:1 ratio, expect 312.75, 104.25, 104.25 and 34.75.

The observed 315, 108, 101 and 32 give χ² = 0.470 on 3 df and p = 0.925, a close fit to the ratio.

For a test of independence, each cell expects E = row total × column total ÷ grand total (NIST).

Take a table with rows 42, 33, 25 and 28, 37, 35: the row totals are 100 and 100, the column totals 70, 70 and 60, and N = 200.

The first two columns then expect 100 × 70 ÷ 200 = 35 in each row, and the third 100 × 60 ÷ 200 = 30. The six contributions sum to χ² = 4.695 on 2 df, so p = 0.0956 and the rows and columns are consistent with independence at α = 0.05.

Degrees of freedom and critical values

  • Goodness of fit: df = k − 1 − m for k categories, where m counts parameters estimated from the data, such as 1 when fitting a Poisson mean.
  • Independence: df = (rows − 1) × (columns − 1), so 2 for a 2×3 table and 1 for a 2×2.

The 5% critical values for 1, 2, 3 and 5 df are 3.841, 5.991, 7.815 and 11.070 (NIST).

When the chi-square approximation holds

The p-value comes from a large-sample approximation that needs large enough expected counts. Cochran's (1954) rule asks that no expected count fall below 1 and no more than 20% fall below 5.

The rule concerns expected counts, not observed ones, so an observed 0 is fine when its expected count is 5 or more. When the rule fails, merge sparse categories or, for a 2×2 table, use Fisher's exact test.

The statistic is Pearson's without Yates's continuity correction. Each observation must fall in exactly one cell, and observations must be independent.

Why the test needs counts, not percentages

χ² grows in proportion to the sample size. A 60/40 split tested against equal shares gives:

  • 6 and 4 out of 10: χ² = 0.4, p = 0.527
  • 60 and 40 out of 100: χ² = 4, p = 0.0455
  • 600 and 400 out of 1,000: χ² = 40, p below 0.000001

Entering percentages treats every sample as exactly 100 observations. Expected ratios are different: they can be on any scale, so 3, 2, 5 and 0.3, 0.2, 0.5 give the same expected counts.

Cramér's V: how strong is the association?

A small p-value says an association exists; Cramér's V says how strong it is, from 0 to 1: V = √(χ² ÷ (N × min(r − 1, c − 1))). The 2×2 table 20, 30 and 30, 20 has χ² = 4 with N = 100, so V = 0.2.

Cohen (1988) treats 0.1, 0.3 and 0.5 as small, medium and large for a 2×2 table. The 2×3 table above has V = 0.153. V shows no direction and says nothing about cause.

Chi-square test examples

Scenarioχ² statisticp-value
Default3.8000000.578555
Two categories4.0000000.045500
Mendel0.4700240.925426
Independence4.6952380.095597
Two by two4.0000000.045500

"Default" is the 120 die rolls and "Two categories" is 60 and 40 against equal shares. "Mendel" is the pea counts against 9:3:3:1, "Independence" the 2×3 table and "Two by two" the 20, 30 and 30, 20 table.

Entering the data

  • Counts are whole numbers of 0 or more. A table takes one row per line, with cells separated by spaces or commas.
  • Totals stay out. A totals row or column counts every observation twice.
  • Expected ratios need one positive value per category.
  • Estimated parameters each remove one degree of freedom, and at least 1 must remain.

Mistakes that change the conclusion

  • Counting one subject several times. Three answers from each of 40 people are not 120 independent observations.
  • Reading a large p-value as proof. 120 rolls can easily miss a modest bias, so p = 0.579 means no evidence of bias, not a fair die.
  • Testing every cell afterwards. The largest contributions show where a departure sits, but treating each cell as its own test inflates false positives.

For chi-square tails and critical values at other degrees of freedom, use the t, chi-square and F distribution calculator. Means rather than counts belong in the t-test calculator or the one-way ANOVA calculator.

Worked examples

Is the die fair? (defaults)

Test
Goodness of fit
Observed counts
15, 22, 18, 25, 17, 23
Expected distribution
Equal in every category
Significance level α
0.05
χ² statistic
3.8
Degrees of freedom
5
p-value
0.578555
Decision
Fail to reject H₀
Critical value
11.0705

Checked against: χ² by hand: Σ(O − 20)²/20 = 76/20; p from the A&S 26.4.4 closed form for odd ν; χ²₀.₉₅,₅ = 11.070 (χ² table)

Mendel's peas vs 9:3:3:1

Test
Goodness of fit
Observed counts
315 108 101 32
Expected distribution
Given ratios or counts
Expected ratios or counts
9 3 3 1
Significance level α
0.05
χ² statistic
0.470024
Degrees of freedom
3
p-value
0.925426

Checked against: Classic textbook example (χ² ≈ 0.47, p ≈ 0.93); exact χ² with exact fractions, p from A&S 26.4.4

2×2 table

Test
Independence
Contingency table
20 30 30 20
Significance level α
0.05
χ² statistic
4
Degrees of freedom
1
p-value
0.0455
Cramér's V
0.2

Checked against: All E = 25, χ² = 4·25/25 = 4; p = erfc(√2) = 0.0455003; V = √(4/100)

2×3 table (defaults)

Test
Independence
Contingency table
42 33 25 28 37 35
Significance level α
0.05
χ² statistic
4.695238
Degrees of freedom
2
p-value
0.095597

Checked against: Expected counts R·C/N and χ² with exact fractions; p = e^(−χ²/2) for ν = 2 (A&S 26.4.5)

Questions

What does the chi-square p-value mean?

It is the probability of a χ² statistic at least as large as the one observed if the null hypothesis holds, such as a fair die or independent rows and columns. For the default rolls p = 0.579: a fair die would give counts at least this uneven in about 58% of 120-roll experiments. A large p-value does not prove the die fair, since small samples can miss a real bias.

What is the minimum expected count for a chi-square test?

Cochran's (1954) rule asks that no expected count be below 1 and no more than 20% of cells be below 5. When it fails, merge sparse categories or, for a 2×2 table, use Fisher's exact test. The rule is about expected counts, not observed ones: an observed 0 is fine when its expected count is 5 or more. The warning here appears whenever any expected count is below 5.

How do you find the degrees of freedom for a chi-square test?

For goodness of fit, df = k − 1 − m, where k is the number of categories and m the number of parameters estimated from the data, so a six-sided die gives 5. For independence, df = (rows − 1) × (columns − 1): 2 for a 2×3 table and 1 for a 2×2 table. The 5% critical values for 1, 2 and 5 df are 3.841, 5.991 and 11.070.

What is Cramér's V?

Cramér's V = √(χ² / (N × (min(r, c) − 1))) rescales χ² to a 0-to-1 strength of association that does not grow with the sample size. The 2×2 worked example, χ² = 4 with N = 100, gives V = 0.2. Cohen (1988) treats 0.1, 0.3 and 0.5 as small, medium and large for a 2×2 table; tables with more rows and columns have lower thresholds.

Can I use percentages instead of counts?

No. χ² grows in proportion to the sample size, so the same percentages from 1,000 observations give 10 times the χ² of 100 observations. Entering percentages treats the sample as exactly 100 and gives the wrong p-value for any other sample size. Expected ratios, by contrast, can be on any scale: 9 3 3 1 is rescaled to the observed total.

About this calculator

χ2=∑(O−E)2E,Eij=Ri CjN,ν=(r−1)(c−1)\chi^2 = \sum \frac{(O - E)^2}{E},\qquad E_{ij} = \frac{R_i\,C_j}{N},\qquad \nu = (r-1)(c-1)

Sources

  1. NIST/SEMATECH e-Handbook of Statistical Methods, §1.3.5.15 Chi-square goodness-of-fit test
  2. NIST/SEMATECH e-Handbook, §7.4.5 contingency tables / test of independence
  3. Abramowitz & Stegun, Handbook of Mathematical Functions, §26.4 (χ² probability function)

Checked against references

This calculator includes 5 worked examples with independently sourced answers. They run in the test suite, and you can run them here too.

Put this calculator on your site

Paste this where you want the calculator to appear. It is free to use, it stays in step with corrections made here, and the credit line below it links back so your readers can find the full version.

Height
Preview the embed

Keep the credit line. It is the only condition, and it is what keeps these calculators free.