Chi-Square Calculator: Goodness of Fit & Independence Test

Gregor Mendel’s 556 pea seeds give χ² = 0.47 against his predicted 9 : 3 : 3 : 1 ratio, with 3 degrees of freedom and a p-value of 0.925. In plain terms, his counts sit about as close to the prediction as random variation allows, and in fact closer than a typical sample would land. That one number, the chi-square statistic, measures how far a set of counts is from the counts you expected.

This calculator runs the two main chi-square tests. The goodness of fit test compares one row of counts with expected shares. The test of independence checks whether two categorical variables are related, using a table of counts. For each test it shows the expected counts, what each category or cell adds to χ², residuals that point to where the difference lies, an effect size, a warning when expected counts are too small, and every step. For 2 × 2 tables it also runs Yates’ correction and Fisher’s exact test. It is set to Mendel’s counts.

Chi-Square Calculator

Type counts, not percentages. Expected values can be counts that add up to the same total, or any shares (25%, 0.25 or a ratio such as 1 : 2 : 1); the calculator scales them.

Chi-square test –
Observed, expected and residuals
Where your χ² falls (dashed line = critical value)
Step by step

    556 Peas and a 9 : 3 : 3 : 1 Ratio, Worked Through

    In his 1866 paper, Mendel crossed pea plants that differed in two traits, seed shape and seed color. His theory predicted that the second generation would split into four groups in the ratio 9 : 3 : 3 : 1. He counted 556 seeds:

    Seed typeObserved (O)Expected shareExpected (E)(O − E)² ÷ E
    Round, yellow3159/16312.750.0162
    Round, green1083/16104.250.1349
    Wrinkled, yellow1013/16104.250.1013
    Wrinkled, green321/1634.750.2176
    Total55615560.4700

    1. Expected counts. Multiply the total by each share: 556 × 9/16 = 312.75, 556 × 3/16 = 104.25 and 556 × 1/16 = 34.75. Expected counts don’t have to be whole numbers.
    2. Each category’s share of χ². Square the gap between observed and expected, then divide by expected. For round, green seeds: (108 − 104.25)² ÷ 104.25 = 0.1349.
    3. Add them up. χ² = 0.0162 + 0.1349 + 0.1013 + 0.2176 = 0.4700.
    4. Degrees of freedom. Four categories give 4 − 1 = 3. Once three of the counts and the total are known, the fourth is fixed.
    5. p-value. The chance of a χ² of 0.47 or more with 3 degrees of freedom is 0.925. The critical value at α = 0.05 is 7.815, and 0.47 is nowhere near it, so the counts fit the 9 : 3 : 3 : 1 ratio.

    Dividing by E is what makes the statistic fair across categories. A gap of 3.75 seeds matters more in a group that expects 34.75 than in one that expects 312.75, and the division weighs it that way: the smallest group, wrinkled green, adds the most to χ² even though its gap of 2.75 seeds is the second smallest.

    In the calculator, type the counts in the first box and the ratio 9 3 3 1 in the second. It scales any ratio, list of percentages or proportions to the total, so there is no need to work out 312.75 yourself.

    Which Chi-Square Test Fits Your Counts

    The calculator’s two tabs cover the two questions chi-square answers. Others look similar but need a different test:

    Your questionDataTestDegrees of freedom
    Do these counts match a ratio, a claim or equal shares?One row of countsGoodness of fitcategories − 1 (minus any estimated parameters)
    Are two categorical variables related?Table of counts from one sampleTest of independence(rows − 1) × (columns − 1)
    Do several groups share the same distribution?Table with one row per groupTest of homogeneity: same arithmetic as independence(rows − 1) × (columns − 1)
    Did the same people change their answer between two times?Paired yes/no dataMcNemar’s test, not this calculator1
    Is a 2 × 2 table too small for the approximation?Small 2 × 2 tableFisher’s exact test (shown in the calculator)not used

    The homogeneity and independence tests share the formula, the expected counts and the p-value. They differ only in how the data were collected. With independence, you take one sample and sort each person by two variables. With homogeneity, you sample each group separately and compare them.

    Chi-square needs counts: people, seeds, orders, defects. It doesn’t work on averages, percentages, measurements or scores, and each observation has to fall into exactly one category.

    Checking a Claim: A Tin of Mixed Nuts

    Goodness of fit doesn’t need equal shares or a famous ratio. Any stated mix works. A brand says its mixed nuts are 40% peanuts, 25% almonds, 20% cashews and 15% pecans. You count 250 nuts from a tin: 115 peanuts, 52 almonds, 48 cashews and 35 pecans.

    NutObservedClaimed shareExpected(O − E)² ÷ EResidual
    Peanuts11540%1002.250+1.50
    Almonds5225%62.51.764−1.33
    Cashews4820%500.080−0.28
    Pecans3515%37.50.167−0.41
    Total250100%2504.261

    χ² = 4.261 with 3 degrees of freedom gives p = 0.235. The tin has more peanuts and fewer almonds than claimed, but not by more than chance allows for 250 nuts. No residual reaches ±2. In the calculator, type 40 25 20 15 as the expected values; it reads them as percentages and scales them to 250.

    A single tin is also a single batch. If the claim fails in one tin, that says something about that batch, not necessarily about every tin the brand sells.

    The Expected Shares Decide the Answer: First Digits and Benford’s Law

    The same counts can pass one goodness of fit test and fail another, because the test only measures distance from the expected shares you give it. Take the first digits of 200 invoice amounts:

    First digit123456789
    Invoices62302520181412109
    Benford’s law30.1%17.6%12.5%9.7%7.9%6.7%5.8%5.1%4.6%

    Against equal shares (each digit 1/9 of the invoices), χ² = 97.63 with 8 degrees of freedom and p is about 1.3 × 10⁻¹⁷: the digits are anything but even. Against Benford’s law, where the chance of a first digit d is log₁₀(1 + 1/d), χ² = 1.19 and p = 0.997, a close fit.

    Many real-world amounts, from invoices to populations, follow Benford’s pattern, with 1 leading about 30% of the time and 9 under 5%. Forensic accountants use this as a screening check, since invented figures tend to spread their first digits more evenly. A poor fit is a reason to look closer, not proof of fraud.

    A Fit Too Good to Be True?

    A p-value of 0.925 means a χ² as small as Mendel’s or smaller turns up only about 7.5% of the time by chance alone. On its own, that isn’t strange. In 1936, though, the statistician Ronald Fisher ran chi-square tests on many of Mendel’s experiments together and argued that the results, taken as a group, matched the predicted ratios far too closely. He suggested the data might have been adjusted, deliberately or not, to fit the theory.

    Later authors have disputed Fisher’s analysis and offered statistical and botanical explanations, including the possibility that Mendel reported his clearest experiments rather than all of them. The debate is still cited as an example of something useful: chi-square can flag counts that fit too well, not only counts that fit badly. A left-tail probability close to 0 across many independent experiments suggests the variation you’d expect from real sampling is missing.

    Test of Independence: Payment Method by Age Group

    A shop asks 300 customers how they paid, 100 from each of three age groups:

    Age groupCardCashMobileTotal
    18–34401050100
    35–54552025100
    55 and over503515100
    Total1456590300

    The null hypothesis says payment method doesn’t depend on age. If that were true, each age group would split the same way the whole sample does.

    1. Expected counts. Each cell is row total × column total ÷ grand total. For 18–34 and card: 100 × 145 ÷ 300 = 48.33. Because every row total here is 100, each age group expects the same counts: 48.33 card, 21.67 cash and 30 mobile.
    2. Each cell’s share of χ². For 18–34 and mobile: (50 − 30)² ÷ 30 = 13.33. For 55 and over, cash: (35 − 21.67)² ÷ 21.67 = 8.21.
    3. Add all nine cells. χ² = 38.70.
    4. Degrees of freedom. (3 − 1) × (3 − 1) = 4.
    5. p-value. 8.05 × 10⁻⁸, far below 0.05. The critical value at α = 0.05 is 9.488. Payment method and age group are related in this sample.

    To enter this in the calculator, open the Independence tab and type the nine counts as three lines: 40 10 50, then 55 20 25, then 50 35 15. Leave out the totals.

    Strictly speaking, because the shop picked 100 customers from each age group on purpose, this is a test of homogeneity: do the three groups pay in the same mix? Had it asked 300 customers at random and sorted them by age afterwards, it would be a test of independence. The numbers, the χ² and the p-value are identical either way. Only the wording of the conclusion changes.

    Which Cells Drove the Result

    A significant χ² says the table as a whole doesn’t fit independence. It doesn’t say where. For that, the calculator shows an adjusted residual for each cell: the gap between observed and expected, scaled so that, if the variables were independent, each residual would behave roughly like a z-score. Values beyond about ±1.96 stand out at the 5% level.

    Age groupCardCashMobile
    18–34−2.04−3.47+5.35
    35–54+1.63−0.50−1.34
    55 and over+0.41+3.96−4.01

    The pattern is clear. Younger customers used mobile payment far more than independence predicts and cash far less. Customers 55 and over did the reverse. The middle group shows nothing unusual. The calculator colors cells above +1.96 orange and below −1.96 blue.

    Reading a dozen residuals at once means several chances for one to cross ±1.96 by luck. In a large table, treat a residual just past 2 as a hint, not a finding. Larger values, such as the +5.35 here, are hard to explain by chance.

    Effect Size: How Strong Is the Relationship?

    χ² grows with the sample size, so it can’t tell you how strong a relationship is. Two measures can:

    • Cramér’s V for a test of independence: V = √(χ² ÷ (N × m)), where m is the smaller of rows − 1 and columns − 1. It runs from 0 (no relationship) to 1 (each row falls entirely in one column). For the payment table, V = √(38.70 ÷ (300 × 2)) = 0.254.
    • Cohen’s w for goodness of fit: w = √(χ² ÷ N). For Mendel’s peas, w = √(0.47 ÷ 556) = 0.029, a tiny departure from the ratio.

    For a 2 × 2 table, V is the same as the phi coefficient φ. Jacob Cohen’s rough benchmarks for w are 0.1 small, 0.3 medium and 0.5 large. For V, the same words need smaller values as the table grows. The payment table, with m = 2, shows a moderate relationship.

    How χ² Turns Into a p-Value

    If the null hypothesis is true, χ² follows a chi-square distribution with the test’s degrees of freedom. Two features make results easy to judge at a glance:

    • Its average equals the degrees of freedom. Under the null hypothesis, a goodness of fit test with 3 degrees of freedom gives a χ² of about 3 on average. Mendel’s 0.47 is well below that. The payment table’s 38.70 with 4 degrees of freedom is nearly ten times its average.
    • It is skewed to the right. Small values are frequent and large ones rare, so the median sits a little below the average: 2.37 for 3 degrees of freedom and 3.36 for 4.

    The p-value is the area under this curve to the right of your χ², the part the calculator shades. A χ² near its degrees of freedom gives a middling p-value. One several times larger gives a small p-value. If you already have χ² and the degrees of freedom, the p-value calculator linked below does this step on its own.

    Counts, Not Percentages

    Say a survey finds 60% prefer option A and 40% option B, and you test it against an even 50/50 split. The answer depends entirely on how many people answered:

    People askedCountsχ²p-value
    5030 and 202.00.1573
    500300 and 20020.00.0000077
    Percentages typed in as counts60 and 404.00.0455

    Ten times as many people gives ten times the χ². Typing 60 and 40 treats the survey as if exactly 100 people answered, which gives a p-value that belongs to neither real sample. If you only have percentages, multiply each by the number of respondents to get counts, then run the test. The calculator warns when the counts aren’t whole numbers or add up to exactly 100, a sign that percentages went in.

    Small Expected Counts and Cochran’s Rule

    The chi-square p-value comes from an approximation that works well when expected counts are large enough. The short version of the guideline is “every expected count at least 5”. A more exact version, known as Cochran’s rule after a 1954 paper by William Cochran in Biometrics, is gentler:

    • no expected count below 1, and
    • no more than 20% of expected counts below 5.

    The rule is about expected counts, not observed ones. An observed count of 0 is fine, as long as its expected count isn’t tiny. The calculator lists the smallest expected count and how many fall below 5, and warns when the rule is broken.

    When it is, there are three fixes:

    • Combine categories that belong together, such as “4 or more” instead of separate 4, 5 and 6. Merge categories that make sense together, and choose them before looking at the results.
    • Collect more data, which raises every expected count.
    • For a 2 × 2 table, use Fisher’s exact test, which doesn’t rely on the approximation.

    2 × 2 Tables: Yates’ Correction and Fisher’s Exact Test

    A small trial gives a new treatment to 12 patients, and 9 improve. Of 11 patients on the standard treatment, 3 improve. Every expected count is above 5, the smallest being 5.26, so Cochran’s rule is met. Even so, three ways of testing the table disagree on the question people care about, whether p is below 0.05:

    MethodStatisticp-value
    Pearson chi-squareχ² = 5.2390.0221
    Chi-square with Yates’ correctionχ² = 3.5010.0613
    Fisher’s exact test–0.0391

    Yates’ continuity correction subtracts 0.5 from each |O − E| before squaring, which pulls χ² down to make up for using a smooth curve on whole-number counts. It is known to over-correct, giving p-values that are too large. Fisher’s exact test skips the approximation: it adds up the probabilities of every table with the same row and column totals that is at least as unusual as the one observed.

    For a small 2 × 2 table, Fisher’s exact p-value is the safest of the three to report. The calculator shows it for every 2 × 2 table, whether or not you tick the Yates box. With large counts, all three methods agree closely, and the choice stops mattering.

    Degrees of Freedom When You Estimate Something

    For goodness of fit, df = categories − 1 holds only when the expected shares come from outside the data, such as Mendel’s theory or a fair 25% each. If you estimate something from the same data to build the expected counts, each estimate costs one more degree of freedom.

    A factory counts defects on 100 circuit boards: 35 boards have none, 37 have one, 18 have two, 7 have three and 3 have four. Do defects follow a Poisson distribution, the pattern you’d expect if they occur at random?

    1. Estimate the rate from the data: (0 × 35 + 1 × 37 + 2 × 18 + 3 × 7 + 4 × 3) ÷ 100 = 1.06 defects per board.
    2. Expected counts from the Poisson distribution with mean 1.06: 34.65, 36.72, 19.46, 6.88 and 2.29 for “4 or more”.
    3. Combine the last two groups. The “4 or more” group expects only 2.29. With 1 of 5 cells below 5, Cochran’s rule is only just met, so combine them into “3 or more” (10 observed, 9.17 expected), which keeps every expected count above 5. Four categories remain.
    4. χ² = 0.192.
    5. df = 4 − 1 − 1 = 2, because the rate was estimated from the data. The p-value is 0.909: the defects are consistent with random occurrence.

    Forgetting the estimated parameter would use df = 3 and give p = 0.979. Here the conclusion doesn’t change, but when the result is near the line, using too many degrees of freedom makes a poor fit look acceptable. The calculator’s “Parameters estimated” box subtracts them for you.

    The G-Test: A Second Opinion on the Same Counts

    The calculator also reports the G-test, or likelihood-ratio test, for any table that isn’t 2 × 2. It uses G = 2 × Σ O × ln(O ÷ E) and the same degrees of freedom. For Mendel’s peas, G = 0.475 and p = 0.924, against χ² = 0.470 and p = 0.925. For the payment table, G = 38.54 against χ² = 38.70.

    With reasonable expected counts, the two tests agree closely, as here. A noticeable disagreement is a sign that the expected counts may be too small for either approximation to be trusted. That is a reason to combine categories, not to pick the friendlier number.

    Chi-Square Ignores Order

    The test treats categories as labels. Swap the columns of the payment table, putting mobile first and cash last, and χ² stays at exactly 38.70. That is right for categories like payment methods, which have no natural order.

    When the categories are ordered, such as “never, sometimes, often” or age groups, a chi-square test throws away the order. A test built for trend, such as a rank correlation, can find a steady pattern of rising or falling that a chi-square test spread over many cells might miss. Chi-square tells you the table isn’t independent. It can’t tell you whether the pattern runs in one direction.

    Significant Doesn’t Mean Important

    With enough data, chi-square flags differences too small to matter. Take two ad designs shown to 10,000 people each. Design A gets 5,100 clicks and design B gets 4,900. The test of independence gives χ² = 8.0 and p = 0.0047, comfortably significant. But Cramér’s V is 0.02, and the click rates are 51% and 49%.

    Whether a 2-point difference is worth acting on depends on the cost and the stakes, not on the p-value. The two-proportion tab of the confidence interval calculator shows how large or small that difference could really be. The calculator adds a note when a result is significant but the effect size is below 0.1. Report V or w alongside every chi-square result, so a reader can judge the size of the effect as well as whether it beat chance.

    The reverse also holds. A non-significant result doesn’t prove the null hypothesis. With small samples, chi-square can miss real differences, so “no evidence of a relationship” is not the same as “evidence of no relationship”.

    What a Significant Result Can’t Tell You

    A test of independence shows that two variables are associated in your data. It doesn’t show that one causes the other. If older customers use more cash, age itself may not be the reason. Income, how often they shop or which branch they visit could sit behind both.

    The test also assumes each observation is independent: one answer per person, and people who don’t influence each other’s answers. Counting the same customer’s ten visits as ten observations, or surveying a whole family as separate people, breaks this assumption, and the p-value comes out smaller than it should be. If the same people answer twice, before and after, use McNemar’s test instead.

    Reading Everything the Calculator Shows

    Each result has the same parts, from the headline down:

    • The headline gives χ², the degrees of freedom and the p-value, with a plain verdict at the α you chose (0.05 unless you change it).
    • Critical value. The χ² you would have needed to reach significance. Comparing it with your own χ² is the same decision as comparing p with α.
    • Effect size. Cramér’s V for a table, Cohen’s w for goodness of fit.
    • Smallest expected count. With a count of how many fall below 5, so you can check Cochran’s rule at a glance.
    • Fisher’s exact test or the G-test. Fisher appears for 2 × 2 tables, the G-test for everything else.
    • The table. Every category or cell with its observed count, expected count and residual. Orange marks counts well above what was expected, blue well below.
    • The curve. The chi-square distribution for your degrees of freedom, with your χ² marked, the p-value area shaded and the critical value as a dashed line.
    • The steps. Each calculation with your numbers in it, ready to copy into homework or a report.

    Checking the Result in a Spreadsheet

    Excel and Google Sheets have three chi-square functions:

    What you wantFormulaPayment example
    p-value straight from a table=CHISQ.TEST(observed_range, expected_range)8.05 × 10⁻⁸
    p-value from a χ² you already have=CHISQ.DIST.RT(38.6958, 4)8.05 × 10⁻⁸
    Critical value=CHISQ.INV.RT(0.05, 4)9.488

    CHISQ.TEST returns the p-value, not the χ² statistic, and you have to build the expected table yourself first. Microsoft’s documentation shows this: its example table has χ² = 16.16957 with 2 degrees of freedom, and the function returns 0.0003082. The calculator gives both numbers and the expected counts in one step.

    A result written up in APA style gives the degrees of freedom, the sample size, χ² to two decimals and the p-value, followed by the effect size: χ²(4, N = 300) = 38.70, p < .001, V = 0.25. The calculator’s Copy button puts the same pieces on one line.

    Five Checks Before You Trust a Chi-Square Result

    1. Are these counts? Not percentages, averages or scores. Each person or item counted once.
    2. Are the observations independent? One answer per person, no repeated visits from the same customer, no matched pairs.
    3. Are the expected counts big enough? None below 1 and no more than 20% below 5. If not, combine categories or use Fisher’s exact test for a 2 × 2 table.
    4. Were the categories and expected shares fixed before looking at the data? Merging or splitting categories after seeing the result changes the p-value.
    5. Is the effect large enough to matter? Check V or w, not only p.

    Critical Values of χ² (df 1–30)

    The calculator works out exact p-values, but a table helps when checking a result by hand. A χ² larger than the value in the table is significant at that level:

    dfα = 0.10α = 0.05α = 0.01α = 0.001
    12.7063.8416.63510.828
    24.6055.9919.21013.816
    36.2517.81511.34516.266
    47.7799.48813.27718.467
    59.23611.07015.08620.515
    610.64512.59216.81222.458
    712.01714.06718.47524.322
    813.36215.50720.09026.124
    914.68416.91921.66627.877
    1015.98718.30723.20929.588
    1117.27519.67524.72531.264
    1218.54921.02626.21732.909
    1319.81222.36227.68834.528
    1421.06423.68529.14136.123
    1522.30724.99630.57837.697
    1623.54226.29632.00039.252
    1724.76927.58733.40940.790
    1825.98928.86934.80542.312
    1927.20430.14436.19143.820
    2028.41231.41037.56645.315
    2129.61532.67138.93246.797
    2230.81333.92440.28948.268
    2332.00735.17241.63849.728
    2433.19636.41542.98051.179
    2534.38237.65244.31452.620
    2635.56338.88545.64254.052
    2736.74140.11346.96355.476
    2837.91641.33748.27856.892
    2939.08742.55749.58858.301
    3040.25643.77350.89259.703

    The chi-square test always uses the right tail, because a bigger χ² means a worse fit. For more than 30 degrees of freedom, the p-value calculator gives the exact critical value and p-value for any χ² and df.