Skip to content
recaplica

    One moment: security check

    Cloudflare wants to make sure you're not a robot. Tick the box below and your search will continue on its own.

    IT
    recaplica Chi-Square Test: Formula, Example and Degrees of Freedom
    © 2026 Recaplica · recaplica.com — All rights reserved
    Home › Science

    Chi-Square Test: Formula, Example and Degrees of Freedom

    By Recaplica Newsroom · Updated on October 21, 2026

    What to print

    Page numbers appear when printing with default margins.

    Slides

    Choose a cut

    Flash10 slidesThe essential thread, to present in classFull15 slidesEvery chapter and the deeper detail

    Both come with speaker notes.

    Telegram channel
    recaplica Clear in 30 seconds, yours in 10 minutes.
    In 30 seconds Key points Figures Deep dive Slides Myths Mind map Quiz Flashcards FAQ

    In 30 seconds quick read

    The chi-square test compares the counts recorded in a table with the counts you would expect if a starting hypothesis were true. For each cell you take the gap between observed and expected, square it and divide by the expected count, then add up all the cells. If the total is larger than a critical value, which depends on the degrees of freedom, the hypothesis is rejected. The test tells you whether two classifications are related, and it can also check whether data follow a predicted distribution. When expected counts are very small, the result can no longer be trusted.

    Key Points

    • Karl Pearson published the test in 1900; it measures how far observed counts drift from the counts expected under the null hypothesis.
    • The statistic is χ² = Σ (O − E)² / E over every cell; in a contingency table a cell's expected count is its row total times its column total, divided by the grand total.
    • Degrees of freedom are (r − 1)(c − 1) for a test of independence and k − 1 for goodness of fit, minus one more for each parameter estimated from the data.
    • You reject the null hypothesis when the statistic exceeds the critical value in the table, which is 3.841 for 1 degree of freedom at the 0.05 level.
    • A significant result points to an association; standardized residuals show which cells are driving it.
    • With small expected counts the approximation breaks down: 2×2 tables call for Yates's correction or Fisher's exact test, and paired data for McNemar's test.

    Key figures

    • 1900 Year Karl Pearson published the chi-square test. Source: MacTutor History of Mathematics, University of St Andrews (Karl Pearson biography, 2003)
    • 3.841 Critical value of chi-square with 1 degree of freedom at the 0.05 significance level. Source: NIST/SEMATECH e-Handbook of Statistical Methods, table of critical values
    • 20% Largest share of cells allowed an expected count below 5 under the rule attributed to Cochran, which also rules out any expected count below 1. Source: Bewick, Cheek and Ball, Critical Care (2004)

    Deep Dive

    The chi-square test (written χ², and also called the chi-squared test) is a test on counts: how many times each category turns up in a sample. It sets those counts, the observed frequencies, against the ones you should see if a starting hypothesis were true, the expected frequencies. Karl Pearson published it in 1900. MacTutor, the history of mathematics archive at the University of St Andrews, says Pearson’s aim with the test was to end the normal distribution’s central role.

    One calculation answers two questions. A test of independence asks whether two ways of classifying the same items are related; a goodness of fit test asks whether a sample behaves the way a chosen distribution predicts.

    Null and alternative hypotheses in a chi-square test

    Chi-square is a good place to see how probability and statistics work together: a number computed from the data is checked against a probability distribution to choose between two hypotheses. The first, the null hypothesis H₀, is the one the calculation puts on trial.

    In a test of independence, the NIST handbook of statistical methods states the null hypothesis as the two classifications being independent. Put another way, the chance of landing in a given cell equals the chance of its row multiplied by the chance of its column, the same multiplication rule used for independent events when you calculate probability. The alternative hypothesis claims the product fails in at least one cell, which would mean the classifications depend on each other.

    For goodness of fit, H₀ says the data follow the stated distribution and the alternative says they depart from it.

    The chi-square test formula

    For every cell, take the observed frequency O and the expected frequency E, square the difference and divide by E. The statistic is the sum of all those pieces:

    χ² = Σ (O − E)² / E

    In a test of independence the expected counts come from the table’s totals. For a single cell:

    E = (row total × column total) / grand total

    A large statistic means the observed counts sit far from the expected ones. Yale’s statistics course reads this, for goodness of fit, as a sign that the proposed model describes the data badly. When the null hypothesis holds, the value roughly follows a chi-square distribution, which NIST defines as the sum of the squares of ν independent standard normal variables, ν being the degrees of freedom.

    Chi-square test example: wafer defects across three shifts

    NIST, the US federal standards agency, works through a factory case. A plant records 309 defects on wafers, sorted by type (A, B, C, D) and by production shift (1, 2, 3), and wants to know whether the mix of defects changes from one shift to the next. The two classifications make a contingency table with 3 rows and 4 columns; expected frequencies are in brackets.

    ShiftType AType BType CType DTotal
    115 (22.51)21 (20.99)45 (38.94)13 (11.56)94
    226 (22.99)31 (21.44)34 (39.77)5 (11.81)96
    333 (28.50)17 (26.57)49 (49.29)20 (14.63)119
    Total746912838309

    Practical example: for the cell «shift 1, type A» the row total is 94, the column total 74 and the grand total 309. The expected count is 94 × 74 / 309 = 22.51. Fifteen defects were actually observed, so this cell adds (15 − 22.51)² / 22.51 to the sum.

    Repeat that for all twelve cells and add them up, and NIST arrives at χ² = 19.18. The table has (3 − 1)(4 − 1) = 6 degrees of freedom, and at a 0.05 significance level the critical value for 6 degrees is 12.5916. Since 19.18 is higher, the independence hypothesis is rejected: in NIST’s reading, the data give significant evidence that the share of each defect type changes with the shift. The example’s p-value is 0.00387.

    Degrees of freedom in the chi-square test

    Degrees of freedom count how many cells can still vary once the totals are fixed. In a goodness of fit test with k categories there are k − 1. Yale’s course points out that the last count has no room to move, because the counts have to add up to the total. Each parameter estimated from the data costs one more, so with d estimated parameters you end up at k − 1 − d.

    NIST writes the same rule as k − c, with k counting the classes that are not empty and c equal to the estimated parameters plus one. In its normality example two parameters are estimated, so c is 3.

    For a contingency table, (r − 1)(c − 1), where c now stands for columns, follows from the same reasoning. There are r·c cells, and after the constraint on the grand total rc − 1 of them are free; subtract the (r − 1) + (c − 1) parameters estimated for the row and column totals and exactly (r − 1)(c − 1) remain.

    Chi-square goodness of fit test: the pea seed example

    Geneticists use the goodness of fit version to check whether counts match an expected ratio. Phillip McClean’s genetics course at North Dakota State University takes a set of pea seeds whose predicted ratio is 9:3:3:1, and counts 556 of them.

    Seed classObservedExpected
    Round and yellow (9/16)315312.75
    Round and green (3/16)108104.25
    Wrinkled and yellow (3/16)101104.25
    Wrinkled and green (1/16)3234.75
    Total556556

    Each expected count is the ratio’s fraction applied to the total, so 9/16 of 556 gives 312.75. Four classes mean 3 degrees of freedom, and the statistic comes to 0.47. At 3 degrees of freedom the table puts the 0.05 critical value at 7.82 (7.815 in the NIST table), and 0.47 corresponds to a probability above 0.90. The page concludes that the data are consistent with the 9:3:3:1 ratio.

    NIST adds two cautions for this use. The test needs data grouped into classes, so loose observations first have to go into frequency tables for categorical variables, and the value of the statistic depends on how those classes are drawn. Discrete distributions, the binomial and the Poisson among them, can be tested this way too.

    Chi-square independence test vs goodness of fit test

    Test of independenceGoodness of fit
    QuestionAre two classifications related?Do the data follow the predicted distribution?
    DataContingency table, r rows and c columnsOne classification into k classes
    Null hypothesisThe classifications are independentThe data follow the stated distribution
    Expected frequenciesRow total × column total / grand totalPredicted probability × total
    Degrees of freedom(r − 1)(c − 1)k − 1, minus estimated parameters
    ExampleWafer defects by shift (NIST)Pea seeds, 9:3:3:1 ratio

    How to read the result

    Pearson’s chi-square test looks at one tail only, the upper one. You find the column for 1 − α in the table and reject the null hypothesis when the statistic is larger than the value listed there. A few thresholds from the NIST table:

    Degrees of freedomα = 0.05α = 0.01
    13.8416.635
    25.9919.210
    37.81511.345

    McClean’s page treats 0.05 as the conventional threshold.

    A significant result tells you an association is there. Bewick, Cheek and Ball, who wrote a review of tests of association in Critical Care in 2004, caution that the test by itself does not say how strong it is. To find the cells that matter most, look at the standardized residuals, which are built on the gap between observed and expected; any beyond 2 in absolute value are significant.

    Practical example: in the review by Bewick and colleagues, 1706 patients with a central venous catheter are classified by catheter site and by outcome, three categories each. The expected count for the cell «internal jugular, no complications» is 1305 × 934 / 1706 = 714.5. The nine contributions add up to 51.26; with 4 degrees of freedom the critical value for P = 0.001 is 18.47, so P < 0.001. The authors write that the result suggests an association between catheter site and infectious complication.

    When the chi-square test is the wrong choice

    The chi-square distribution is an approximation, and it only works when expected counts are big enough. NIST says the approximation needs a large enough sample to be valid. Bewick and colleagues put a number on it, with the rule they attribute to Cochran: no cell may have an expected count under 1, and at most 20% of cells may fall under 5. Their way out is to combine categories where that makes sense. For 2×2 tables with few cases, the options are Yates’s continuity correction and Fisher’s exact test. Yates’s correction raises the smallest frequency by 0.5 and adjusts the others so the totals stay unchanged.

    Practical example: a trial of intravenous nitrate in heart attack patients enrolled 95 people; 3 of the 50 treated patients died, against 8 of the 45 controls. Chi-square gives 3.209 with 1 degree of freedom, P = 0.073. On the same data Fisher’s exact test gives a two-sided P of 0.140, while Yates’s correction brings the statistic down to 2.162 and pushes P up to 0.141.

    In large samples chi-square, Fisher and Yates land close together; in small ones Fisher and Yates return higher, more cautious P values. There is no full agreement on which to prefer; Bewick and colleagues report that Bland recommends Fisher or Yates for a more conservative approach.

    Paired data are a separate case, for instance the same patients measured twice. Chi-square ignores the pairing, so these tables go to McNemar’s test.

    Slide deck

    Slides ready to download and make your own in PowerPoint or Google Slides, with speaker notes. Pick the Flash cut or the Full one.

    Slide 1 of the presentation on Chi-Square Test: The chi-square testSlide 2 of the presentation on Chi-Square Test: Does the mix of defects change from shift to shift?Slide 3 of the presentation on Chi-Square Test: From a table of counts to a decisionSlide 4 of the presentation on Chi-Square Test: Chapter 01: Counted and expectedSlide 5 of the presentation on Chi-Square Test: Shift 1, type A: 15 defects seenSlide 6 of the presentation on Chi-Square Test: Chapter 02: Adding up the cellsSlide 7 of the presentation on Chi-Square Test: NIST's wafer total clears the 0.05 cut-offSlide 8 of the presentation on Chi-Square Test: Chapter 03: Degrees of freedom and the cut-offSlide 9 of the presentation on Chi-Square Test: 3 shifts and 4 types give 6 degrees of freedomSlide 10 of the presentation on Chi-Square Test: 556 · k - 1 · Below the cut-offSlide 11 of the presentation on Chi-Square Test: Chapter 04: Where the test stops workingSlide 12 of the presentation on Chi-Square Test: A huge chi-square means a very strong linkSlide 13 of the presentation on Chi-Square Test: With expected counts under 5 or paired measurements: Fisher, Yates, McNemarSlide 14 of the presentation on Chi-Square Test: How many degrees of freedom does a table with 3 rows and 4 columns have?Slide 15 of the presentation on Chi-Square Test: And now, the review
    Flash10 slidesThe essential thread, to present in classFull15 slidesEvery chapter and the deeper detail

    Common myths

    • ✗ Myth The bigger the chi-square value, the stronger the link between the two variables.

      ✓ Reality The statistic grows as observed and expected counts move apart, but Bewick, Cheek and Ball warn that by itself it does not measure how strong the association is. To find the cells that move the result, you compute standardized residuals; those beyond 2 in absolute value are significant.

    • ✗ Myth A chi-square test works on any table, however few cases it holds.

      ✓ Reality The statistic only follows a chi-square distribution approximately, and that approximation holds when expected counts are large enough; the rule attributed to Cochran allows counts under 5 in no more than 20% of cells. In a 2×2 table of 95 patients analysed by Bewick and colleagues, chi-square gave P = 0.073 and Fisher's exact test gave 0.140.

    • ✗ Myth To compare the same people before and after a treatment, a chi-square test is enough.

      ✓ Reality Measuring the same subjects twice produces paired observations, and the chi-square test takes no account of the link between the two measurements. For tables of this kind Bewick and colleagues point to McNemar's test.

    Mind map

    Drag the background to move around and the nodes to reposition them; use − and + to collapse and expand branches.

    Customize
    Mind map: Chi-Square Test: Formula, Example and Degrees of Freedom
    • Chi-square test
      • What it is for
        • Test of independence Two classifications of the same items
        • Goodness of fit Data checked against a predicted distribution
      • The formula
        • Observed frequencies The counts collected
        • Expected frequencies Row total times column total, divided by the grand total
        • Sum of contributions (O − E)² / E for each cell
      • Degrees of freedom
        • r by c table (r − 1)(c − 1)
        • k categories k − 1, minus estimated parameters
      • The decision
        • Critical value From the table, upper tail
        • 0.05 level Conventional threshold
        • Rejecting the null hypothesis When the statistic exceeds the threshold
      • Limits
        • Small expected counts Cochran's rule, none under 1 and at most 20% under 5
        • 2×2 tables Yates's correction or Fisher's test
        • Paired data McNemar's test
        • Cells that matter Standardized residuals

    Quiz: test yourself

    Answer the questions to check what you have learned: you get instant feedback and a short explanation.

    Grade 0/10 0/5
    1 In the NIST wafer table, shift 1 has 94 defects, type A has 74 and the grand total is 309. How do you get the expected count for the cell «shift 1, type A»?

    Under independence you multiply the two marginal totals of the cell and divide by the grand total: 94 × 74 / 309 = 22.51.

    2 How many degrees of freedom does a contingency table with 3 rows and 4 columns have?

    The rule is (r − 1)(c − 1). Three shifts and four defect types give (3 − 1)(4 − 1) = 6, the figure NIST uses in its wafer example.

    3 In the pea seed example the statistic is 0.47 with 3 degrees of freedom, and the critical value at 0.05 is 7.82. What do you conclude?

    A value of 0.47 sits far below the 7.82 threshold, with a probability above 0.90, so the null hypothesis stands. McClean's page judges the counts consistent with the expected ratio.

    4 True or false: the chi-square test stays reliable even when many cells have expected counts below 5.

    The chi-square approximation only holds when expected counts are reasonably large. The rule attributed to Cochran allows counts under 5 in no more than 20% of cells, and none under 1.

    5 The same students answer yes or no to a question before and after a course. Which test suits a comparison of the two sets of answers?

    Both sets of answers come from the same people, so the data are paired, the situation for which Bewick and colleagues describe McNemar's test as more appropriate.

    Answers: 1-B · 2-C · 3-B · 4-B · 5-D

    Flashcards

    Tap the card to flip it and check whether you remember the answer, then move to the next one.

    1 / 7

    Explain it in your own words

    The ultimate test: if you can explain it in simple words, you've truly understood it. Write your explanation, then compare it with the Recap.

    Your explanation is saved only on this device.

    The chi-square test compares the counts recorded in a table with the counts you would expect if a starting hypothesis were true. For each cell you take the gap between observed and expected, square it and divide by the expected count, then add up all the cells. If the total is larger than a critical value, which depends on the degrees of freedom, the hypothesis is rejected. The test tells you whether two classifications are related, and it can also check whether data follow a predicted distribution. When expected counts are very small, the result can no longer be trusted.

    Frequently asked questions

    What is the chi-square test formula and how do you calculate it?

    Start from the table of observed counts and work out the expected ones; in a contingency table each expected count is the row total times the column total, divided by the grand total. Then compute (O − E)² / E for every cell and add the results. Compare the sum with the critical value in the table for the right number of degrees of freedom.

    How many degrees of freedom does a chi-square test have?

    It depends on the test. For a table with r rows and c columns the answer is (r − 1)(c − 1), so a 3×4 table has 6. For goodness of fit, count the categories and subtract one; every parameter you had to estimate takes away another.

    What is the difference between a chi-square independence test and a goodness of fit test?

    The independence test takes two classifications of the same items, such as defect type and work shift, and checks whether they are related. Goodness of fit works with a single classification and asks whether the counts match a distribution or ratio set in advance, like the 9:3:3:1 ratio of the pea seeds. Both use the same formula; expected counts and degrees of freedom are worked out differently.

    When should you not use a chi-square test?

    When expected counts are too small (by the rule attributed to Cochran, none under 1 and no more than 20% under 5) and when the data are paired. In the first case you can merge categories or, for 2×2 tables, switch to Fisher's exact test or Yates's correction. In the second, McNemar's test is the one to use.

    What does a significant chi-square result mean?

    The statistic has passed the critical value at the chosen level, usually 0.05, so the null hypothesis is rejected. In a test of independence this signals an association between the two variables. Standardized residuals show which cells produce it.

    Sources

    • NIST/SEMATECH e-Handbook of Statistical Methods, §7.4.5, How can we compare the results of classifying according to several categories?
    • NIST/SEMATECH e-Handbook of Statistical Methods, §1.3.5.15, Chi-Square Goodness-of-Fit Test
    • NIST/SEMATECH e-Handbook of Statistical Methods, §1.3.6.7.4, Critical Values of the Chi-Square Distribution
    • NIST/SEMATECH e-Handbook of Statistical Methods, §1.3.6.6.6, Chi-Square Distribution
    • Bewick V, Cheek L, Ball J, Statistics review 8, Qualitative data – tests of association, Critical Care 2004; 8(1):46-53
    • Yale University, Department of Statistics, Statistics 101, Chi-Square Goodness of Fit Test
    • Phillip McClean, North Dakota State University, PLSC 431, The Chi-Square Test (archived copy)
    • MacTutor History of Mathematics, University of St Andrews, Karl Pearson

    Every Recap goes through an independent review before publication.

    Every evening, the day's new Recaps on our Telegram channel. Join the channel →

    Keep learning

    • Science Balancing Redox Reactions: Half-Reaction and Oxidation Number Methods A redox reaction is balanced when every element has the same number of atoms on both sides and the total charge is the same too. There are two routes. With the half-reaction method you write oxidation and reduction separately, add water, H⁺ and electrons, and then add the two halves together. With the oxidation number method you look at how much each element's number rises and falls, and pick coefficients that make the total rise equal the total fall. In basic solution you start as in acid, and at the end the H⁺ ions are turned into water with OH⁻. Read the Recap →
    • Science Brønsted-Lowry theory: definition of acids, bases and conjugate pairs In 1923 Johannes Brønsted and Thomas Lowry, working independently, defined an acid as a species that gives away a proton and a base as a species that takes one in. An acid-base reaction is therefore a transfer of a proton, and every transfer produces two conjugate pairs. Water can act as either an acid or a base, and ammonia is a base even though its formula has no OH-. The theory generalizes Arrhenius's, which holds only for water solutions. Read the Recap →
    • Science Chemical Equilibrium: Definition, the Constant K and the Reaction Quotient Q A reversible reaction can run in either direction, and in a closed system it reaches a point where the amounts of reactants and products stop changing. At that point the forward and reverse reactions keep going at the same rate. The equilibrium constant K depends on temperature. Comparing the reaction quotient Q with K shows which way the reaction is heading. An ICE table handles the calculations. Read the Recap →

    recaplica

    Clear in 30 seconds, yours in 10 minutes.

    Recaps Mind maps Request a Recap Telegram channel Mind map maker Our method About Privacy & cookies Legal notes & terms of use

    © 2026 Recaplica · A project by Curi S.r.l. — VAT IT05472000750

    Statistics, only if you say so

    To learn which Recaps help most we would use Google Analytics, with aggregate, anonymous data. It starts only with your OK, and you can change your mind anytime. Privacy policy