ExamSaathi learning library · Government exams · SSC CGL quantitative foundations; published syllabus labelled 2024 · Quantitative aptitude

Chi-square tests: goodness of fit and independence in contingency tables

Learn the chi-square statistic, the sum of (O - E)^2 / E, and use it to test whether counts fit a claimed ratio, whether two classifications in a contingency table are independent, and whether one variance is larger than claimed.

Status: original pilot lesson. Teacher review is pending. It is not a complete course or official exam material.

You will learn to

Before you start

You should already know the steps of a significance test: a null hypothesis H0, an alternative H1, a level of significance such as 5 percent, and a critical value beyond which H0 is rejected. Chi-square tests apply those steps to counts. The data are frequencies in classes, such as how often each face of a die turns up, or how many shoppers in each city chose each tea brand. O stands for an observed frequency and E for the frequency expected if H0 were true. The chi-square distribution is a family of curves, one for each number of degrees of freedom (df); each curve covers only values of 0 or more and has a long tail to the right. Every count in this lesson is invented for practice, and the letter x is used only for multiplication.

The chi-square statistic and its conditions

For each class, take the gap O - E, square it and divide by E. Adding these pieces over all the classes gives chi-square = sum of (O - E)^2 / E. If every observed count matched its expected count, chi-square would be 0; the further the counts drift from what H0 predicts, the larger it grows. So only a large value is evidence against H0: these tests are right-tailed, and H0 is rejected when the calculated chi-square is more than the upper critical value for the correct df and level. Four conditions come first. Use actual counts, never percentages or proportions; turn them back into counts. The expected frequencies must add up to the same total N as the observed ones. Each item must be counted in exactly one class, independently of the others. Every expected frequency should be at least 5; if one is smaller, join that class to a neighbouring class before testing, and count the classes again when fixing the df.

Goodness of fit: do the counts match a claim?

A goodness of fit test asks whether observed counts agree with a claimed pattern, and H0 states that claim. The expected count of a class is N x (its claimed share). A fair die rolled 120 times should show each face 120 x 1/6 = 20 times. If 160 pea seeds should split in the ratio 9 : 3 : 3 : 1, the shares are 9/16, 3/16, 3/16 and 1/16, giving expected counts 90, 30, 30 and 10. If road accidents in a town are claimed to be spread evenly over the 7 days of a week, each day expects one seventh of the total. The degrees of freedom are df = k - 1 - m, where k is the number of classes after any merging and m is the number of parameters estimated from the same data. One df is always lost because the expected counts are forced to total N. When the claim fixes every share, m = 0, so the evenly spread accidents give df = 7 - 1 = 6, and the upper 5 percent point of chi-square with 6 df is 12.592. When a Poisson mean must first be estimated from the data, m = 1.

Independence in a contingency table

A contingency table counts N items classified in two ways at once, one way along the rows and the other down the columns. H0 says the two classifications are independent: for example, the tea brand a person prefers does not depend on the city. Under H0 the expected frequency of each cell is E = (row total x column total) / grand total, which keeps every row total and column total the same as observed. Then chi-square = sum of (O - E)^2 / E over every cell, and df = (rows - 1) x (columns - 1), because once that many cells are filled in, the totals fix all the others. For a table with 2 rows and 3 columns, df = 1 x 2 = 2, and the upper 5 percent point of chi-square with 2 df is 5.991. A 2 x 2 table has a shortcut. Name the cells a, b in the first row and c, d in the second, with N = a + b + c + d. Then chi-square = N x (ad - bc)^2 / ((a + b)(c + d)(a + c)(b + d)) with 1 df, whose upper 5 percent point is 3.841. The shortcut gives exactly the same value as the cell-by-cell method.

A further case: testing one variance

Chi-square can also test a claim about the spread of a single normal population. Take a random sample of n values and work out s^2 = (sum of squared deviations from the sample mean) / (n - 1). If H0 claims the population variance is sigma^2, the statistic is chi-square = (n - 1) x s^2 / sigma^2, which is just the sum of squared deviations divided by sigma^2, with n - 1 degrees of freedom. In this lesson H1 always says the spread is larger than claimed, as when a filling machine is suspected of becoming erratic, so H0 is rejected when the statistic is more than the upper 5 percent point. For a sample of 10, df = 9 and that point is 16.919. A claim given as a standard deviation must be squared first: a claimed SD of 2 ml means sigma^2 = 4. Unlike the tests on counts, this test depends strongly on the population being normal. A test for a smaller spread needs a lower point of the chi-square curve, and a two-sided test needs both a lower and an upper point; this lesson uses only upper points.

Common mistakes

Putting percentages into the formula: chi-square is built from counts, and the same percentages from 1,000 people give ten times the value they give from 100 people. Dividing by O instead of E, or forgetting to square each gap. Taking df as the number of classes in a goodness of fit test instead of k - 1, or forgetting to subtract one more for each parameter estimated from the data. Taking rows x columns as the df of a contingency table instead of (rows - 1) x (columns - 1). Keeping a class whose expected frequency is below 5. Rejecting H0 because chi-square is small; in these tests only a value above the critical value leads to rejection. In the variance test, multiplying by n instead of n - 1, or using the SD where the variance is needed. Treating 'do not reject H0' as proof that H0 is true; it only means the data are consistent with it.

A routine for chi-square questions

1. Write H0 and H1 in words: the counts fit the claim, the two classifications are independent, or the variance equals the claimed value. 2. Find every E: N x share for goodness of fit, or row total x column total / grand total for a table. 3. Check that the E values total N and are all at least 5, and merge classes if not. 4. Work out (O - E)^2 / E for each class or cell and add them; for a variance, divide the sum of squared deviations by sigma^2. 5. Fix the df: k - 1 - m, (rows - 1) x (columns - 1), or n - 1. 6. Compare with the stated upper critical value and write the decision in words, keeping the chi-square value in the sentence.

Worked examples

  1. A plant breeder at an agricultural university in Ludhiana crosses two pea varieties and expects four seed types in the ratio 9 : 3 : 3 : 1. Among 160 seeds she counts 81 round yellow, 36 round green, 33 wrinkled yellow and 10 wrinkled green. The upper 5 percent point of chi-square with 3 degrees of freedom is 7.815. Test at the 5 percent level whether the counts fit the ratio.

    1. H0: the seeds follow the 9 : 3 : 3 : 1 ratio. H1: they do not.
    2. Expected counts: 160 x 9/16 = 90, 160 x 3/16 = 30, 160 x 3/16 = 30 and 160 x 1/16 = 10. They total 160 and each is at least 5.
    3. Round yellow: (81 - 90)^2 / 90 = 81 / 90 = 0.9.
    4. Round green: (36 - 30)^2 / 30 = 36 / 30 = 1.2.
    5. Wrinkled yellow: (33 - 30)^2 / 30 = 9 / 30 = 0.3.
    6. Wrinkled green: (10 - 10)^2 / 10 = 0.
    7. chi-square = 0.9 + 1.2 + 0.3 + 0 = 2.4. The ratio fixes every share, so no parameter is estimated and df = 4 - 1 = 3.
    8. 2.4 is less than 7.815, so the statistic is not in the right-tail rejection region.

    Answer: chi-square = 2.4 with 3 df, below 7.815: do not reject H0. The seed counts are consistent with the 9 : 3 : 3 : 1 ratio.

  2. A tea company asks 100 tea drinkers in Kolkata and 100 in Guwahati which of its three brands, P, Q or R, they prefer. Kolkata: P 28, Q 45, R 27. Guwahati: P 12, Q 55, R 33. The upper 5 percent point of chi-square with 2 degrees of freedom is 5.991, and the upper 1 percent point is 9.210. Test at the 5 percent level whether brand preference is independent of the city.

    1. H0: brand preference does not depend on the city. H1: it does.
    2. Totals: rows 100 and 100; columns P 28 + 12 = 40, Q 45 + 55 = 100, R 27 + 33 = 60; grand total N = 200.
    3. Expected frequencies: P 100 x 40 / 200 = 20, Q 100 x 100 / 200 = 50, R 100 x 60 / 200 = 30, the same in both rows because the row totals are equal. All are at least 5.
    4. Kolkata cells: (28 - 20)^2 / 20 = 3.2, (45 - 50)^2 / 50 = 0.5, (27 - 30)^2 / 30 = 0.3.
    5. Guwahati cells: (12 - 20)^2 / 20 = 3.2, (55 - 50)^2 / 50 = 0.5, (33 - 30)^2 / 30 = 0.3.
    6. chi-square = 3.2 + 0.5 + 0.3 + 3.2 + 0.5 + 0.3 = 8, with df = (2 - 1) x (3 - 1) = 2.
    7. 8 is more than 5.991, so H0 is rejected at 5 percent. At the stricter 1 percent level, 8 is below 9.210 and H0 would not be rejected, so the level must always be stated.

    Answer: chi-square = 8 with 2 df, above 5.991: reject H0 at the 5 percent level. Brand preference appears to depend on the city.

  3. A dairy in Anand fills 500 ml milk pouches, and the machine maker claims the fill has a standard deviation of 2 ml. An inspector measures 8 pouches: 497, 503, 500, 506, 494, 501, 499 and 500 ml. Assuming the fills are normally distributed, test at the 5 percent level whether they vary more than claimed. The upper 5 percent point of chi-square with 7 degrees of freedom is 14.067.

    1. H0: the population variance is 2^2 = 4 ml^2. H1: it is more than 4 ml^2, a right-tailed test.
    2. Sample mean = (497 + 503 + 500 + 506 + 494 + 501 + 499 + 500) / 8 = 4000 / 8 = 500 ml.
    3. Deviations: -3, 3, 0, 6, -6, 1, -1, 0. Squares: 9, 9, 0, 36, 36, 1, 1, 0, which total 92.
    4. s^2 = 92 / (8 - 1) = 92/7, about 13.14 ml^2.
    5. chi-square = (n - 1) x s^2 / sigma^2 = 7 x (92/7) / 4 = 92 / 4 = 23, with df = 8 - 1 = 7.
    6. 23 is more than 14.067, so the statistic falls in the rejection region.

    Answer: chi-square = 23 with 7 df, above 14.067: reject H0. The pouch fills vary more than a standard deviation of 2 ml allows.

Practice checks

  1. A board-game maker in Jaipur rolls a die 120 times. The faces 1 to 6 turn up 15, 25, 18, 22, 26 and 14 times. The upper 5 percent point of chi-square with 5 degrees of freedom is 11.070. Testing H0: the die is fair, which line gives the statistic and the decision at the 5 percent level?
  2. A health survey of 200 adults in a district of Maharashtra found that, of the 100 who had taken a flu vaccine, 10 fell ill, while of the 100 who had not, 30 fell ill. Using the 2 x 2 shortcut, test at the 5 percent level whether falling ill is independent of vaccination. The upper 5 percent point of chi-square with 1 degree of freedom is 3.841.
  3. Calls reaching a Pune helpline in each minute are grouped into 6 classes (0, 1, 2, 3, 4, and 5 or more calls), and a Poisson distribution is fitted after estimating its mean from the same data. Every expected frequency is at least 5. Upper 5 percent points of chi-square: 3 df 7.815, 4 df 9.488, 5 df 11.070, 6 df 12.592. Which line gives the degrees of freedom and the critical value for the goodness of fit test at 5 percent?
  4. A biscuit factory in Bengaluru claims that the weights of its packets have variance 25 g^2. A sample of 11 packets has sample variance 45 g^2, found with divisor n - 1. Assuming normal weights, test at the 5 percent level whether the variance is larger than claimed. The upper 5 percent point of chi-square with 10 degrees of freedom is 18.307.
  5. In a 3 x 3 contingency table for 150 commuters in Lucknow (mode of travel against age group), one cell has row total 60, column total 45 and observed frequency 24. How much does this cell add to the chi-square statistic for independence?

Sources checked