Contingency Tables and Two-Way Tables: How to Build, Read, and Test for Association
A practical guide to contingency tables (two-way tables) โ covering how to construct them from raw data, how to calculate row and column percentages, how to test for association between two categorical variables using the chi-square test, and the common mistakes students make when interpreting two-way tables.
What You'll Learn
- โConstruct a contingency table from raw categorical data
- โCalculate row percentages, column percentages, and marginal totals
- โCompute expected frequencies and the chi-square test statistic
- โInterpret the chi-square test result to determine whether two variables are associated
1. The Direct Answer: Contingency Tables Show How Two Categorical Variables Relate
A contingency table (also called a two-way table, cross-tabulation, or crosstab) displays the frequency counts for every combination of two categorical variables. It is the fundamental tool for examining the relationship between two categorical variables. **Example**: you survey 200 people about their exercise frequency (Regular, Occasional, Never) and their self-reported health (Good, Fair, Poor). A contingency table shows how many people fall into each combination: | | Good | Fair | Poor | Total | |---|---|---|---|---| | Regular | 60 | 20 | 5 | 85 | | Occasional | 30 | 25 | 10 | 65 | | Never | 10 | 20 | 20 | 50 | | Total | 100 | 65 | 35 | 200 | Each cell contains the count of people with that specific combination. The row totals (85, 65, 50) are called ROW MARGINALS โ they show the total for each exercise category regardless of health. The column totals (100, 65, 35) are COLUMN MARGINALS โ they show the total for each health category regardless of exercise. The table immediately reveals patterns: regular exercisers are concentrated in the "Good" health column (60/85 = 71%), while non-exercisers are spread more evenly, with a heavy concentration in "Poor" (20/50 = 40%). This pattern SUGGESTS an association between exercise and health, but we need a statistical test to confirm it is not due to chance. **Terminology**: - **Rows**: one categorical variable (here, exercise frequency) - **Columns**: the other categorical variable (here, health status) - **Cells**: the intersections (each specific combination) - **Marginals**: row and column totals - **Grand total**: the total sample size (200) - **Dimensions**: described as r ร c (rows ร columns). The example is a 3 ร 3 table. A table comparing gender (M/F) and voting preference (Yes/No) would be 2 ร 2. **When to use a contingency table**: whenever you have two categorical variables and want to examine their relationship. Common examples: gender vs. preference, treatment vs. outcome, education level vs. income bracket, region vs. political affiliation. Snap a photo of any contingency table problem and StatsIQ identifies the variables, calculates percentages, performs the appropriate test, and interprets the result.
Key Points
- โขContingency table = frequency counts for every combination of two categorical variables.
- โขRow marginals = row totals. Column marginals = column totals. Grand total = sample size.
- โขDescribed as r ร c (rows ร columns). A 2ร2 table is the simplest; 3ร3 and larger are common.
- โขUse when both variables are categorical. The table reveals patterns; the chi-square test confirms them.
2. Percentages: Row, Column, and Total โ Which One to Use
Raw counts in a contingency table are hard to interpret when row or column totals differ. Percentages make comparisons easier โ but you must choose the RIGHT type of percentage for your question. **Row percentages**: divide each cell by its ROW total. Use row percentages when you want to compare the distribution of the COLUMN variable across different ROW categories. Using the exercise/health example: | | Good | Fair | Poor | |---|---|---|---| | Regular | 60/85 = 70.6% | 20/85 = 23.5% | 5/85 = 5.9% | | Occasional | 30/65 = 46.2% | 25/65 = 38.5% | 10/65 = 15.4% | | Never | 10/50 = 20.0% | 20/50 = 40.0% | 20/50 = 40.0% | Interpretation: among regular exercisers, 70.6% report good health. Among non-exercisers, only 20.0% report good health. Row percentages answer: "given this exercise category, what is the distribution of health outcomes?" **Column percentages**: divide each cell by its COLUMN total. Use column percentages when you want to compare the distribution of the ROW variable across different COLUMN categories. | | Good | Fair | Poor | |---|---|---|---| | Regular | 60/100 = 60.0% | 20/65 = 30.8% | 5/35 = 14.3% | | Occasional | 30/100 = 30.0% | 25/65 = 38.5% | 10/35 = 28.6% | | Never | 10/100 = 10.0% | 20/65 = 30.8% | 20/35 = 57.1% | Interpretation: among people with good health, 60.0% are regular exercisers. Among people with poor health, 57.1% never exercise. Column percentages answer: "given this health category, what is the distribution of exercise habits?" **Total percentages**: divide each cell by the GRAND total. Shows what proportion of the ENTIRE sample falls in each cell. Less useful for comparison but shows the overall distribution. **Which to use**: the rule depends on which variable you consider the "explanatory" or "independent" variable: - If exercise is the explanatory variable (you think exercise affects health): use ROW percentages (percentages within exercise categories). Compare across rows. - If health is the explanatory variable: use COLUMN percentages. - If neither is clearly explanatory: either works, but be consistent. **The comparison rule**: whatever type of percentage you choose, COMPARE ACROSS the other dimension. If you compute row percentages, compare them across rows (down each column). If you compute column percentages, compare them across columns (along each row). **Common mistake**: computing row percentages and then comparing across columns within the same row. This is circular โ row percentages within a row must sum to 100%, so of course they differ. The meaningful comparison is between rows. StatsIQ calculates both row and column percentages and identifies which comparison is appropriate based on the structure of the problem.
Key Points
- โขRow percentages: cell / row total. Use when the row variable is explanatory.
- โขColumn percentages: cell / column total. Use when the column variable is explanatory.
- โขCompare across the dimension you DIDN'T percentage. Row %s โ compare down columns.
- โขChoose based on which variable is explanatory. If unsure, either works but be consistent.
3. The Chi-Square Test of Independence: Are the Variables Associated?
A contingency table shows patterns, but patterns can arise by chance. The chi-square test of independence determines whether the association you see in the table is statistically significant or could have occurred by random sampling variation alone. **The hypothesis setup**: - Hโ (null): the two variables are INDEPENDENT (no association). The distribution of one variable is the same regardless of the other. - Hโ (alternative): the two variables are NOT independent (there IS an association). **Step 1: Calculate expected frequencies.** If the variables were truly independent, the expected count in each cell would be: Expected = (Row Total ร Column Total) / Grand Total For the exercise/health example: - Expected count for (Regular, Good) = (85 ร 100) / 200 = 42.5 - Expected count for (Regular, Fair) = (85 ร 65) / 200 = 27.625 - Expected count for (Regular, Poor) = (85 ร 35) / 200 = 14.875 - Expected count for (Occasional, Good) = (65 ร 100) / 200 = 32.5 - Expected count for (Never, Poor) = (50 ร 35) / 200 = 8.75 - ... and so on for all 9 cells. **Step 2: Calculate the chi-square statistic.** ฯยฒ = ฮฃ (Observed - Expected)ยฒ / Expected For each cell, compute (O - E)ยฒ/E and sum across all cells. Using our example (showing a few cells): - (Regular, Good): (60 - 42.5)ยฒ / 42.5 = (17.5)ยฒ / 42.5 = 306.25/42.5 = 7.206 - (Regular, Poor): (5 - 14.875)ยฒ / 14.875 = (-9.875)ยฒ / 14.875 = 97.516/14.875 = 6.556 - (Never, Good): (10 - 25.0)ยฒ / 25.0 = (-15.0)ยฒ / 25.0 = 225.0/25.0 = 9.000 - (Never, Poor): (20 - 8.75)ยฒ / 8.75 = (11.25)ยฒ / 8.75 = 126.56/8.75 = 14.464 Summing all 9 cells gives ฯยฒ โ 52.6 (the full calculation includes all 9 cells). **Step 3: Determine degrees of freedom.** df = (r - 1) ร (c - 1) For a 3 ร 3 table: df = (3-1) ร (3-1) = 4. **Step 4: Find the p-value.** Compare ฯยฒ to the chi-square distribution with 4 degrees of freedom. ฯยฒ = 52.6 with df = 4 gives a p-value far less than 0.001. **Step 5: Conclusion.** Since p < 0.05 (and in fact p < 0.001), we reject the null hypothesis. There IS a statistically significant association between exercise frequency and self-reported health. The pattern we observed in the table โ regular exercisers tend to report better health โ is not due to chance alone. **Important caveat**: association does not mean causation. The chi-square test tells you the variables are related, not that one causes the other. People who exercise regularly may report better health because exercise improves health, but it could also be that healthier people are more likely to exercise (reverse causation) or that a third variable (income, education, age) drives both. **Assumptions of the chi-square test**: 1. Data must be frequency counts (not percentages or means). 2. Observations must be independent (each person counted once). 3. Expected frequency in each cell should be at least 5. If any expected count is < 5, use Fisher's exact test (for 2ร2 tables) or merge categories. StatsIQ performs the full chi-square test from any contingency table โ computing expected frequencies, the test statistic, degrees of freedom, p-value, and interpretation.
Key Points
- โขExpected frequency = (Row Total ร Column Total) / Grand Total. Calculate for every cell.
- โขฯยฒ = ฮฃ (Observed - Expected)ยฒ / Expected. Larger ฯยฒ = stronger evidence of association.
- โขDegrees of freedom = (r-1)(c-1). For a 3ร3 table: df = 4.
- โขAssumptions: counts (not %), independent observations, expected frequencies โฅ 5 in each cell.
4. Special Case: The 2ร2 Table and Practical Applications
The 2ร2 contingency table is the simplest and most common form. It compares two binary categorical variables. Many practical applications use 2ร2 tables: treatment vs. placebo combined with outcome vs. no outcome, exposed vs. unexposed combined with disease vs. no disease, male vs. female combined with yes vs. no. **The standard 2ร2 layout**: | | Outcome Yes | Outcome No | Total | |---|---|---|---| | Group A | a | b | a+b | | Group B | c | d | c+d | | Total | a+c | b+d | n | The cells are labeled a, b, c, d by convention, starting top-left and going clockwise. **Measures of association for 2ร2 tables**: Beyond just testing whether the association exists (chi-square), 2ร2 tables allow you to quantify the STRENGTH of the association: **Risk ratio (relative risk)**: RR = [a/(a+b)] / [c/(c+d)]. The ratio of the outcome rate in Group A vs. Group B. RR = 1 means no difference. RR > 1 means Group A has higher risk. Used in prospective studies and clinical trials. **Odds ratio**: OR = (a ร d) / (b ร c). The ratio of the odds of the outcome in Group A vs. Group B. OR โ RR when the outcome is rare (< 10%). Used in case-control studies and logistic regression. **Example**: a clinical trial tests a new drug vs. placebo for headache relief. | | Relief | No Relief | Total | |---|---|---|---| | Drug | 70 | 30 | 100 | | Placebo | 45 | 55 | 100 | | Total | 115 | 85 | 200 | Risk of relief: Drug = 70/100 = 70%. Placebo = 45/100 = 45%. Risk Ratio = 0.70/0.45 = 1.56. The drug group is 56% more likely to get relief. Odds Ratio = (70 ร 55) / (30 ร 45) = 3850/1350 = 2.85. Chi-square test: Expected for (Drug, Relief) = (100 ร 115)/200 = 57.5. ฯยฒ = (70-57.5)ยฒ/57.5 + (30-57.5)ยฒ/57.5 ... wait, that's not right. Let me recalculate properly: - (Drug, Relief): (70-57.5)ยฒ/57.5 = 156.25/57.5 = 2.717 - (Drug, No Relief): (30-42.5)ยฒ/42.5 = 156.25/42.5 = 3.676 - (Placebo, Relief): (45-57.5)ยฒ/57.5 = 156.25/57.5 = 2.717 - (Placebo, No Relief): (55-42.5)ยฒ/42.5 = 156.25/42.5 = 3.676 ฯยฒ = 2.717 + 3.676 + 2.717 + 3.676 = 12.786 df = (2-1)(2-1) = 1. Critical value at 95% for df=1 is 3.841. 12.786 >> 3.841, so p < 0.001. The association is statistically significant. **Yates correction**: for 2ร2 tables, some textbooks apply Yates' continuity correction, which subtracts 0.5 from each |O - E| before squaring. This makes the test slightly more conservative. The corrected formula: ฯยฒ = ฮฃ (|O - E| - 0.5)ยฒ / E. Whether to apply Yates' correction is debated โ many modern textbooks skip it. Use whatever your course requires. **Fisher's exact test**: when any expected cell count is less than 5 in a 2ร2 table, use Fisher's exact test instead of chi-square. Fisher's test calculates the exact probability of the observed table (and more extreme tables) under the null hypothesis, without relying on the chi-square approximation. Most statistical software performs this automatically. **Real-world applications of 2ร2 tables**: - Clinical trials: treatment vs. control ร outcome vs. no outcome - Epidemiology: exposure vs. no exposure ร disease vs. no disease - Marketing: ad campaign A vs. B ร purchased vs. did not purchase (A/B testing) - Quality control: production line A vs. B ร defective vs. not defective StatsIQ handles all 2ร2 table calculations including chi-square (with optional Yates correction), risk ratio, odds ratio, and Fisher's exact test when appropriate.
Key Points
- โข2ร2 tables use the standard a, b, c, d cell labeling. df always = 1.
- โขRisk Ratio = [a/(a+b)] / [c/(c+d)]. Odds Ratio = (ad)/(bc). Both measure association strength.
- โขUse Fisher's exact test when any expected cell count < 5. More reliable than chi-square for small samples.
- โขYates correction: |O-E| - 0.5 before squaring. Conservative adjustment for 2ร2 tables. Follow your course.
Key Takeaways
- โ Contingency table: frequency counts for two categorical variables. Row ร Column format.
- โ Expected frequency = (Row Total ร Column Total) / Grand Total. Calculate for every cell.
- โ Chi-square test: ฯยฒ = ฮฃ(O-E)ยฒ/E. df = (r-1)(c-1). Larger ฯยฒ = stronger evidence of association.
- โ Row percentages when the row variable is explanatory. Compare across rows (down columns).
- โ Assumptions: counts not percentages, independent observations, all expected frequencies โฅ 5.
Practice Questions
1. A 2ร2 table shows: Treatment group: 45 success, 15 failure. Control group: 30 success, 30 failure. Calculate the expected frequency for (Treatment, Success), the chi-square statistic, and determine if the association is significant at the 5% level.
2. A researcher computes row percentages from a contingency table and then compares the percentages across columns within the same row. What is wrong with this approach?
FAQs
Common questions about this topic
Use chi-square when all expected cell frequencies are at least 5. When any expected frequency is below 5, chi-square approximation becomes unreliable โ use Fisher's exact test instead. Fisher's is computationally feasible for 2ร2 tables and small rรc tables. For larger tables with some small expected counts, you can merge categories to eliminate cells with expected counts below 5. Most statistics software performs both tests and flags when Fisher's is preferred.
Yes. Snap a photo of any contingency table and StatsIQ calculates row percentages, column percentages, expected frequencies, the chi-square statistic, degrees of freedom, p-value, and provides a clear interpretation. For 2ร2 tables, it also calculates the risk ratio and odds ratio. It flags when Fisher's exact test should be used instead of chi-square.