Menu
Sign up Sign in

AI HL · 13.4

Hypothesis Testing

13.4.4

Chi-squared test for independence

13.4.4

Chi-squared test for independence

Target: I can state H0 and H1 for a chi-squared test for independence, naming both variables. (+5 more)

By the end of this section

Key points

The test and its hypotheses

  • A chi-squared test for independence tests whether two categorical variables are linked, using counts in a contingency table.
  • H0: the two variables are independent. H1: they are not independent. Name both variables, in context.
  • Use the counts, never percentages. Do not include the Total row or column in the table you enter.
  • Every expected frequency must be at least 5. If one is smaller, combine categories first.

Expected frequencies and degrees of freedom

  • If the variables are independent, the expected frequency of a cell is \(E=\dfrac{\text{row total}\times\text{column total}}{\text{grand total}}\).
  • The statistic is \(\chi^2=\sum\dfrac{(O-E)^2}{E}\). It is large when the observed values are far from the expected ones.
  • For a table with r rows and c columns, df = (r − 1)(c − 1). Count the rows and columns of data, not the totals.
  • To see which cells matter most, compare O with E in each cell. The cells with the biggest \((O-E)^2/E\) depart most from independence.

Concluding the test

  • Reject H0 if p < α, or if \(\chi^2\) is greater than the critical value. Otherwise do not reject H0.
  • Write the conclusion in context: "There is sufficient evidence, at the 5% significance level, that … is not independent of …"
  • If you do not reject H0, say "insufficient evidence". The test never proves that two variables are independent.
  • The same p-value can give different conclusions at different levels: p = 0.03 rejects H0 at 5% but not at 1%.

The chi-squared curve

  • The \(\chi^2\) curve starts at 0 and has a long tail to the right. Its shape depends on df.
  • Only the right tail is used. The p-value is the area to the right of the test statistic.
  • The critical value is the point with area α to its right. It gets larger as df gets larger.

On the calculator

Entering the observed table

  • Enter the observed counts as a matrix with the same rows and columns as the table.
  • Leave out the totals.
  • Check the number of rows and columns before you run the test.

Screenshot and video to come

The chi-squared test for independence

  • Choose the chi-squared test for independence and give it your observed matrix.
  • Read χ², the p-value and df.
  • Check that df = (rows − 1)(columns − 1).

Screenshot and video to come

The expected matrix

  • The test stores the expected frequencies in a second matrix.
  • Open it to read every expected frequency.
  • Check that none is below 5, then compare each with its observed value.

Screenshot and video to come

Critical value and p-value from the curve

  • For a p-value, find the area to the right of the test statistic for the right df.
  • For a critical value, use the inverse chi-squared with area 1 − α to the left.
  • Give 3 significant figures.

Screenshot and video to come

Explore: the chi-squared test for independence

Change the counts in the observed table and watch the expected counts, χ² and the conclusion change. Press an expected count to see how it is worked out.

Observed

Total
Total

Expected, if the two variables are independent

Each expected count also shows how much it adds to χ². The orange outline is the largest.

χ² (add the contributions)
Degrees of freedom
p-value

Change a count to start.

  1. In "Travel to school", which expected count is furthest from its observed count? Is that also the cell that adds most to χ²?
  2. Change the significance level from 5% to 1%. Does the conclusion change? What does that tell you about a p-value close to 0.05?
  3. In "Music", make the two rows more different, one count at a time. How far must they move before H₀ is rejected?
  4. In "Clubs", which expected count is below 5? Why can the test not be trusted, and what would you do before running it?

Explore: the p-value and the critical region

The p-value is an area under the curve of the test statistic, if H₀ is true. Choose the test and H₁, then drag the orange point to the value of the statistic. The gold area is α, over the critical region.

Significance level, α
Critical value
p-value

Drag the point with the mouse or a finger, or select it (Tab) and use the arrow keys. Type a value for an exact one.

Drag the point to choose the test statistic.

  1. Drag the statistic further from the middle. What happens to the p-value? Why is a statistic far out strong evidence against H₀?
  2. Press "Move to the critical value". What is the p-value? Compare it with α.
  3. Change H₁ from > to ≠ and keep the statistic. Why does the p-value double? Where is the other half of the area?
  4. In t, make the degrees of freedom large. Which curve does the t curve become? Compare the critical values of t and z.

Worked examples

Problems like the ones in class. Pick one, try it, then open the working.
One expected frequency by hand

A café asks 290 customers which hot drink they choose and which age group they are in. The results are shown. If the choice of drink is independent of age group, find the expected frequency of customers who choose coffee and are aged 30 to 50.

DrinkUnder 3030 to 50Over 50TotalTea223147100Coffee455218115Juice38241375Total10510778290
Working
  1. The totals

    Coffee row total = 115, 30 to 50 column total = 107, grand total = 290

    Take the totals from the edges of the table, not the cell itself.

  2. The rule

    E = \(\dfrac{\text{row total}\times\text{column total}}{\text{grand total}}\) = \(\dfrac{115\times107}{290}\)

    If drink and age are independent, the share of coffee drinkers is the same in every age group.

  3. Work it out

    E = 42.4

    An expected frequency need not be a whole number. Keep 3 significant figures.

Video walkthrough coming
Hypotheses and degrees of freedom

A school asks 197 pupils which after-school club they joined. Is the club chosen independent of the year group? The results are shown. A chi-squared test for independence is used. (a) State the null and alternative hypotheses. (b) Find the number of degrees of freedom.

ClubYear 7Year 8Year 9Football241917Netball152220Tennis91214Swimming181116
Working
  1. H0

    H0: the club chosen is independent of year group.

    H0 always says the two variables are independent. Name both variables.

  2. H1

    H1: the club chosen is not independent of year group.

    H1 says they are not independent. Do not write 'dependent on'.

  3. Rows and columns

    4 rows (clubs), 3 columns (year groups)

    Count the rows and columns of data. Do not count a Total row or column.

  4. Degrees of freedom

    df = (4 − 1)(3 − 1) = 6

    df = (rows − 1)(columns − 1).

Video walkthrough coming
A full test using a critical value

A canteen manager records the lunch chosen by 110 customers. She tests whether the lunch chosen is independent of whether the customer is a student or a member of staff, at the 5% significance level. (a) State the hypotheses. (b) Find the number of degrees of freedom. (c) Find the expected frequencies and the value of \(\chi^2\). (d) The critical value is 5.99. State the conclusion.

LunchStudentsStaffSandwich2817Salad922Soup1321
Working
  1. Hypotheses

    H0: lunch chosen is independent of customer type. H1: lunch chosen is not independent of customer type.

    Both hypotheses name the two variables.

  2. Degrees of freedom

    df = (3 − 1)(2 − 1) = 2

    3 rows and 2 columns.

  3. LunchStudentsStaffSandwich20.524.5Salad14.116.9Soup15.518.5
    Enter the observed table

    Enter the table as a 3 × 2 matrix. Expected frequencies:

    The calculator makes the expected matrix itself. Check that every expected frequency is at least 5.

  4. Chi-squared

    From the calculator: χ² = 9.19, p = 0.0101, df = 2

    The calculator gives χ², the p-value and df.

  5. Compare

    9.19 > 5.99, so reject H0.

    A large χ² means the observed values are far from the expected ones.

  6. In context

    There is sufficient evidence, at the 5% significance level, that the lunch chosen is not independent of whether the customer is a student or staff.

    Say what it means for the lunches, not just 'reject H0'.

Video walkthrough coming
Which cells depart most from independence?

266 households are asked where they live and which pet they have. Find the expected frequencies, then find the cell that adds most to \(\chi^2\). Is the observed frequency in that cell above or below the expected frequency?

HomeDogCatFishNoneHouse40351223Flat14281543Farm311069
Working
  1. HomeDogCatFishNoneHouse35.230.213.631.0Flat32.027.412.428.2Farm17.915.46.9515.8
    Expected table

    From the calculator, expected frequencies:

    Enter the observed matrix and read the expected matrix.

  2. HomeDogCatFishNoneHouse0.6690.7670.1992.07Flat10.10.01130.5427.77Farm9.601.880.1292.92
    Each cell's share

    (O − E)²/E for every cell:

    These add up to χ². The cell with the biggest value is the one that departs most.

  3. The biggest

    Flat and Dog: (O − E)²/E = 10.1

    Total χ² = 36.6.

  4. Above or below

    O = 14, E = 32.0, so O is below E.

    In context: fewer flat households than expected have a dog.

Video walkthrough coming
Concluding with a p-value

A council records 309 bus journeys on three routes and whether each bus was on time. It tests, at the 5% significance level, whether punctuality is independent of the route. The calculator gives a p-value. State the conclusion.

RouteOn timeLate by under 5 minLate by 5 min or moreRoute 1682111Route 2632714Route 3701916
Working
  1. Hypotheses

    H0: punctuality is independent of the route. H1: punctuality is not independent of the route.

    State them before the test, in context.

  2. From the calculator

    χ² = 2.72, p = 0.606, df = 4

    df = (3 − 1)(3 − 1) = 4.

  3. Compare p with α

    0.606 > 0.05, so do not reject H0.

    A p-value above the significance level means the data are not unusual if H0 is true.

  4. In context

    There is insufficient evidence, at the 5% significance level, that punctuality is not independent of the route.

    Say 'insufficient evidence'. Do not say that the test proves they are independent.

Video walkthrough coming
Reading the chi-squared curve

A chi-squared test has 4 degrees of freedom. The curve shows the shaded right tail, which has area 0.05. Find the critical value at the 5% significance level, and find the p-value when \(\chi^2=11.2\).

2468101214160.050.10.150.2χ²df = 4
Working
  1. Only the right tail

    Shaded area to the right of the critical value = 0.05

    Chi-squared tests only use the right-hand tail. The critical value cuts off the area α.

  2. Critical value

    From the calculator: critical value = 9.49

    Use the inverse chi-squared with area 0.95 to the left (0.05 to the right) and df = 4.

  3. p-value

    From the calculator: P(χ² > 11.2) = 0.0244

    The p-value is the area to the right of the test statistic.

  4. Check

    11.2 > 9.49 and 0.0244 < 0.05

    The two ways of testing always agree.

Video walkthrough coming