Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 5

Calculating Chi-Square for Contingency Tables

Recommended resources

Introduction

Welcome back! In our last lesson, we established a crucial decision rule: based on the study's sampling scheme, we formulate our null hypothesis as either a test of homogeneity (for two or more samples) or a test of independence (for a single sample). This sets the stage for the actual analysis.

Today, we'll learn how to quantify the evidence against that null hypothesis. Your goal is to master the calculation of two key components: expected frequencies and the Pearson chi-square () statistic. The chi-square statistic measures the discrepancy between the data you observed in your sample and the data you would expect to see if the null hypothesis of "no relationship" or "no difference" were perfectly true.

A key takeaway for your exam preparation is that the calculation process is identical for both tests of homogeneity and independence. Once the contingency table is set up, the mechanics are the same.


Step 1: Understanding and Calculating Expected Frequencies

Before we can measure how far our observed data is from the null hypothesis, we first need to figure out what the world would look like if the null hypothesis were true. This is the concept of expected frequencies.

The expected frequency for a cell, often denoted , is the count we would expect to see in that cell if there were absolutely no association between the row and column variables (for independence) or if the proportions were perfectly equal across groups (for homogeneity).

Intuition Behind the Formula

How do we calculate this? The logic is rooted in basic probability. If two variables are independent, the probability of them occurring together is the product of their individual probabilities.

Let's watch a short video that provides a great intuitive explanation before introducing the formula.

Chi-squared Test for Independence! Extensive video!

This video from 'zedstatistics' uses a simple analogy to explain why the expected frequency formula makes sense, connecting it to the concept of independence you already know.

Watch from 05:01 to 08:41. Focus on how the idea of multiplying probabilities for independent events (like a dice roll and a coin toss) leads directly to the formula for calculating expected frequencies in a contingency table.

The Formula for Expected Frequencies

As the video explained, the intuition of multiplying probabilities leads to a simple and powerful formula that works for every cell in your table:

Using more formal notation, which you may see on your exam, this is written as:

where:

  • is the total for row .
  • is the total for column .
  • is the grand total number of observations.

Walkthrough: Calculating Expected Frequencies

Now, let's watch a clear, step-by-step demonstration of these calculations.

Test of Independence Using Chi-Square Distribution

The video 'Test of Independence Using Chi-Square Distribution' by The Organic Chemistry Tutor provides a very direct, procedural guide to this calculation. We will use it to see the formula in action.

First, watch from 00:23 to 01:25 to see how the row and column totals are calculated. Then, watch carefully from 03:59 to 08:28 as the presenter calculates the expected value for each of the six cells using the formula we just learned.

After watching, you should be comfortable taking any contingency table, calculating the row and column totals, and then using the formula to produce a corresponding table of expected frequencies.


Step 2: Calculating the Pearson Chi-Square Statistic ()

Now that we have both our observed frequencies () and our expected frequencies (), we can calculate the Pearson chi-square statistic. This value summarizes the total difference between our observed and expected tables.

The formula is:

What the formula does:

  1. : For each cell, it finds the difference between what you saw and what you expected.
  2. : It squares this difference to make all values positive and to penalize larger differences more heavily.
  3. : It "standardizes" this squared difference by dividing by the expected frequency. This ensures that a difference of 10 is more significant in a cell where you only expected 20 counts than in a cell where you expected 200.
  4. : It sums up these values from all the cells to get a single, overall measure of discrepancy.

A value of 0 means the observed and expected tables are identical. The larger the value, the more evidence you have against the null hypothesis.

The image below provides a perfect visual summary of this entire workflow.

Calculating Chi-Square Test Statistic for Contingency Tables
This diagram shows the complete process for calculating the chi-square statistic. It begins with the OBSERVED data, uses row and column totals to calculate the EXPECTED data, computes the (O-E)²/E component for each cell, and finally sums these components to arrive at the chi-square value.

Walkthrough: Calculating the Statistic

Let's return to the video example to see the final step performed.

Test of Independence Using Chi-Square Distribution

Continuing with the same example, the presenter now calculates the chi-square statistic using the observed and expected values.

Watch from 08:28 to 11:47. Follow along as each cell's contribution to the chi-square statistic is calculated and then summed to get the final result.

For an alternative perspective, a well-structured text guide can be very helpful for reinforcing these procedural steps.

Chi-Square Test of Independence | Formula, Guide & Examples

This guide from Scribbr, 'Chi-Square Test of Independence', breaks down the same calculation into a clear, five-step table. This format is excellent for building a repeatable process for your exam.

Read the section 'How to calculate the test statistic (formula).' Follow the five steps, paying close attention to the tables in each step. They clearly show how to calculate 'O-E', '(O-E)²', and finally '(O-E)² / E' for each cell before summing them up.


Complete Worked Example

Let's solidify this by applying the entire process to a new problem from start to finish. This example comes from a study comparing unnecessary blood transfusions between experienced attending physicians and residents in training. Since the researchers started with two fixed groups (49 attending physicians, 71 residents), this is a test of homogeneity.

The observed data is as follows:

Physician Frequent Occasionally Rarely Never Total
Attending 2 3 31 13 49
Resident 15 28 23 5 71
Total 17 31 54 18 120

Now, let's see the calculation of the expected frequencies and the final statistic.

Lesson 17: Contingency Tables - Statistics Online

The following resource from Penn State walks through the complete calculation for the physician data. This reinforces that the calculation method is the same, even though this is a test of homogeneity.

Read through 'Example 17-3.' First, examine the calculation of the expected counts. Then, look at the final calculation of the chi-square statistic, Q, which is another symbol for \chi^2. Note how the final value, 31.88, is the sum of the (O-E)²/E term for all eight cells.

This example confirms that no matter the hypothesis (homogeneity or independence), the computational steps remain the same:

  1. Start with the observed counts table.
  2. Calculate row, column, and grand totals.
  3. Compute the expected count for each cell.
  4. Compute the statistic by summing the components.

Conclusion

In this lesson, you have learned the essential mechanics of the chi-square test. You can now take a contingency table and compute the two most important quantities for the analysis: the expected frequencies and the Pearson chi-square statistic.

Key Takeaways:

  • Expected Frequencies (): These are the cell counts you would expect if the null hypothesis were true. They are calculated for each cell using the formula: .
  • Pearson Chi-Square Statistic (): This single number measures the overall difference between your observed data and what was expected under . It is calculated by summing across all cells.
  • A larger value signifies a greater discrepancy between observed and expected counts, suggesting stronger evidence against the null hypothesis.

You may be wondering: "How large does the chi-square statistic need to be for us to reject the null hypothesis?" Just calculating the statistic isn't enough to draw a conclusion. The answer to that question lies in our next lesson, where we will introduce the concept of degrees of freedom to find the all-important p-value.