Home / Tools / Chi-Square Test Calculator

Chi-Square Test Calculator

A chi-square test asks whether two categorical variables are related (like sex and smoking status), or whether observed counts match what you would expect by chance. Paste a 2-D table or a row of observed counts to get chi-square, the p-value, Cramer's V, standardized residuals, and a mosaic plot. Everything runs in your browser.

I want to run a on at α = .

New to chi-square? Read the 4-min primer

What it is. The chi-square test asks one question of a table of counts: are these counts farther from what we would expect under independence (or under a hypothesised distribution) than chance can comfortably explain? If yes, the row variable and the column variable are not independent; some cells carry a real signal.

Three flavours, one statistic. Independence takes a two-way table (e.g. treatment by outcome) and tests whether the two classifications are linked. Goodness-of-fit takes one row of counts and tests whether they match a hypothesised distribution (uniform dice, expected proportions). Homogeneity tests whether several samples share the same distribution; the math is identical to independence.

The recipe. For each cell compute the expected count under the null: E = row_total * col_total / N for independence, or E = N * p_hyp for goodness-of-fit. Sum the squared, scaled deviations: chi-square = sum (O - E)^2 / E. Compare against a chi-square distribution with df = (rows-1)(cols-1) for independence or df = k-1 for goodness-of-fit.

Beyond the p-value. A small p tells you something is off; standardised residuals tell you where. Cells with |r| > 1.96 drive the result. Cramer's V (independence) and Cohen's w (goodness-of-fit) put a magnitude on the association so you can say not just "significant" but "weak / moderate / strong".

Try a real example:
🧪 2×2 vaccineVaccinated vs unvaccinated, infected vs not. Are vaccination and infection independent?

You have a two-way table of counts and want to test whether row and column classifications are independent.

e.g. Does outcome (infected / not) depend on group (vaccine / placebo)?

  • tbl a 2-D table of counts
  • α significance level
Significance α
no result

Enter a table to see the result

Cell area is proportional to the observed count; tint to the standardised residual.

χ²
-
df
-
p-value
-
Cramer's V
-
n
-

Plain-English readEnter a table above to see a plain-English verdict.

How this is computed
1
2
3
4
The same test in R

  

No data leaves your browser Verified against R's chisq.test() Free, no sign-up

Anatomy of the chi-square test

One statistic, four moving parts. Here is what happens between the counts you paste and the verdict above.

Expected counts (independence): E[i,j] = row_total[i] * col_total[j] / N
Step 1: build the null table. Under independence, the joint probability factors into row- and column-marginals. Multiply marginal probabilities, scale by the grand total, and you get the count you would expect to see in each cell if the variables were unrelated.
Pearson chi-square: chi-square = sum (O[i,j] - E[i,j])^2 / E[i,j] df = (rows - 1) * (cols - 1)
Step 2: pool the deviations. Each cell contributes a squared, scaled deviation. Cells with a high expected count tolerate larger absolute deviations; small expected counts make a small absolute deviation contribute a lot. The sum is approximately chi-square under H0; compare to qchisq(1 - alpha, df).
Standardised residual: r[i,j] = (O - E) / sqrt(E * (1 - row%) * (1 - col%))
Step 3: read the cells. Pearson residuals (O - E)/sqrt(E) are easy but biased. The standardised version (matches R's chisq.test()$stdres) divides by an SE that accounts for the marginal totals; under H0 each cell is approximately N(0, 1). Cells with |r| > 1.96 drive the result.
Effect size: Cramer's V = sqrt(chi-square / (N * min(rows-1, cols-1))) Cohen's w = sqrt(chi-square / N)
Step 4: size the effect. A significant chi-square at large N is almost guaranteed; effect size is what tells you whether the association is practically meaningful. Cramer's V (independence) and Cohen's w (goodness-of-fit) both range from 0 to 1; conventional benchmarks: 0.1 small, 0.3 medium, 0.5 large.
Yates' correction (2x2 only): chi-square_Y = sum (|O - E| - 0.5)^2 / E
Step 5: optional correction. The chi-square approximation can be poor for 2x2 tables with small expected counts. Yates' continuity correction subtracts 0.5 from each absolute deviation before squaring; it tends to be conservative. Default is off (matching R's chisq.test(x, correct = FALSE)); switch it on for small 2x2 tables, or use Fisher's exact directly.

When this is the wrong tool

If you have...Use instead
Any expected count < 5 (especially in 2x2)Switch to Fisher's exact test - the chi-square approximation degrades when expected cells are sparse. The handoff button above adds it to the R code.
Paired binary outcomes (before/after on the same subject)McNemar's test - chi-square treats rows as independent, which paired data are not. Use mcnemar.test().
Ordinal categories (low / med / high) where order mattersThe Cochran-Armitage trend test or a polychoric correlation. Chi-square ignores ordering and wastes power.
Stratified 2x2 tables (e.g. across hospitals)Cochran-Mantel-Haenszel test - pooling strata via plain chi-square risks Simpson's paradox.
Continuous variables binned into categoriesDo not bin first - use a t-test, ANOVA, or correlation. Binning throws away information and the p-value depends on the cut points.
3+ way (multidimensional) tablesLog-linear models or hierarchical contingency analysis. Chi-square only handles two classifications cleanly.

Frequently asked questions

When should I use chi-square versus Fisher's exact test?

Use chi-square when all expected cell counts are at least 5. Switch to Fisher's exact when any expected cell count drops below 5, the sample is small (n < 30), or you have a 2x2 table where the asymptotic approximation is unreliable. Fisher's test is also the right choice for case-control studies with rare outcomes.

How do I interpret Cramer's V from a chi-square test?

Cramer's V measures the strength of association in a contingency table, from 0 (no association) to 1 (perfect). Rules of thumb: 0.1 is small, 0.3 medium, 0.5 large. Unlike the chi-square statistic itself, V does not grow with sample size, so it tells you whether a significant result is also practically meaningful.

What does Yates' continuity correction do?

Yates' correction subtracts 0.5 from each absolute deviation in a 2x2 table before squaring, compensating for treating discrete counts as continuous. It reduces the false-positive rate but is conservative for moderate samples. R's chisq.test applies it by default; set correct = FALSE to disable. Modern practice often prefers Fisher's exact for small samples.

What is the difference between independence and homogeneity?

The arithmetic is identical: both build expected counts from the marginals and sum the scaled squared deviations. They differ in study design. Independence samples one group and cross-classifies it on two variables (are they linked?). Homogeneity samples several groups with fixed row totals and asks whether they share the same column distribution. Same chi-square, different sentence.

Does this calculator match R?

Yes. The chi-square statistic, degrees of freedom and p-value match R's chisq.test(), the standardised residuals match chisq.test()$stdres, and Cramer's V matches rcompanion::cramerV(). The math is verified against R 4.6.0 across every mode and edge case.

Go deeper

Numerical accuracy: chi-square, df, and p match R's chisq.test() to at least 4 decimals; standardised residuals match chisq.test()$stdres; Cramer's V matches rcompanion::cramerV().