The z and t tests concern numbers such as means. Many data are counts in categories, such as how many students prefer each subject. The chi-square test compares observed counts with the counts expected under a hypothesis.
The Chi-Square Statistic
The chi-square statistic is chi-square = sum of (O - E) squared / E, where O is an observed frequency and E is the expected frequency. The value is zero when the observed and expected counts agree, and it grows as they differ. The chi-square distribution is skewed to the right and depends on the degrees of freedom. The test is right-tailed, so H0 is rejected when the calculated value exceeds the table value. Each expected frequency should be at least 5.
Definition: Chi-square test. A test that compares observed frequencies with expected frequencies using the statistic sum of (O - E) squared / E.
Goodness of Fit
The goodness of fit test checks whether observed data follow a stated distribution. H0 says that the data fit the distribution. The expected frequencies are calculated from it, and the degrees of freedom are the number of categories minus 1 (and minus one more for each parameter estimated from the data).
Independence of Attributes
This test decides whether two attributes, such as gender and choice of course, are independent. The data are arranged in a contingency table of r rows and c columns. The expected frequency of a cell is E = (row total × column total) / grand total. The degrees of freedom are (r - 1)(c - 1). H0 states that the attributes are independent.
Table 4.4: Chi-square tests compared
| Feature | Goodness of fit | Independence |
|---|---|---|
| Question | Does data fit a pattern? | Are two attributes related? |
| Expected frequency | From the stated distribution | Row total x column total / grand total |
| Degrees of freedom | k - 1 | (r - 1)(c - 1) |
Example 4.6: Goodness of fit for a die
Problem: A die is thrown 120 times with frequencies 25, 17, 15, 23, 24 and 16 for faces 1 to 6. Test at 5 percent whether the die is fair.
Solution:
- H0: the die is fair, so E = 120 / 6 = 20 for each face.
- chi-square = (25 - 20)² / 20 + (17 - 20)² / 20 + (15 - 20)² / 20 + (23 - 20)² / 20 + (24 - 20)² / 20 + (16 - 20)² / 20 = 5.0.
- The degrees of freedom are 5, and the table value at 5 percent is 11.07.
Result: Since 5.0 is less than 11.07, H0 is not rejected. The die may be considered fair.
Example 4.7: Independence of attributes
Problem: Of 100 students, 30 of 50 boys and 20 of 50 girls chose Course A, and the rest chose Course B. Test at 5 percent whether the course is independent of gender.
Solution:
- Observed: boys 30 and 20, girls 20 and 30. Row and column totals are all 50, so every expected frequency is 25.
- chi-square = 4 × (5 squared / 25) = 4.
- The degrees of freedom are (2 - 1)(2 - 1) = 1, and the table value at 5 percent is 3.841.
Result: Since 4 is greater than 3.841, H0 is rejected. The choice of course depends on gender.
obs = [25, 17, 15, 23, 24, 16]
print(sum((o - 20) ** 2 / 20 for o in obs))
O = [30, 20, 20, 30]
print(sum((o - 25) ** 2 / 25 for o in O))
Output
5.0
4.0
Common mistakes
- Using observed totals that do not match the expected totals.
- Using n - 1 degrees of freedom for a contingency table instead of (r - 1)(c - 1).
- Applying the test when expected frequencies are below 5 without combining categories.
Key takeaways
- chi-square = sum of (O - E) squared / E.
- Goodness of fit has k - 1 degrees of freedom.
- Independence has (r - 1)(c - 1) degrees of freedom.
- E = row total x column total / grand total.
Practice questions
- Write the chi-square statistic.
- How many degrees of freedom has a 3 by 4 contingency table?
- Is the chi-square test left-tailed or right-tailed?
Answers to the practice questions
- Sum of (O - E) squared divided by E.
- (3 - 1)(4 - 1) = 6.
- Right-tailed.
Sources
- Official BCA syllabus Probability and Statistics, course code 26BCA403DS01, Unit IV
