14. Contingency Tables

Math 115 - Examples for Topic 14

Author

Yurk

These worked examples demonstrate inference for contingency tables using the chi-square test (model-based approach).


Example 1: Chi-Square Test for Independence

Scenario: A streaming service wants to understand whether music genre preferences differ by age group. They survey 480 subscribers and ask each person to identify their favorite music genre from three options: Pop, Rock, or Hip-Hop/R&B.

Survey results:

Age Group Pop Rock Hip-Hop/R&B Total
18-29 52 38 70 160
30-44 64 58 48 170
45+ 44 74 32 150
Total 160 170 150 480

The standardized bar plot below shows the same data: within each age group, the bar is split by the proportion preferring each genre.

Research question: Is there an association between age group and music genre preference?

Task: Conduct a chi-square test at the α = 0.05 level.

Step 1: State the hypotheses

  • \(H_0\): There is no association between age group and music genre preference (the variables are independent)
  • \(H_A\): There is an association between age group and music genre preference (the variables are not independent)

Step 2: Read the standardized bar plot

Before any expected counts, the standardized bar plot already shows the association the test will quantify — the Hip-Hop/R&B share shrinks steadily across the age groups while the Rock share grows. If the bars were (nearly) identical, the chi-square statistic would be small; visibly different bars are what large \(\chi^2\) values look like. The arithmetic below puts a number on how far these bars sit from “identical.”

Step 3: Set up the analysis in Jamovi

Frequencies → Independent Samples

  • Place Age Group in Rows
  • Place Genre in Columns

Step 4: Check conditions for chi-square test

Independence:

  • Random sample of subscribers ✓
  • Each subscriber’s response is independent of others ✓

Expected counts: We need at least 5 expected counts in each cell.

To view expected counts in Jamovi: expand the Cells menu and check Expected Counts.

Expected counts from Jamovi:

Age Group Pop Rock Hip-Hop/R&B
18-29 53.33 56.67 50
30-44 56.67 60.21 53.12
45+ 50 53.12 46.88

All expected counts are ≥ 5 ✓

Conditions are met for the chi-square test.

Step 5: Read the test results from Jamovi

Jamovi automatically computes the chi-square statistic, degrees of freedom, and p-value:

Statistic Value
\(X^2\) 29.35
df 4
p-value < 0.001

Step 6: Interpret the p-value

Interpretation: If there were truly no association between age group and music genre preference, there is less than a 0.1% chance of observing a chi-square statistic of 29.35 or larger.

Step 7: Make a decision

Using the significance level α = 0.05:

  • P-value < 0.001
  • Compare: Is p-value < 0.05? YES

Decision: We reject \(H_0\) at the α = 0.05 level.

Step 8: State the conclusion in context

The data provide convincing evidence of an association between age group and music genre preference (\(X^2 = 29.35\), df = 4, p-value < 0.001). Younger subscribers (18-29) show a stronger preference for Hip-Hop/R&B, while older subscribers (45+) show a stronger preference for Rock. This information could help the streaming service tailor its recommendations and marketing by age group.


Example 2: Computing Expected Counts and Chi-Square Contributions

Scenario: A university surveyed 600 students about their preferred study environment. Students were classified by year (Freshman, Sophomore, Junior, Senior) and asked to choose their preferred study location (Library, Dorm Room, Coffee Shop).

Survey results:

Year Library Dorm Room Coffee Shop Total
Freshman 42 68 40 150
Sophomore 48 52 50 150
Junior 55 45 50 150
Senior 65 35 50 150
Total 210 200 190 600

Part A: Computing Degrees of Freedom

Question: Calculate the degrees of freedom for the chi-square test using this contingency table.

Degrees of freedom formula:

\[df = (\text{rows} - 1) \times (\text{columns} - 1)\]

For this table:

  • Number of rows = 4 (Freshman, Sophomore, Junior, Senior)
  • Number of columns = 3 (Library, Dorm Room, Coffee Shop)

\[df = (4 - 1) \times (3 - 1) = 3 \times 2 = 6\]

Answer: The degrees of freedom for this chi-square test is df = 6.

Why this formula? Once we know the row totals, column totals, and grand total, only some cells can “vary freely.” In a 4×3 table, if we fill in any 6 cells (a 3×2 block), the remaining cells are determined by the marginal totals. This is why \(df = (r-1) \times (c-1)\).


Part B: Expected Count for Freshmen who prefer Dorm Room

Question: If there were no association between year and study location preference, how many Freshmen would we expect to prefer the Dorm Room?

Expected count formula:

\[\text{Expected} = \frac{\text{row total} \times \text{column total}}{\text{grand total}}\]

For Freshmen who prefer Dorm Room:

  • Row total (Freshman) = 150
  • Column total (Dorm Room) = 200
  • Grand total = 600

\[\text{Expected} = \frac{150 \times 200}{600} = \frac{3\times 10^{4}}{600} = 50\]

Answer: We would expect 50 Freshmen to prefer the Dorm Room if year and study location were independent.


Part C: Expected Count for Seniors who prefer Library

Question: If there were no association between year and study location preference, how many Seniors would we expect to prefer the Library?

For Seniors who prefer Library:

  • Row total (Senior) = 150
  • Column total (Library) = 210
  • Grand total = 600

\[\text{Expected} = \frac{150 \times 210}{600} = \frac{3.15\times 10^{4}}{600} = 52.5\]

Answer: We would expect 52.5 Seniors to prefer the Library if year and study location were independent.


Part D: Chi-Square Contribution for Freshmen/Dorm Room Cell

Question: Calculate the contribution to the chi-square statistic from the Freshmen/Dorm Room cell.

Chi-square contribution formula:

\[\frac{(\text{Observed} - \text{Expected})^2}{\text{Expected}}\]

For Freshmen who prefer Dorm Room:

  • Observed = 68
  • Expected = 50 (from Part B)

\[\frac{(68 - 50)^2}{50} = \frac{(18)^2}{50} = \frac{324}{50} = 6.48\]

Answer: The contribution to \(X^2\) from this cell is 6.48.

Interpretation: This cell contributes 6.48 to the overall chi-square statistic. Since the observed count (68) is higher than expected (50), Freshmen prefer the Dorm Room more than we’d expect if year and location were independent.


Part E: Chi-Square Contribution for Seniors/Library Cell

Question: Calculate the contribution to the chi-square statistic from the Seniors/Library cell.

For Seniors who prefer Library:

  • Observed = 65
  • Expected = 52.5 (from Part C)

\[\frac{(65 - 52.5)^2}{52.5} = \frac{(12.5)^2}{52.5} = \frac{156.25}{52.5} = 2.976\]

Answer: The contribution to \(X^2\) from this cell is 2.976.

Interpretation: This cell contributes 2.976 to the overall chi-square statistic. Since the observed count (65) is higher than expected (52.5), Seniors prefer the Library more than we’d expect if year and location were independent.


Note: Computing the Chi-Square Statistic

To compute the full chi-square statistic:

  1. Calculate expected counts for all 12 cells (4 rows × 3 columns) using the formula from Parts B and C
  2. Calculate the contribution \(\frac{(O - E)^2}{E}\) for each of the 12 cells
  3. Add all 12 contributions to get \(X^2\)

\[X^2 = \sum_{\text{all cells}} \frac{(O - E)^2}{E}\]

For this table, the complete chi-square statistic is \(X^2 = 18.72\).

Note: The two cells we calculated by hand contribute significantly to this total:

  • Freshmen/Dorm Room: 6.48
  • Seniors/Library: 2.976

Together, these two cells contribute 9.456 out of 18.72, which is about 50.5% of the total \(X^2\).

The largest contributions actually come from the Dorm Room column: Freshmen/Dorm (6.48) and Seniors/Dorm (4.5). Freshmen prefer the Dorm Room much more than expected, while Seniors prefer it much less than expected. This suggests that as students progress through college, their study location preferences shift away from the Dorm Room.


Summary: Key Formulas

Expected count: \[\text{Expected} = \frac{\text{row total} \times \text{column total}}{\text{grand total}}\]

Chi-square contribution (per cell): \[\frac{(\text{Observed} - \text{Expected})^2}{\text{Expected}}\]

Chi-square statistic: \[X^2 = \sum_{\text{all cells}} \frac{(O - E)^2}{E}\]

Degrees of freedom: \[df = (\text{rows} - 1) \times (\text{columns} - 1)\]

Back to top