Goodness of fit tests and contingency tables

Goodness of fit tests and contingency tables

Spec 6.1 · Edexcel A-Level Further Maths: Revision notes

Studying this course? Keep your progress in one place.

Key facts

  • H0\mathrm{H}_0: the model fits (or the variables are independent). The test statistic is X2=∑(O−E)2EX^2=\sum\frac{(O-E)^2}{E}, approximately χν2\chi^2_\nu, and the test is always upper-tailed.
  • Every expected frequency must be at least 55: combine neighbouring cells otherwise, adding both OO and EE.
  • ν=cells−1−(parameters estimated)\nu=\text{cells}-1-\text{(parameters estimated)}, counting cells after combining.
  • Contingency tables: E=row total×column totalgrand totalE=\frac{\text{row total}\times\text{column total}}{\text{grand total}} and ν=(r−1)(c−1)\nu=(r-1)(c-1).
  • Reject H0\mathrm{H}_0 if X2≥X^2\geq the critical value, and conclude in context.

Hypotheses and test statistic

H0\mathrm{H}_0 says the model fits; the statistic X2=∑(O−E)2EX^2=\sum\frac{(O-E)^2}{E} is large when the data are far from it.

A goodness of fit test checks whether observed frequencies are consistent with a proposed model.

State the hypotheses in words: H0\mathrm{H}_0 the model fits (for example the die is fair, or the data follow B(4,p)\mathrm{B}(4,p)); H1\mathrm{H}_1 the model does not fit. They must be about the model or population, not the sample.

OiO_i are the observed frequencies and EiE_i the expected frequencies if H0\mathrm{H}_0 is true. X2X^2 is approximately χν2\chi^2_\nu; a large value means the data are far from the model, so the test is always upper-tailed.

0246810121416123456Die scoreFrequency
  • Observed
  • Expected
Illustrative data: 60 rolls of a die. The statistic adds up the squared gaps between observed and expected, so bigger gaps give a larger X².
  • Test statisticX2=∑(Oi−Ei)2EiX^2=\sum\dfrac{(O_i-E_i)^2}{E_i}

Observed 18, expected 15 in a cell. What does this cell contribute to X2X^2?

Expected frequencies and combining

Each EiE_i is the total times the model probability; combine neighbouring cells until every E≥5E\geq5.

Find each EiE_i as total ×\times probability: nk\frac nk for a discrete uniform model with kk outcomes; n P(X=i)n\,\mathrm{P}(X=i) from B(N,p)\mathrm{B}(N,p) or Po(λ)\mathrm{Po}(\lambda).

For a Poisson model the last cell is "rr or more", so the probabilities sum to 1.

The χ2\chi^2 approximation is only reliable if every Ei≥5E_i\geq5. If not, combine cells with a neighbour, adding both OO and EE, and count the combined cell as one. Combining only the observed or only the expected frequencies is a common mistake.

  1. 1

    Find E

    total × model probability for each cell

  2. 2

    Check

    is every E at least 5?

  3. 3

    Combine

    merge with a neighbour, adding O and E

  4. 4

    Count

    the merged cell counts as one

Combining cells.

Worked example

The expected frequencies are 37.97, 41.60, 17.09, 3.12, 0.2137.97,\ 41.60,\ 17.09,\ 3.12,\ 0.21. Combine cells so that every E≥5E\geq5.

Expected frequencies are 12, 9, 4, 212,\ 9,\ 4,\ 2. How many cells remain after combining so that every E≥5E\geq5?

Degrees of freedom

Count the cells after combining, subtract 1, and subtract one more for each parameter estimated from the data.

The degrees of freedom are ν=(cells after combining)−1−(parameters estimated from the data)\nu=\text{(cells after combining)}-1-(\text{parameters estimated from the data}). The "−1-1" is because the total is fixed.

If a parameter is given in the question (a fair die, p=12p=\frac12, λ=1.5\lambda=1.5) nothing more is subtracted. If it is estimated from the sample, subtract one for each estimate: p^=xˉN\hat p=\frac{\bar x}{N} for a binomial, λ^=xˉ\hat\lambda=\bar x for a Poisson.

Always count the cells after combining, not before.

  1. 1

    Count the cells

    After combining any cells with a small expected frequency

  2. 2

    Subtract 1

    The total is fixed

  3. 3

    Subtract estimated parameters

    One for each parameter estimated from the data; none if given

  4. 4

    Result

    This is ν

Finding the degrees of freedom.
  • Degrees of freedom ν\nucells − 1 − estimated

Fair die

Cells:
6
Estimated parameters:
0
Degrees of freedom:
5

B(4, p), p estimated

Cells:
3
Estimated parameters:
1
Degrees of freedom:
1

A Poisson model with λ\lambda estimated from the data has 5 cells after combining. What is ν\nu?

Uniform, binomial and Poisson models

Only the expected frequencies and the degrees of freedom change.

The method is the same for each model. Only the expected frequencies and the degrees of freedom change.

When a last cell like "4 or more" is used, you need the total of the observations in that cell to find xˉ\bar x.

For a discrete uniform model nothing is estimated. For a binomial model, pp may be given or estimated from the sample mean. For a Poisson model, λ\lambda is estimated by xˉ\bar x unless given.

05101520253001234Value of XExpected frequency
Expected frequencies for a binomial B(4, 0.5) model with 80 observations: 80 × P(X = i) is 5, 20, 30, 20, 5.

Discrete uniform

Expected frequency:
nk\frac nk
Degrees of freedom:
k − 1

Binomial, p given

Expected frequency:
n P(X=i)n\,\mathrm{P}(X=i)
Degrees of freedom:
cells − 1

Binomial, p estimated

Expected frequency:
use p^=xˉN\hat p=\frac{\bar x}{N}
Degrees of freedom:
cells − 2

Poisson, λ estimated

Expected frequency:
use λ^=xˉ\hat\lambda=\bar x
Degrees of freedom:
cells − 2

A B(4,p)\mathrm{B}(4,p) model has pp estimated from the sample mean and 55 cells. What is ν\nu?

Contingency tables

Find each expected frequency from the totals, and use ν=(r−1)(c−1)\nu=(r-1)(c-1).

A contingency table classifies each observation by two variables. The test asks whether they show independence.

  • H0\mathrm{H}_0: there is no association between the variables.
  • H1\mathrm{H}_1: there is an association.

Under H0\mathrm{H}_0, E=row total×column totalgrand totalE=\frac{\text{row total}\times\text{column total}}{\text{grand total}}, and ν=(r−1)(c−1)\nu=(r-1)(c-1) for rr rows and cc columns. No parameters are estimated separately. Combine cells with E<5E<5 by merging rows or columns, and recalculate ν\nu from the new table.

24681012140.10.20.30.40.5xyν = 2ν = 4ν = 6
Chi-squared distributions for ν = 2, 4 and 6. Each is skewed to the right and the peak moves right as ν increases; the critical region is the right-hand tail.
  • Expected frequencyrow total×column totalgrand total\dfrac{\text{row total}\times\text{column total}}{\text{grand total}}
  • Degrees of freedomν=(r−1)(c−1)\nu=(r-1)(c-1)

Worked example

In a 2×32\times3 table the row totals are 4040 and 6060, the column totals are 20, 30, 5020,\ 30,\ 50 and the grand total is 100100. Find the expected frequency for row 11, column 33, and ν\nu.

A contingency table has 33 rows and 44 columns. What is ν\nu?

Reaching a conclusion

Compare X2X^2 with the critical value, or the p-value with the significance level.

Compare X2X^2 with the critical value from the χν2\chi^2_\nu table at the given significance level. If X2≥X^2\geq the critical value, reject H0\mathrm{H}_0.

Alternatively, a calculator gives the p-value P(χν2≥X2)\mathrm{P}(\chi^2_\nu\geq X^2). Reject H0\mathrm{H}_0 if it is below the significance level.

A small X2X^2 is not proof the model is right, only that there is no evidence against it. Always write the conclusion in context.

0246810ν = 1ν = 2ν = 5Degrees of freedomCritical value
Critical values of χ² at the 5% level.

Worked example

A die is rolled 120120 times. Each face has E=20E=20 and X2=9420=4.7X^2=\frac{94}{20}=4.7. Test at the 5%5\% level whether the die is fair.

ν=2\nu=2 and X2=7.2X^2=7.2 at the 5%5\% level. What is the conclusion?

Try an exam question

A die is rolled 6060 times with these frequencies for faces 11 to 66: 8, 12, 14, 6, 9, 118,\ 12,\ 14,\ 6,\ 9,\ 11. Test, at the 5%5\% significance level, whether the die is fair. The critical value of χ52\chi^2_5 at the 5%5\% level is 11.07011.070.

[5 marks]

That's the notes covered.

Carry on to the next subtopic.

Try a question

Find the expected frequency of each face if the die is fair.

1 mark, marked against the mark scheme

Revision notes