topic badge
Standard level

9.04 Chi squared test for goodness of fit

Lesson

$\chi^2$χ2 test for goodness of fit

So far we have looked at one and two sample $t$t-tests which are both concerned with hypotheses about population means

The $\chi^2$χ2 test for goodness of fit (GOF) can be used to determine if a population proportion appears to be correct as claimed or expected. Note: the greek letter $\chi$χ is pronounced "ki". 

Consider the problem where the colours in a packet of smarties are said to be evenly distributed by the manufacturer. Emma opens a packet of $60$60 smarties and finds the distribution as follows:

Pink Blue Red Yellow Orange Brown Purple Green
$6$6 $8$8 $10$10 $7$7 $6$6 $6$6 $9$9 $8$8

If the manufacturer's claim is correct, then the population proportion of pink smarties, $p_1=\frac{1}{8}$p1=18, will be the same as the proportion of blue smarties, $p_2=\frac{1}{8}$p2=18, or red smarties, $p_3=\frac{1}{8}$p3=18 and so on.

We define the null hypothesis, $H_0$H0 as $p_1=p_2=p_3=p_4=p_5=p_6=p_7=p_8=\frac{1}{8}$p1=p2=p3=p4=p5=p6=p7=p8=18

The alternative hypothesis is that at least one of the smartie colours is not equally distributed. We write this as

$H_1$H1 : at least one of $p_1\ne\frac{1}{8},p_2\ne\frac{1}{8},p_3\ne\frac{1}{8},p_4\ne\frac{1}{8},p_5\ne\frac{1}{8},p_6\ne\frac{1}{8},p_7\ne\frac{1}{8},p_8\ne\frac{1}{8}$p118,p218,p318,p418,p518,p618,p718,p818

Note that in this case the population proportions are all the same, but that does not have to always be the case. 

In order to test the hypothesis, we wish to compare the observed frequencies of smartie colours, with the expected frequency of smartie colours. 

The expected frequency of smarties for each colour is calculated using  $np=60\times\frac{1}{8}=7.5$np=60×18=7.5.

The test statistic, $\chi^2$χ2, is a measure of the difference between the observed frequencies and expected frequencies.

$\chi^2$χ2 test statistic for goodness of fit

$\chi^2=\sum_{\quad}^{\quad}\frac{\left(f_{observed}-f_{expected}\right)^2}{f_{expected}}$χ2= (fobservedfexpected)2fexpected 

A table is a common way to calculate the test statistic (note that you will not be required to calculate $\chi^2$χ2 manually in formal assessments for this course).

Colour $f_{obs}$fobs $f_{exp}$fexp $f_{obs}-f_{exp}$fobsfexp $(f_{obs}-f_{exp})^2$(fobsfexp)2 $\frac{(f_{obs}-f_{exp})^2}{f_{exp}}$(fobsfexp)2fexp
Pink $6$6 $7.5$7.5 $-1.5$1.5 $2.25$2.25 $0.3$0.3
Blue $8$8 $7.5$7.5 $0.5$0.5 $0.25$0.25 $0.0333$0.0333
Red $10$10 $7.5$7.5 $2.5$2.5 $6.25$6.25 $0.8333$0.8333
Yellow $7$7 $7.5$7.5 $-0.5$0.5 $0.25$0.25 $0.0333$0.0333
Orange $6$6 $7.5$7.5 $-1.5$1.5 $2.25$2.25 $0.3$0.3
Brown $6$6 $7.5$7.5 $1.5$1.5 $2.25$2.25 $0.3$0.3
Purple $9$9 $7.5$7.5 $1.5$1.5 $2.25$2.25 $0.3$0.3
Green $8$8 $7.5$7.5 $0.5$0.5 $0.25$0.25 $0.0333$0.0333
Total         $2.13$2.13

Therefore the value of $\chi^2=2.13$χ2=2.13.

If the null hypothesis is not true, then we expect the value of $\chi^2$χ2 to be large as this indicates a big difference between the observed frequencies and expected frequencies of the distribution. 

As for our $t$t-tests, we now need to find a $p$p-value and compare it to a pre-determined level of significance. This time we use the $\chi^2$χ2 curve, rather than a $t$t-distribution to calculate the $p$p-value.  The $p$p-value indicates the probability of observing a score greater than or equal to the calculated  $\chi^2$χ2 value (in this case $2.13$2.13)  on the $\chi^2$χ2 curve. 

$p$p indicates the probability of observing a value greater than or equal to $X^2$X2 calculated. 

 

Note that the shape of the $\chi^2$χ2 curve changes depending on the degrees of freedom ($df$df) of the observed data. You will need to know the $df$df value in order to calculate $p$p-values using your calculator.

Degrees of freedom for $\chi^2$χ2 GOF test

$df=$df= number of categories $-1$1 

To calculate the $p$p-value we first enter the observed values into List 1 and the expected values into List 2 on our graphics calculator. 

Graphics calculator instructions for $\chi^2$χ2 GOF test:

TI-nspire
calculator instructions
Casio fx-CG 50
calculator instructions
TI-84 Plus CE
calculator instructions
Choose  Add Calculator Press menu then select Statistics  Press stat 
Press menu then select 6 Statistics Press F3 for TEST then F3 for CHI Select D: $\chi^2$χ2 GOF-Test from the TESTS menu
Press 7 Stat Tests then 7 $\chi^2$χ2 Test Press F1 for GOF Type in the data
Choose List A for the observed list
and List B for the expected list and enter the $df$df value.
Type in the data Highlight Calculate
Press OK  view the results Scroll down to Execute and press EXE Press enter to display the results.

For our smarties distribution problem above the calculator gives us $p$p = $0.952$0.952.

For a significance level of $10%$10% we find $p>10%$p>10%, therefore we do not reject the null hypothesis. We accept that the colours of smarties are evenly distributed. 

Worked example

example 1

A school conducted a survey of its year $12$12 students to collect health information. The survey revealed that a substantial proportion of students were not engaging in regular exercise and many felt their nutrition was poor.  In response to a question on regular exercise, $55%$55% of all students reported getting no regular exercise, $30%$30% reported exercising occasionally and $15%$15% reported exercising regularly. The next year the school launched a health promotion campaign on campus in an attempt to increase health behaviours among year $12$12's. The program included posters and flyers on exercise and nutrition. To evaluate the impact of the campaign, the school again surveyed students and asked the same questions. The survey was completed by $210$210 students and the following data was collected regarding the regularity of exercise:

  No regular exercise  Occasional exercise Regular exercise Total
Observed number
of students
 
$113$113 $72$72 $25$25 $210$210
Expected number
of students
       

In order to assess whether the health promotion campaign has been successful in improving exercise levels among year $12$12's the school conducts a $\chi^2$χ2 goodness of fit test at a $5%$5% significance level. 

(a) State the hypotheses set for this problem.

Think: The null hypothesis means 'no change'. Therefore the original exercise percentages are stated.

$H_0$H0 : $p_1$p1=$0.55$0.55, $p_2$p2 = $0.3$0.3, $p_3$p3 = $0.15$0.15

$H_1$H1 : $H_0$H0 is false, therefore one or more of the following is true $p_1\ne0.55,p_2\ne0.3,p_3\ne0.15$p10.55,p20.3,p30.15

(b) State the level of significance, $\alpha$α as a decimal number.

Think: The level of significance should be pre-determined. In this case it is part of the question, but note that it should always be determined before performing the test so that there is no temptation to adjust it to fit later results.

Do: $\alpha=0.05$α=0.05

(c) Complete the table above with the expected numbers of students.

Think: The original percentages are $55%$55%, $30%$30% and $15%$15%. Multiply these values by $210$210 to find the expected values. 

Do:

  No regular exercise  Occasional exercise Regular exercise Total
Observed number
of students
 
$113$113 $72$72 $25$25 $210$210
Expected number
of students
$115.5$115.5 $63$63 $31.5$31.5 $210$210

(d) State the number of degrees of freedom ($df$df) for this data.

Think: $df$df = number of categories $-1$1. There are three categories, so $df$df = $2$2

(e) Use your calculator to find the $\chi^2$χ2 and $p$p-values.

Think: First put the observed data into List 1 and the expected data into List 2.

Using the $\chi^2$χ2 GOF test we find $\chi^2=2.6811$χ2=2.6811 and $p$p = $0.2617$0.2617

(f) Comment on your findings.

Think: compare the $p$p-value to the level of significance. If $p<\alpha$p<α then we reject $H_0$H0.

Do: $0.2617>0.05$0.2617>0.05 therefore $p>\alpha$p>α and we do not reject the null hypothesis. It appears the campaign has not been successful in significantly changing the exercise levels of year $12$12 students. 

Careful!

The $\chi^2$χ2 goodness of fit test only works well if there is a sufficient number of observations in each category. As a general rule there should be at least $5$5 observations. If there are not $5$5 observations in a category then similar categories may be combined together to ensure more accurate results. 

Critical values

Rather than calculating a $p$p-value and comparing it to a given level of significance, it is also possible to make a judgement on whether to accept or reject $H_0$H0 using a critical value for $\chi^2$χ2

The critical value is the largest value that a calculated test statistic ($\chi^2$χ2) can be and not reject $H_0$H0. If the calculated $\chi^2$χ2 value is larger than the critical value for a given level of significance and degrees of freedom (as shown in the table below), then the probability of the value occurring is low and we reject the null hypothesis. 

Table of critical values for $\chi^2$χ2

$df$df value $\alpha=1%$α=1% $\alpha=5%$α=5% $\alpha=10%$α=10%
$1$1 $6.63$6.63 $3.84$3.84 $2.71$2.71
$2$2 $9.21$9.21 $5.99$5.99 $4.61$4.61
$3$3 $11.34$11.34 $7.81$7.81 $6.25$6.25
$4$4 $13.28$13.28 $9.49$9.49 $7.78$7.78
$5$5 $15.09$15.09 $11.07$11.07 $9.24$9.24

Consider worked example 1 above where the level of significance given was $5%$5% and the degrees of freedom was found to be $2$2. Using the table of critical values we can determine that the critical value for $\chi^2$χ2 is $5.99$5.99.

The calculated test statistic $\chi^2=2.6811$χ2=2.6811.

As the calculated $\chi^2$χ2 is less than the critical value of $\chi^2$χ2 , we do not reject the null hypothesis, $H_0$H0.

$\chi^2$χ2 critical value

If $\left(\chi^2\right)_{calculated}\ \ge\ \left(\chi^2\right)_{critical}$(χ2)calculated  (χ2)critical then we reject $H_0$H0

 

What is Mathspace

About Mathspace