Tutorials › AP Statistics › Degrees of Freedom for a Two-Way Table

Chi-square tests for categorical data · Tutorial 551 of 1000

Degrees of Freedom for a Two-Way Table

Use the table’s number of row and column categories to calculate its degrees of freedom and understand what that count represents.

Intermediate 9 min read

What You'll Learn

  • Identify the row and column category counts used in the degrees-of-freedom formula.
  • Calculate degrees of freedom for two-way tables of different sizes.
  • Explain how fixed margins restrict the cell counts that can vary independently.
  • Distinguish degrees of freedom from the number of cells and the sample size.
  • Check table dimensions carefully before choosing degrees of freedom.

How Table Size Determines Degrees of Freedom

In “The Chi-Square Statistic Formula,” you added the cell contributions \((O-E)^2/E\) to summarize how a two-way table’s observed counts differ from expected counts. To use a chi-square reference distribution for a test, we also need to know the table’s degrees of freedom. For a two-way table, that number depends on how many categories the two variables have—not on the number of people surveyed.

Count only the actual categories represented by rows and columns. Do not count a row or column labeled “Total.” If a table has \(r\) row categories and \(c\) column categories, its degrees of freedom are calculated with a simple formula.

Formula: For a two-way table with \(r\) row categories and \(c\) column categories, the degrees of freedom are
$$ df=(r-1)(c-1) $$
Here, \(r\) and \(c\) count categories, not totals. The formula applies to chi-square tests using a two-way table, including tests of independence and homogeneity.

For example, a table with 3 row categories and 4 column categories has \(df=(3-1)(4-1)=2\times3=6\). The key is to identify the table dimensions accurately before doing the arithmetic.

What the Degrees of Freedom Represent

Degrees of freedom describe how many independent pieces of information about the table’s discrepancies from expected counts can vary. The cell counts are connected by the table margins: the row totals, column totals, and grand total. Because these totals constrain the cells, not every cell discrepancy can change independently.

The relationship is visible if you imagine the margins held fixed. Once enough cell counts have been chosen, the remaining counts must take particular values to preserve those margins. For a table with \(r\) rows and \(c\) columns, the counts in the first \(r-1\) rows and first \(c-1\) columns can be thought of as the freely chosen interior block. The remaining cells are constrained by the row and column totals.

That block has \((r-1)(c-1)\) cells. This gives an intuitive way to understand the formula: it counts the number of cell values that can vary freely once the margins impose their constraints. It does not mean that only those cells matter in the chi-square statistic. As in the previous tutorial, the statistic still adds a contribution for every cell.

Definition: Degrees of freedom for a two-way table count the independent pieces of information available after accounting for the constraints among the cell counts. In a chi-square test, the table’s degrees of freedom help specify which chi-square reference distribution is used.

A second way to see the constraints is to consider the differences between observed and expected counts. For each row, those differences must add to zero because the expected counts preserve the row total. The differences also add to zero down each column because the expected counts preserve the column totals. These connected row and column requirements mean the cell discrepancies cannot all be freely selected.

The formula gives the count of independent discrepancies without requiring you to list the constraints one at a time. In the next tutorial, “Shape of Chi-Square Distributions,” you will study the reference distributions themselves. For now, the essential task is to calculate the correct degrees of freedom from the table dimensions.

Worked Example: A Two-by-Three Table

Worked Example: A Two-by-Three Table

A fictional recreation center records the preferred activity of visitors in two time slots. The activity variable has three categories. The table includes totals to help organize the counts, but those totals are not categories.

Time slotSwimmingClimbingFitness roomTotal
Morning18122050
Evening22282070
Total404040120

There are 2 row categories: morning and evening. There are 3 column categories: swimming, climbing, and fitness room. Do not count the “Total” row or “Total” column. Therefore, \(r=2\) and \(c=3\).

$$ df=(r-1)(c-1)=(2-1)(3-1)=1\times2=2 $$

The table has 6 interior cells, but its degrees of freedom are 2. One way to picture the constraints is to choose the counts in the morning row for swimming and climbing. The morning fitness-room count is then determined by the morning total. Once the column totals are also preserved, the corresponding evening counts are determined as well. The full table has more than two cells, but only two independent values are needed to determine the others when the margins are fixed.

Answer: The degrees of freedom are 2. This number describes independent information in the table’s cell discrepancies; it is not the number of cells or the number of visitors.

Worked Example: A Three-by-Four Table

Worked Example: A Three-by-Four Table

A fictional technology survey classifies respondents by device type and by the main way they receive local news. Device type has three categories, and news source has four.

Device typeNews appWebsiteEmailPrint
Phone3418226
Tablet16142010
Computer12261814

There are 3 device categories, so \(r=3\). There are 4 news-source categories, so \(c=4\). The table has \(3\times4=12\) interior cells, but that cell count is not the degrees of freedom.

$$ df=(3-1)(4-1)=2\times3=6 $$

The independent block can be pictured as the first two rows and first three columns, which contain \(2\times3=6\) cells. If the margins are accounted for, the remaining cells are constrained by those totals. That is why the degrees of freedom are 6 rather than 12.

Answer: This three-by-four table has 6 degrees of freedom. The actual counts are useful for a chi-square calculation, but the dimensions alone determine the degrees of freedom.

Worked Example: Comparing Tables With Different Dimensions

Worked Example: Comparing Tables With Different Dimensions

Two fictional surveys organize preferences in different ways. The first compares two delivery options across five neighborhoods. The second compares four types of garden with two watering methods. Their tables have different dimensions, so calculate each table’s degrees of freedom separately.

NeighborhoodLockerHome delivery
North1218
Central1515
East1020
South1713
West1416

The delivery table has 5 row categories and 2 column categories. Its degrees of freedom are

$$ df=(5-1)(2-1)=4\times1=4 $$
Garden typeDrip wateringSprinkler
Vegetable219
Native plants1812
Herbs246
Flowers1515

The garden table has 4 row categories and 2 column categories. Its degrees of freedom are

$$ df=(4-1)(2-1)=3\times1=3 $$

The delivery table has \(5\times2=10\) interior cells and 4 degrees of freedom. The garden table has \(4\times2=8\) interior cells and 3 degrees of freedom. The tables do not have the same number of cells, and their degrees of freedom are not the same.

Answer: The delivery table has 4 degrees of freedom, and the garden table has 3. Count the categories in each dimension first, then apply the formula. Do not infer degrees of freedom from a rough visual impression of table size.

Patterns That Make the Formula Easier to Check

When one variable has only two categories, its factor in the formula is \(2-1=1\). The degrees of freedom then equal one less than the number of categories in the other variable. A two-by-five table, for example, has \((2-1)(5-1)=4\) degrees of freedom. This shortcut follows directly from the formula and can help you check your arithmetic.

For a two-by-two table, \(df=(2-1)(2-1)=1\). Adding categories generally increases the number of degrees of freedom because there are more independent pieces of information in the table. The exact increase depends on both dimensions, so use the formula rather than trying to estimate the result.

The degrees of freedom are always a whole number for a table with at least two categories in each variable. The value is positive in that setting. If your calculation gives zero or a negative value, recheck whether you counted categories correctly and excluded the totals.

The sample size does not appear in \((r-1)(c-1)\). A two-by-three table has 2 degrees of freedom whether it contains 30 observations or 3,000, provided the categories are the same. Sample size affects the observed and expected counts, but it does not change the degrees of freedom for a table with unchanged dimensions.

Common Mistakes and AP Exam Tip

  • Counting totals as categories: A “Total” row or column summarizes counts; it is not an additional category. Exclude it when finding \(r\) and \(c\).
  • Using \(rc\) as the degrees of freedom: \(rc\) is the number of interior cells. The formula is \((r-1)(c-1)\), which accounts for constraints among those cells.
  • Confusing degrees of freedom with sample size: The number of observations is not part of this calculation. Use the table dimensions.
  • Reversing the dimensions or miscounting a row: The formula is symmetric, so switching \(r\) and \(c\) does not change the answer, but the category counts must still be correct. Count each category once.
  • Assuming tables with similar-looking cell counts have the same degrees of freedom: Different dimensions can produce different degrees of freedom. Work from each table’s row and column categories.
AP Exam Tip: State the number of row categories and column categories before substituting. For example: “There are 4 row categories and 2 column categories, so \(df=(4-1)(2-1)=3\).” This makes the table dimensions and calculation easy to verify.
Key takeaway: For a two-way table with \(r\) row categories and \(c\) column categories, calculate \(df=(r-1)(c-1)\). The result counts independent information after the cell counts are constrained by the margins; it is not the total number of cells or observations.

Check Your Understanding

For each question, count categories rather than totals, then use the degrees-of-freedom formula.

  1. A table has 2 row categories and 4 column categories. What are its degrees of freedom?
  2. A two-way table has 3 rows of categories and 5 columns of categories. How many interior cells does it have, and what are its degrees of freedom?
  3. Why are the degrees of freedom for a two-by-two table 1 rather than 4?
  4. A two-by-three table summarizes 40 observations. If a new sample has 400 observations but the same two variables and categories, do the degrees of freedom change? Explain.
  5. A table has 4 row categories and 2 column categories. Show the calculation for its degrees of freedom, and explain why the total row and total column are not included.