Skip to main content

Section 9.3 Contingency Tables
 1 
This section is adapted from David M. Lane. "Contingency Tables." Online Statistics Education: A Multimedia Course of Study. https://onlinestatbook.com/2/chi_square/contingency.html

This section shows how to use Chi Square to test the relationship between qualitative variables for significance. For example, Table 9.3.1 shows the data from the Mediterranean Diet and Health case study,
 2 
in which 605 survivors of a heart attack were randomly assigned to follow either (1) a low-fat diet similar to one recommended at the time by the American Heart Association (AHA) or (2) a Mediterranean-type diet rich in vegetables, fruits, and grains.
Table 9.3.1. Frequencies for Diet and Health Study.
Outcome
Diet Cancers Fatal Heart Disease Non-Fatal Heart Disease Healthy Total
Total 22 38 33 512 605
AHA 15 24 25 239 303
Mediterranean 7 14 8 273 302
The question is whether there is a significant relationship between diet and outcome. Again, software can calculate a p-value for us in order to test for significance. But if we are wondering what’s going on under the hood, the first step is to compute the expected frequency for each cell based on the assumption that there is no relationship. These expected frequencies are computed from the totals as follows. We begin by computing the expected frequency for the AHA Diet/Cancers combination. Note that 22/605 subjects developed cancer. The proportion who developed cancer is therefore 0.0364. If there were no relationship between diet and outcome, then we would expect 0.0364 of those on the AHA diet to develop cancer. Since 303 subjects were on the AHA diet, we would expect (0.0364)(303) = 11.02 cancers on the AHA diet. Similarly, we would expect (0.0364)(302) = 10.98 cancers on the Mediterranean diet. In general, the expected frequency for a cell in the \(i\)th row and the \(j\)th column is equal to
\begin{equation*} E_{ij}=\frac{T_iT_j}{T} \end{equation*}
where \(E_{ij}\) is the expected frequency for cell \(i,j\text{,}\) \(T_i\) is the total for the \(i\)th row, \(T_j\) is the total for the \(j\)th column, and \(T\) is the total number of observations. For the AHA Diet/Cancers cell, \(i = 1\text{,}\) \(j = 1\text{,}\) \(T_i = 303\text{,}\) \(T_j = 22\text{,}\) and \(T = 605\text{.}\) Table 9.3.2 shows the expected frequencies (in parenthesis) for each cell in the experiment.
The significance test is conducted by computing Chi Square as follows.
\begin{equation*} \chi^2_3=\sum\frac{(E-O)^2}{E}=16.55 \end{equation*}
The degrees of freedom is equal to \((r-1)(c-1)\text{,}\) where \(r\) is the number of rows and \(c\) is the number of columns. For this example, the degrees of freedom is \((2-1)(4-1) = 3\text{.}\) The Chi Square calculator
 3 
can be used to determine that the probability value for a Chi Square of 16.55 with three degrees of freedom is equal to 0.0009. Therefore, the null hypothesis of no relationship between diet and outcome can be rejected.
Table 9.3.2. Observed and Expected Frequencies for Diet and Health Study.
Outcome
Diet Cancers Fatal Heart Disease Non-Fatal Heart Disease Healthy
AHA 15
(11.02)
24
(19.03)
25
(16.53)
239
(256.42)
Mediterranean 7
(10.98)
14
(18.97)
8
(16.47)
273
(255.58)
Total 22 38 33 512

Subsection 9.3.1 Drawing Substantive Conclusions from a Contingency Table
 4 
This subsection is written by Nathan Favero.

Now that we know there is a statistically significant relationship between diet and health outcome (since we rejected the null hypothesis of no relationship), we will naturally wonder what kind of relationship exists. Specifically, which diet is associated with better health? To answer this, we must look to the specific values within the cells and describe the patterns we observe. One straightforward way to do this is to make note of where the observed frequency differs notably from the expected frequency.
From Table 9.3.2, we can see that for those on the AHA diet, the frequencies for cancers, fatal heart disease, and non-fatal heart disease are all higher than expected. At the same time, the frequency for “healthy” is lower than expected on the AHA diet. Results for the Mediterranean diet are the exact opposite: cancers and both types of heart disease occur less frequently than expected, and the healthy outcome occurs more than expected. Thus, the Mediterranean diet is unambiguously associated with better outcomes than the AHA diet.
Though it is easy to make sense of results in this manner based on a comparison of observe to expected frequencies, the typical contingency table you encounter will probably not display expected frequencies. Instead, it is common to include percentages by row or by column, which can also be used to draw conclusions about the nature of a relationship between variables. For example, in Table 9.3.3, the expected frequencies from the prior table have been replaced by percentages. The percentages shown in this table are calculated by row, meaning that they are found by dividing the frequency in each cell by the total for that row, shown in the final column.
Table 9.3.3. Frequencies and Percentages by Row for Diet and Health Study.
Outcome
Diet Cancers Fatal Heart Disease Non-Fatal Heart Disease Healthy Total
AHA 15
4.95%
24
7.92%
25
8.25%
239
78.88%
303
100%
Mediterranean 7
2.32%
14
4.64%
8
2.65%
273
90.40%
302
100%
Total 22
3.64%
38
6.28%
33
5.45%
512
84.63%
605
100%
Drawing conclusions based on column or row percentages takes some care, and it is easy to make mistakes if you are not careful. For example, consider the AHA/Cancers cell, which shows 4.95%. Because this seems like a small percentage, it is tempting to conclude that being on the AHA diet is not associated with having cancer. However, that conclusion would be wrong. Since the percentages have been calculated by row, the key to finding meaningful patterns will be to compare across the percentages found within the same column. In this case, we see that the 4.95% in the AHA-Cancers cell is over twice as large as the 2.32% shown in the Mediterranean/Cancers cell right below it. To make sense of these percentages, remember that we have calculated percentages by row, meaning that the frequency in the cell is divided by the total at the far right of the table. So the 4.95% means that among all those on the AHA diet (303 individuals in total), 4.95% have cancer. By comparison, only around 2% (2.32% to be precise) of those on a Mediterranean diet have cancer. Both percentages are small, because cancer is (fortunately) a relatively rare outcome; at the bottom of the table we see that only 3.64% of participants overall have cancer. Still, since the percentage is higher among those on the AHA diet than on the Mediterranean diet, we can say that the AHA diet is positively associated with having cancer.
If we continue moving down the table column by column, we see that rates of heart disease are higher among those on the AHA diet (7.92% for fatal and 8.25% for non-fatal) than among those on the Mediterranean diet (4.64% and 2.65%). Finally, around 90% of those on the Mediterranean diet are healthy, while just under 79% of those on the AHA diet are.
We can reach an equivalent conclusion by examining percentages that are calculated by column, as shown in Table 9.3.4. Here, percentages are calculated by dividing the frequency in each cell by the total shown at the bottom of the column.
Table 9.3.4. Frequencies and Percentages by Column for Diet and Health Study.
Outcome
Diet Cancers Fatal Heart Disease Non-Fatal Heart Disease Healthy Total
AHA 15
68.18%
24
63.16%
25
75.76%
239
46.68%
303
50.08%
Mediterranean 7
31.82%
14
36.84%
8
24.24%
273
53.32%
302
49.92%
Total 22
100%
38
100%
33
100%
512
100%
605
100%
The AHA/Cancers cell shows 68.18%, indicating that around 68% of those who have cancer are on the AHA diet. In the case of this particular dataset, the split of participants between the two diets is approximately 50-50 (as shown by the percentages in the final column), so finding that the percentage of those with cancer who are on the AHA diet is well above 50% actually does indicate that individuals on the AHA diet are overrepresented among those with cancer. But remember, you cannot generally assume that 50% is a reasonable baseline for comparison; if, for example, 80% of study participants were on the AHA diet, seeing any percentage smaller than 80% for the AHA-Cancers would indicate that those on the AHA diet were underrepresented among those with cancer.
As a general rule, when percentages are calculated by columns, the key to finding meaningful patterns is making comparisons across the columns (within the same row), such as noticing that the 68.18% in the AHA-Cancers cell is greater than the 50.08% in the AHA Total cell. One can also see that while people on the AHA diet make up a clear majority of those with heart disease (both fatal and non-fatal), among those who are healthy only around 47% are on the AHA diet. As a clear contrast, those on the Mediterranean diet make up around 53% of the healthy subjects but just 32% of those with cancer, 37% of those with fatal heart disease, and 24% of those with non-fatal heart disease. Thus, we see consistent indication that outcomes are better for subjects on the Mediterranean diet, as opposed to the AHA one.
As you can now see, regardless of whether we examine (1) expected versus observed outcomes, (2) percentages by row, or (3) percentages by column, we reach the same conclusion: health outcomes are uniformly better for those on the Mediterranean diet. All three approaches are equally valid ways of evaluating associations from a contingency table, and you normally need use only one. We examined all three here for illustrative purposes since each approach is one you may encounter in your own analysis or in a research report.

Subsection 9.3.2 Key Assumption of the Chi Square Test

A key assumption of this Chi Square test is that each subject contributes data to only one cell. Therefore, the sum of all cell frequencies in the table must be the same as the number of subjects in the experiment. Consider an experiment in which each of 16 subjects attempted two anagram problems. The data are shown in Table 9.3.5.
Table 9.3.5. Anagram Problem Data.
Anagram 1 Anagram 2
Solved 10 4
Did not Solve 6 12
It would not be valid to use the Chi Square test on these data since each subject contributed data to two cells: one cell based on their performance on Anagram 1 and one cell based on their performance on Anagram 2. The total of the cell frequencies in the table is 32, but the total number of subjects is only 16.