Introduction
Welcome to the next lesson in our exploration of categorical data analysis. In our previous session, we established the critical foundation for hypothesis testing by learning how to formulate the null () and alternative () hypotheses for a test of independence. We defined the null hypothesis as the statement that two categorical variables have no relationship.
But how do we decide if our data provides enough evidence to reject that claim? We need a way to measure the discrepancy between what we actually observed and what we would expect to see if the variables were truly independent. This lesson introduces the tool for that job: the Pearson chi-square () statistic.
By the end of this lesson, you will be able to explain how this statistic is constructed by comparing observed and expected counts, and you will know how to determine its degrees of freedom, which is essential for interpreting the test's result.
1. The Core Logic: Observed vs. Expected Counts
The entire chi-square test hinges on a single, powerful idea: comparing the data you have with the data you would expect if your null hypothesis were true.
- Observed Counts (): These are simply the raw numbers in your contingency table. They are the frequencies you actually observed in your sample.
- Expected Counts (): These are the counts we would theoretically expect in each cell if the two variables were perfectly independent.
How do we calculate these expected counts? Recall from the last lesson that under independence, the probability of an observation falling in cell is the product of its marginal probabilities: .
We can estimate these probabilities from our data's row and column totals. To get the expected count for a cell, we multiply the total sample size () by this joint probability:
This simplifies to the well-known formula for expected counts:
The following video provides an excellent walkthrough of calculating these expected values in a practical example.
Contingency table chi-square test | Probability and Statistics | Khan Academy
Watch this segment from a Khan Academy video to see how to calculate the expected counts for a contingency table, assuming the null hypothesis of independence is true.
Please watch from 03:30 to 09:07. Focus on how the overall proportion of 'sick' people is used to calculate the expected number of sick people in each experimental group.
The expected counts create a blueprint of a world where there is no association between the variables. The chi-square test simply measures how far our real-world observations deviate from this "no association" blueprint.
2. Constructing the Pearson Chi-Square Statistic
Now that we have both our observed () and expected () counts for every cell, we can calculate the chi-square statistic. The goal is to combine the differences between and across all cells into a single number that quantifies the total discrepancy.
The formula for the Pearson chi-square statistic is:
Let's break this formula down to understand its logic, which is more important than memorizing the formula itself:
- : For each cell, we find the difference between the observed and expected counts. This is the raw deviation.
- : We square each difference. This serves two purposes: it makes all deviations positive (so that cells with fewer-than-expected counts don't cancel out cells with more-than-expected counts) and it penalizes larger deviations more heavily.
- : We standardize each squared difference by dividing it by the expected count for that cell. This is a crucial step. A difference of 10 is much more surprising if you only expected 5 counts than if you expected 500. This step puts all deviations on a common scale.
- : Finally, we sum these standardized, squared deviations across all cells in the table. This gives us a single, overall measure of how much our observed table deviates from the table we'd expect under independence.
A value of 0 means the observed counts perfectly match the expected counts. The larger the value, the greater the discrepancy, and the more evidence we have against the null hypothesis of independence.
Understanding the Chi-Square Test Beyond the Formula
This article, 'Understanding the Chi-Square Test Beyond the Formula', provides a superb step-by-step conceptual walkthrough of constructing the chi-square statistic.
Please read from the beginning down to the section where the Chi-Square statistic is calculated (ending just before the discussion of degrees of freedom). Pay close attention to how the author explains the reasoning behind each step: calculating expected counts, finding the deviation, squaring it, and standardizing it.
3. Determining the Degrees of Freedom
The final piece of the puzzle is the degrees of freedom (df). A calculated value of, say, 8.5 is meaningless on its own. We need to know if 8.5 is a "large" or "small" value for a table of our specific size. The degrees of freedom provide this context.
Conceptually, the degrees of freedom for a contingency table are the number of cell counts that are free to vary after the row and column totals are fixed.
Imagine a simple 2x2 table. If you know the row totals and the column totals, you only need to know the value of one cell. Once that single cell is filled in, all other cell values are automatically determined to make the totals add up correctly. Therefore, a 2x2 table has only 1 degree of freedom.
This logic extends to larger tables, leading to a simple formula:

The following video provides an excellent visual and intuitive explanation of this concept.
What are degrees of freedom?!? Seriously.
To solidify your understanding, this video from zedstatistics provides a fantastic intuitive explanation of why the degrees of freedom are calculated this way for a test of independence.
Please watch from 24:18 to 26:48. The video demonstrates visually how filling in a few cells in a table with fixed totals determines the values of the remaining cells.
The degrees of freedom determine the specific chi-square probability distribution we use to evaluate our test statistic. Different degrees of freedom result in differently shaped distributions.

Conclusion
In this lesson, we deconstructed the Pearson chi-square test, moving from the logic of hypothesis testing to the mechanics of calculating a test statistic. You now have the tools to quantify the evidence against a claim of independence.
Key Takeaways:
- The Pearson statistic is a measure of the total discrepancy between the observed counts () in your sample and the expected counts () that would occur if the null hypothesis of independence were true.
- Expected counts are calculated for each cell using the formula: .
- The chi-square formula, , sums the standardized squared deviations across all cells to produce a single test statistic.
- The degrees of freedom () determine the specific chi-square distribution used for comparison and are calculated as .
Preview of the Next Lesson:
We now know how to calculate the statistic and its degrees of freedom. But what are the rules for using this test? In the next lesson, we will discuss the critical assumptions of the chi-square test, particularly regarding sample size and expected counts. You'll learn how to check these assumptions and what to do when they are violated, which will lead us directly to an important alternative: Fisher's exact test.