Statistics

Chi-Square Test Calculator

Find out whether two categories are related, or whether counts match the split you expected. Paste a table straight from Excel and get χ², the p-value, expected counts, Cramér’s V and the cells driving the result, with Fisher’s exact test for 2×2 tables.

Your data

Paste your counts and press Calculate, or load the example.

What this calculator does

The chi-square (χ²) test compares the counts you observed with the counts you would expect if nothing interesting were going on. The test of independence asks whether two categorical variables are related: if they were unrelated, each cell of the table would simply reflect its row and column totals. The goodness of fit test asks whether one categorical variable follows a split you specify, such as equal shares or last year’s mix.

Besides χ² and the p-value, you get the expected counts, Cramér’s V (or Cohen’s w for goodness of fit) as an effect size, adjusted residuals that show which cells drive the result, the likelihood-ratio G-test, and for 2×2 tables Yates’ correction and Fisher’s exact test.

The formulas

Expected: Eij = (row totali × column totalj) / N
χ² = Σ (O − E)² / E,   df = (rows − 1)(columns − 1)   ·   goodness of fit: df = k − 1
Cramér’s V = √( χ² / (N × (min(rows, columns) − 1)) )   ·   Cohen’s w = √(χ² / N)
Adjusted residual = (O − E) / √( E × (1 − row share) × (1 − column share) )

The p-value is the upper tail of the chi-square distribution with those degrees of freedom.

Worked example: payment method by city tier

Press Load example. An online store took 1,300 recent orders and recorded the customer’s city tier and how they paid: UPI, card or cash on delivery (COD).

  • Result: χ²(4) = 74.63, p < 0.0001. Payment method and city tier are clearly related.
  • Effect size: Cramér’s V = 0.169, a small association for a 3×3 table (medium starts near 0.21). Tier matters, but it is far from the whole story.
  • Where it comes from: Tier 3 uses COD much more than independence predicts (39.7% of its orders against 25.7% overall, adjusted residual +6.6), while Tier 1 uses COD less (−6.6) and cards more (+5.7). UPI is broadly similar everywhere.

The business reading: COD costs and return risk concentrate in Tier 3, so that is where a prepaid incentive would pay off. Card-specific offers mainly reach Tier 1 customers.

Worked example: are tickets spread evenly across the week?

Switch to Goodness of fit and load the example: 800 support tickets by day of week, tested against an equal split (114.3 expected per day). The result is χ²(6) = 33.56, p < 0.0001. Sunday alone contributes 15.65 of the 33.56, with only 72 tickets against 114 expected, so weekend staffing can be lighter than weekday staffing.

Assumptions

  • Counts, not percentages or averages. Each record is counted in exactly one cell.
  • Independent records. The same customer should not appear in several cells (for repeated yes/no answers from the same people, use McNemar’s test).
  • Large enough expected counts: none below 1 and at most 20% below 5. The calculator checks this for you.

Getting the same result in Excel and Python

In Excel, build the expected table with =row_total*column_total/grand_total, then =CHISQ.TEST(observed_range, expected_range) returns the p-value. In Python:

from scipy import stats
import numpy as np

observed = np.array([[312, 148,  90],    # Tier 1: UPI, Card, COD
                     [241,  72, 117],    # Tier 2
                     [158,  35, 127]])   # Tier 3

chi2, p, dof, expected = stats.chi2_contingency(observed, correction=False)
print(chi2, p, dof)   # 74.63, 2.4e-15, 4
print(stats.contingency.association(observed, method="cramer"))   # 0.169

print(stats.chisquare([142, 128, 121, 119, 131, 87, 72]))   # goodness of fit: 33.56, p = 8.2e-06

Common mistakes

  • Running the test on percentages. The result then depends on nothing but the percentages, and is meaningless.
  • Reading significance as strength. With thousands of records, a trivial association can be significant. Check Cramér’s V.
  • Stopping at “they are related”. The residuals tell you which categories differ and in which direction, which is usually the actual answer.
  • Including a totals row or column in the pasted table. The calculator drops rows and columns labelled “Total” automatically and tells you.

Related learning on Codeayan

FAQ

Chi-square questions

Which chi-square test do I need?

Use the test of independence when you have two categorical variables measured on the same people or orders, for example payment method and city tier, and want to know whether they are related. Use goodness of fit when you have one categorical variable and want to compare its counts with an expected split, for example whether support tickets are spread evenly across the week.

What does "expected count below 5" mean, and is it a problem?

The chi-square p-value is an approximation that works well when expected counts are large. The usual rule is that no expected count should be below 1 and no more than 20% of them below 5. If your table breaks that rule, merge sparse categories, collect more data, or, for a 2×2 table, use the Fisher exact p-value shown in the Other tests tab.

The test is significant. Which categories are driving it?

Open the Residuals tab. Each cell shows its adjusted (standardised) residual: how many standard errors the observed count sits above or below what independence predicts. Cells beyond ±1.96 are unusual at the 5% level; the further from zero, the stronger the departure. The verdict also names the largest ones.

What is a good Cramér's V?

Cramér's V runs from 0 (no association) to 1 (perfect association). What counts as large depends on the table size. For a 2×2 table, about 0.1 is small, 0.3 medium and 0.5 large; for a table whose smaller side has 3 categories, roughly 0.07, 0.21 and 0.35. The calculator applies the right thresholds for your table.

Should I use Yates' continuity correction?

Yates' correction only applies to 2×2 tables and makes the test more conservative. Many statisticians now prefer the uncorrected test for reasonable sample sizes, or Fisher's exact test for small ones. The calculator shows all three for a 2×2 table so you can report the one your course or team uses.

Can I enter percentages instead of counts?

Not for the observed data. The chi-square test needs the actual counts, because the same percentages from 50 people and from 5,000 people carry very different evidence. For goodness of fit, the expected column can be counts, proportions or percentages; the calculator rescales it to your total.

More tools

Keep going