Tutorials › Introduction to Statistics › Degrees of Freedom in Two-Way Tables

Chi-Square Tests of Independence and Homogeneity · Tutorial 827 of 996

Degrees of Freedom in Two-Way Tables

See how row and column totals constrain a two-way table and determine its chi-square degrees of freedom.

Intermediate 9 min read

What You'll Learn

  • Define degrees of freedom for counts in a two-way table with fixed margins.
  • Derive the \((r-1)(c-1)\) formula by identifying the independently chosen cells.
  • Explain why the row and column constraints are not all independent.
  • Connect constrained cell deviations to the chi-square degrees of freedom.
  • Calculate degrees of freedom for tables of different dimensions.
  • Recognize why the formula does not simply equal the number of cells.

Why Table Size Is Not the Same as Degrees of Freedom

A two-way table with several rows and columns contains many cell counts, but those counts cannot all vary freely once the row and column totals are specified. If one cell count changes while the margins stay fixed, other cells must change to compensate. Degrees of freedom describe how many counts can be chosen independently before the remaining counts are determined.

This constraint explains the degrees of freedom used for the chi-square test of independence or homogeneity. The formula depends on the table's number of rows and columns, not on the total number of observations. To see why, we can first treat the margins as fixed and count how many cell counts remain free.

Definition: The degrees of freedom of a two-way table with fixed margins are the number of cell counts that can be chosen independently while still allowing all row and column totals to remain unchanged. For an ordinary table with \(r\) rows and \(c\) columns, this number is \((r-1)(c-1)\).

How the Margins Constrain the Cells

Let \(n_{ij}\) denote the observed count in row \(i\) and column \(j\), where \(i=1,\ldots,r\) and \(j=1,\ldots,c\). Let \(R_i\) be the total in row \(i\), \(C_j\) the total in column \(j\), and \(N\) the grand total. The row and column totals satisfy \(\sum_i R_i=\sum_j C_j=N\).

If the margins are known, it is tempting to say that there are \(r+c\) constraints: one for each row and one for each column. But one of these constraints is redundant. Once all row totals and all but one column total are specified, the final column total must equal \(N\) minus the other column totals. Equivalently, the sum of all row totals and the sum of all column totals both equal the same grand total. There are therefore only \(r+c-1\) independent margin constraints.

Starting with \(rc\) cell counts and imposing \(r+c-1\) independent constraints leaves

$$ rc-(r+c-1)=rc-r-c+1=(r-1)(c-1) $$

independent cell counts. This arithmetic suggests the result; a direct construction makes the constraints concrete.

A Direct Derivation

Imagine filling a table with its row and column totals already specified. Choose the counts in the first \(r-1\) rows and first \(c-1\) columns. There are \((r-1)(c-1)\) such cells. For each of those rows, its last cell is forced by that row's total. For each of the first \(c-1\) columns, its last-row cell is forced by that column's total. The bottom-right cell is then forced by the last row or the last column; the two calculations agree because both margins sum to \(N\).

1
Choose the interior counts.
Choose values for the cells in the first \(r-1\) rows and first \(c-1\) columns. These are the only counts selected freely in this construction.
2
Complete the first \(r-1\) rows.
In each such row, subtract the chosen counts from its row total to find the remaining cell in the last column.
3
Complete the first \(c-1\) columns.
In each such column, subtract the chosen counts from its column total to find the remaining cell in the last row.
4
Complete the last cell.
The bottom-right count is the last row total minus its other cells. It also equals the last column total minus its other cells, because the margins have the same grand total.

This construction gives \((r-1)(c-1)\) freely chosen counts. It also explains the equivalent constraint-counting argument: of the \(r+c\) row and column totals, one is redundant, leaving \(r+c-1\) independent constraints on the \(rc\) cells.

Worked Example: A 2-by-2 Table Has One Free Count

Suppose a hypothetical table has row totals 24 and 16, and column totals 18 and 22. The grand total is 40. Choose the top-left cell count to be 10. Find the other three cells.

The first row totals 24, so its second cell must be \(24-10=14\). The first column totals 18, so the bottom-left cell must be \(18-10=8\). The second row totals 16, so the bottom-right cell is \(16-8=8\). The completed table is:

Column 1Column 2Row total
Row 1101424
Row 28816
Column total182240

Only one cell count was chosen freely. Once the margins and that count were given, all other cells followed. The formula agrees: \((2-1)(2-1)=1\) degree of freedom.

Worked Example: A 3-by-2 Table Has Two Free Counts

Consider a hypothetical table with row totals 20, 15, and 25, and column totals 32 and 28. Its grand total is 60. Choose the first-column counts for the first two rows to be 12 and 7. Complete the table.

The first row's second cell is \(20-12=8\), and the second row's second cell is \(15-7=8\). The third row's first-column count must make that column total 32:

$$ 32-12-7=13 $$

The third row's second cell is \(25-13=12\). The completed table checks against the second column total, since \(8+8+12=28\).

Column 1Column 2Row total
Row 112820
Row 27815
Row 3131225
Column total322860

Two counts were selected independently, and the rest were determined by the margins. Thus \(df=(3-1)(2-1)=2\).

Worked Example: A 3-by-3 Table Has Four Free Counts

Suppose a hypothetical 3-by-3 table has row totals 30, 25, and 20, and column totals 25, 25, and 25. Choose the four counts in the first two rows and first two columns as 10, 9, 8, and 7, in row order. Complete the table.

The first row's last cell is \(30-10-9=11\). The second row's last cell is \(25-8-7=10\). The last row's first cell is \(25-10-8=7\), using the first-column total. Its second cell is \(25-9-7=9\), using the second-column total. The final cell is \(20-7-9=4\), which also completes the third column because \(11+10+4=25\).

Column 1Column 2Column 3Row total
Row 11091130
Row 2871025
Row 379420
Column total25252575

The first two rows and first two columns contain four freely chosen counts. All five remaining cells are fixed by the margins. The formula gives \((3-1)(3-1)=4\) degrees of freedom.

Why This Count Is Used in a Chi-Square Test

The fixed-margin argument describes the degrees of freedom for the table's deviations from the pattern expected under independence. For a table with observed counts \(n_{ij}\), the expected count under independence is \(E_{ij}=R_iC_j/N\). These expected counts have the same row and column totals as the observed counts.

Consequently, the cell-by-cell differences between observed and expected counts must sum to zero across every row and every column. For example, the deviations in row \(i\) sum to zero because the observed row total is \(R_i\), and the expected counts in that row also sum to \(R_i\). The same reasoning applies to each column. There are \(rc\) deviations subject to \(r+c-1\) independent margin constraints, so their number of independent contrasts is \((r-1)(c-1)\).

For the usual chi-square test of independence or homogeneity, this is the degrees-of-freedom value used for the chi-square approximation under the null hypothesis. The approximation is a large-sample result: it applies when the sampling design and count conditions make the chi-square approximation appropriate. It does not mean that the observed cell deviations can be freely assigned any real values; counts must also be possible for the study and margins. The formula counts independent dimensions of variation, not every numerical table that can be written down.

The result applies to an ordinary rectangular table in which every row-column combination is possible. If some combinations are structurally impossible, the available cells and constraints differ, so the usual formula cannot automatically be applied. Also, a table's degrees of freedom do not change merely because its sample size increases: a 3-by-2 table has two degrees of freedom whether it summarizes 60 observations or 6,000.

Common Mistake: Counting Cells Instead of Free Counts

A common error is to say that a 3-by-4 table has 12 degrees of freedom because it contains 12 cells. That reasoning ignores the margins. Once the row and column totals are set, most of those cell counts are determined by the others. The correct calculation is \((3-1)(4-1)=6\), not 12.

Another tempting calculation is to subtract all \(r+c\) margins from \(rc\), yielding \(rc-r-c\). That subtracts one constraint too many. One margin is redundant because the row totals and column totals both add to \(N\); the correct subtraction is \(rc-(r+c-1)\).

Key takeaway: With fixed row and column totals, an ordinary \(r\)-by-\(c\) table has \((r-1)(c-1)\) independent cell counts. For chi-square tests of independence and homogeneity, this is the degrees of freedom used by the standard chi-square approximation.

Check Your Understanding

Use the fixed-margin idea to answer these questions.

  1. How many degrees of freedom does a 4-by-2 table have? Briefly explain how many counts can be chosen before the margins determine the rest.
  2. A 3-by-5 table contains 15 cells. Why are its degrees of freedom fewer than 15, and what is the correct value?
  3. In counting constraints, why do the row and column totals provide \(r+c-1\), rather than \(r+c\), independent constraints?
  4. A 2-by-6 table is enlarged from 80 observations to 800 observations, but the same categories are used. Does its degrees of freedom change? Explain.
  5. Why can the usual \((r-1)(c-1)\) formula not automatically be used if some cells represent combinations that are structurally impossible?