Chi-Square Calculator: Goodness of Fit, Independence and P-Values
By FindMySum · Last reviewed
The chi-square (χ²) test answers one question about counts: are the differences between what you observed and what you expected bigger than chance alone would produce? This calculator runs the two everyday versions of the test, goodness of fit and independence, and also turns any χ² statistic and degrees of freedom into a p-value and a critical value.
Three ways to use the calculator
Goodness of fit compares one row of counts with an expected distribution. Is a die fair? Do customers arrive equally on each weekday? Do Mendel's peas follow a 9:3:3:1 ratio? Enter the observed counts, then say what you expected: equal shares, proportions or ratios in any scale, or expected counts that add up to the same total.
Test of independence works on a contingency table, a grid of counts with one categorical variable down the side and another across the top. Is preferred product related to age group? Is treatment related to outcome? Type one row per line (or separate rows with semicolons). The calculator finds every expected count, the statistic, the p-value and Cramér's V, a measure of how strong the association is.
P-value from χ² and df is for when you already have the statistic, from a textbook exercise or another program's output, and need the p-value or the critical value for a significance level.
The formula
For every category or cell:
χ² = Σ (O − E)² ÷ E
where O is the observed count and E the expected count. Squaring makes every difference positive, and dividing by E scales each one: being 10 off when you expected 20 is a big miss; being 10 off when you expected 2,000 is not.
The p-value is the probability that a chi-square variable with the same degrees of freedom is larger than your statistic. The calculator computes it from the regularised upper incomplete gamma function, Q(df ÷ 2, χ² ÷ 2), to about 12 significant digits, so you do not need a table and you get an exact p rather than "between 0.05 and 0.10".
Worked example: is the die fair?
The calculator opens with 120 rolls of a die: 22 ones, 17 twos, 20 threes, 26 fours, 22 fives and 13 sixes. A fair die would give 20 of each.
| Face | Observed | Expected | (O − E)² ÷ E |
|---|---|---|---|
| 1 | 22 | 20 | 0.20 |
| 2 | 17 | 20 | 0.45 |
| 3 | 20 | 20 | 0.00 |
| 4 | 26 | 20 | 1.80 |
| 5 | 22 | 20 | 0.20 |
| 6 | 13 | 20 | 2.45 |
The statistic is the sum, χ² = 5.1, with 6 − 1 = 5 degrees of freedom. The area of the chi-square curve to the right of 5.1 is p = 0.404. At the usual 5% significance level the critical value for 5 df is 11.07, and 5.1 is well below it. There is no evidence the die is unfair: a spread like this happens in about 40% of fair runs of 120 rolls.
Degrees of freedom
Degrees of freedom count how many values could vary freely once the totals are fixed.
- Goodness of fit: df = k − 1, where k is the number of categories. Once you know the total and k − 1 of the counts, the last one is fixed. If you estimated parameters of the expected distribution from the same data (for example, the mean of a Poisson distribution), subtract one more for each. The "parameters estimated" box does that.
- Independence: df = (rows − 1) × (columns − 1). A 2 × 2 table has 1 degree of freedom, a 3 × 4 table has 6.
Expected counts in a contingency table
If the two variables were unrelated, every row would split across the columns in the same proportions as the whole table. That gives:
E = row total × column total ÷ grand total
In the calculator's default table, 32, 18, 10 in the first row and 20, 25, 15 in the second, the first row and first column both have known totals (60 and 52) and the grand total is 120, so the expected count in the top-left cell is 60 × 52 ÷ 120 = 26. The calculator lists every cell's observed and expected count and its share of χ² in the breakdown table, so you can see which cells drive the result.
Checking the assumptions
The chi-square p-value is an approximation. It is reliable when:
- The data are counts of independent observations: each person or item is counted once, in one cell.
- Expected counts are large enough. The usual rule is at least 5 in every cell; a common relaxation allows up to 20% of cells between 1 and 5 with none below 1. The calculator flags every expected count below 5 and highlights those rows.
- The categories are fixed in advance, not chosen after looking at the data.
When expected counts are too small, combine neighbouring categories, collect more data, or use an exact test. For a 2 × 2 table, Fisher's exact test is the standard alternative.
The Yates correction
For 2 × 2 tables, the chi-square approximation tends to give p-values that are slightly too small. Frank Yates proposed subtracting ½ from each |O − E| before squaring. The corrected statistic is smaller and the test more conservative. R's chisq.test applies it to 2 × 2 tables by default; Excel's CHISQ.TEST and many textbooks do not. The calculator shows both values when you switch it on, and ignores the setting for bigger tables, where it does not apply.
Effect size: Cramér's V
A p-value only says whether an association is likely to be real, not whether it matters. With thousands of observations, a trivial association becomes "significant". Cramér's V puts the strength on a 0-to-1 scale:
V = √(χ² ÷ (N × (min(r, c) − 1)))
As a rough guide for a 2 × 2 table, 0.1 is a small association, 0.3 medium and 0.5 large; the thresholds fall as the table gets bigger. Report V alongside χ² and p.
Reading the result
- p below α (for example, below 0.05): reject the null hypothesis. For goodness of fit, the counts do not follow the expected distribution; for independence, the variables appear related.
- p at or above α: the data are consistent with the null hypothesis. That is not proof the null is true; a small sample may simply lack the power to show a difference.
- The chart shows the chi-square curve for your degrees of freedom, the critical value and your statistic. The p-value is the area to the right of your χ².
Common mistakes
- Running the test on percentages or averages instead of counts.
- Counting the same person in more than one cell.
- Using a goodness-of-fit test when you have two variables (use independence) or the other way round.
- Reading a non-significant result as proof that two things are unrelated.
- Testing many tables and reporting only the significant ones, which inflates false positives.
More questions
Can I use percentages instead of counts in a chi-square test?
Not for the observed values. The statistic depends on the sample size: 60% of 10 people and 60% of 1,000 people give very different evidence, and percentages throw that away. Enter the observed counts; the expected distribution can be given as percentages or ratios, which the calculator scales to the observed total.
Is a chi-square test one-tailed or two-tailed?
The p-value is the area in the right tail of the chi-square distribution, because any departure from the expected counts, in any direction, makes χ² larger. In that sense it is a one-tailed test of a non-directional hypothesis. A χ² that is suspiciously close to 0 (a left-tail result) can point to data that fit too well, which is how Fisher questioned Mendel's numbers.
What is the difference between a chi-square test and a t-test?
A chi-square test works on counts in categories (how many people chose each option). A t-test compares the means of a measured quantity (average height, average score). If your data are numbers you averaged, use a t-test; if they are tallies, use chi-square.
Can the observed counts be decimals?
The calculator accepts them, but the test assumes whole-number counts of independent observations. Weighted survey totals or rates per 100 people are not counts, and a chi-square test on them gives a misleading p-value. Convert back to the number of people or items first.
What does a p-value of exactly 1 mean?
That χ² is 0: every observed count equals its expected count. It does not prove the expected distribution is right; it only means this sample shows no difference from it at all, which with real data is rare and worth a second look.
Sources
This article explains how the calculator works and is general information, not financial, tax, legal or medical advice. Rules change; check the sources above or a qualified professional before you act.