Lecture #20: Chi-Squared Goodness-of-Fit Test

STAT 200 – Introductory Statistics

Torin Quinlivan

Knox College

February 11, 2026

Chi-Squared (\(\chi^2\)) Distribution

If \(Z_1, \dots, Z_n\) are independent, standard Normal random variables, then:

\[ Q = \sum_{i = 1}^n Z_i \sim \chi_{\nu}^2, \]

is a chi-squared (\(\chi^2\)) distribution with \(\nu\) degrees of freedom.

Goodness of Fit

Suppose we want to see if we have an idea of what distribution we think a categorical variable should be following. How do we test that?

I would like to test if my three-sided die is fair. To do this, I roll it \(n = 600\) times and tabulate the observed frequency distribution.

In those 600 rolls, I got \(n_1 = 180\) ones, \(n_2 = 215\) twos, and \(n_3 = 205\) threes.

Goodness of Fit

I would like to test if my three-sided die is fair. To do this, I roll it \(n = 600\) times and tabulate the observed frequency distribution.

In those 600 rolls, I got \(n_1 = 180\) ones, \(n_2 = 215\) twos, and \(n_3 = 205\) threes.

That is, we are given the following information:

  • Observed Counts: \(\{ x_1, x_2, x_3 \} = \{ 180, 215, 205 \}\)
  • Expected Counts: \(\{ \mu_1, \mu_2, \mu_3 \} = \{ 200, 200, 200 \}\)

The Theory of Goodness of Fit

Note what information the above gives to us:

  • Observed Counts: \(\{ x_1, x_2, \dots, x_k \}\)
  • Expected Counts: \(\{ \mu_1, \mu_2, \dots, \mu_k \}\)

The goal is to create a test statistic that measures how far the observed counts are from the expected counts, while still having a distribution we know (or can determine).

The Theory of Goodness of Fit

It can be shown (in later STAT courses) that this test statistic approximately follows a chi-square distribution with \(k - 1\) degrees of freedom (if the \(\mu_i\) are large enough):

\[ TS = \sum_{i=1}^k \frac{(x_i - \mu_i)^2}{\mu_i} \]

It just requires that we can determine the expected value of each count, \(\mu_i\).

Example: Goodness of Fit

Recall from earlier that we have:

  • Observed Counts: \(\{ x_1, x_2, x_3 \} = \{ 180, 215, 205 \}\)
  • Expected Counts: \(\{ \mu_1, \mu_2, \mu_3 \} = \{ 200, 200, 200 \}\)

Where did the expected counts come from?

Recall from the Binomial distribution that \(\mu_i = n p_i\), where \(n\) is the number of rolls and \(p_i\) is the probability of the \(i^{th}\) outcome.

Example: Goodness of Fit

\[ \begin{aligned} TS &= \sum_{i=1}^k \frac{(x_i - \mu_i)^2}{\mu_i} \\ &= \frac{(x_1 - \mu_1)^2}{\mu_1} + \frac{(x_2 - \mu_2)^2}{\mu_2} + \frac{(x_3 - \mu_3)^2}{\mu_3} \\ &= \frac{(180 - 200)^2}{200} + \frac{(215 - 200)^2}{200} + \frac{(205 - 200)^2}{200} \\ &= \frac{(-20)^2}{200} + \frac{15^2}{200} + \frac{5^2}{200} \\ &= \frac{400}{200} + \frac{225}{200} + \frac{25}{200} \\ &= \frac{650}{200} \\ &= 3.250 \end{aligned} \]

Example: Goodness of Fit

How does this test statistic value (3.250) compare to the chi-squared distribution with \(k - 1 = 2\) degrees of freedom?

Example: Goodness of Fit

Conclusion:

We would like to test if the three-sided die is fair. To test this, we rolled the die 100 times. In those rolls, ones came us 180 times; twos, 215 times; and threes, 205 times. To test if there is significant evidence that the die is unfair, we use the chi-squared goodness-of-fit test.

Because the test’s \(p\)-value of 0.1969 is greater than our selected value of \(\alpha = 0.05\), we fail to reject the null hypothesis. There is not enough evidence to conclude that the die is unfair.

As an aside, we are also unable to conclude that the die is fair. In fact, this experiment gave us no additional information about the outcome distribution of the three-sided die.

Example: Goodness of Fit

R Code:

obs = c(180, 215, 205)
exp = c(200, 200, 200)

TS = sum( (obs-exp)^2 / exp )

TS
[1] 3.25
1 - pchisq(TS, df=2)
[1] 0.1969117

Example: Goodness of Fit

R Code:

chisq.test(x = c(180, 215, 205), p = c(1/3, 1/3, 1/3))

    Chi-squared test for given probabilities

data:  c(180, 215, 205)
X-squared = 3.25, df = 2, p-value = 0.1969

Note: This is the process you should use if you are performing genuine statistical analysis.

Example #1: Car Origin

My friend claims that the proportion of cars on the Knox campus that are American is the same as the proportion that are European and the proportion that are Asian. To test this, I went to the parking lot across Berrien from SMC and counted the cars and their origins. In that sample, there were 19 American cars, 23 Asian cars and 2 European cars.

From this, the observed and expected values are:

  • Observed Counts: \(\{ x_1, x_2, x_3 \} = \{ 19, 23, 2 \}\)
  • Expected Counts: \(\{ \mu_1, \mu_2, \mu_3 \} = \left\{ \frac{44}{3}, \frac{44}{3}, \frac{44}{3} \right\}\)

Example #1: Car Origin

\[ \begin{aligned} TS &= \sum_{i=1}^k \frac{(x_i - \mu_i)^2}{\mu_i} \\ &= \frac{(x_1 - \mu_1)^2}{\mu_1} + \frac{(x_2 - \mu_2)^2}{\mu_2} + \frac{(x_3 - \mu_3)^2}{\mu_3} \\ &= \frac{(19 - \frac{44}{3})^2}{\frac{44}{3}} + \frac{(23 - \frac{44}{3})^2}{\frac{44}{3}} + \frac{(2 - \frac{44}{3})^2}{\frac{44}{3}} \\ &= \frac{\left( \frac{13}{3} \right)^2}{\frac{44}{3}} + \frac{\left( \frac{25}{3} \right)^2}{\frac{44}{3}} + \frac{\left( -\frac{38}{3} \right)^2}{\frac{44}{3}} \\ \end{aligned} \]

Example #1: Car Origin

\[ \begin{aligned} TS &= \sum_{i=1}^k \frac{(x_i - \mu_i)^2}{\mu_i} \\ &= \frac{169}{132} + \frac{625}{132} + \frac{1444}{132} \\ &= \frac{2238}{132} \\ &\approx 16.954\dots \end{aligned} \]

Example #1: Car Origin

How does this test statistic value (16.954) compare to the chi-squared distribution with \(k - 1 = 2\) degrees of freedom?

Example #1: Car Origin

Conclusion:

We would like to test if the proportions of American, Japanese, and European cars are equal at Knox College. To test this, we examined the country-of-origin of cars in the parking lot on Berrien and Academy. In this lot, the number of American, European, and Japanese cars is 19, 23, and 2. To test if there is significant evidence that there is a difference in the origin proportions, we use the chi-squared goodness-of-fit test.

Because the \(p\)-value is much less than our selected value of \(\alpha = 0.05\), we reject the null hypothesis. We are able to conclude that the proportion of cars from the three regions is not the same.

Example #1: Car Origin

R Code:

obs = c(19, 23, 2)
exp = c(44/3, 44/3, 44/3)

TS = sum( (obs-exp)^2 / exp )

TS
[1] 16.95455
1 - pchisq(TS, df=2)
[1] 0.0002081456
chisq.test(x = c(19, 23, 2))

    Chi-squared test for given probabilities

data:  c(19, 23, 2)
X-squared = 16.955, df = 2, p-value = 0.0002081

Example #2: Math Graduates

The Department of Mathematics claims that the proportion of its graduates who went to graduate school is twice the proportion of any other post-baccalaureate path.

To test this, the Department sent out a questionnaire to all of the alums for whom they had current addresses. Here is a table of our results from those who responded:

Category Grad School Business Education Unemployed
Count \(13\) \(7\) \(10\) \(5\)
Expected \(14\) \(7\) \(7\) \(7\)

Example #2: Math Graduates

\[ \begin{aligned} TS &= \sum_{i=1}^k \frac{(x_i - \mu_i)^2}{\mu_i} \\ &= \frac{(x_1 - \mu_1)^2}{\mu_1} + \frac{(x_2 - \mu_2)^2}{\mu_2} + \frac{(x_3 - \mu_3)^2}{\mu_3} + \frac{(x_4 - \mu_4)^2}{\mu_4} \\ &= \frac{(13 - 14)^2}{14} + \frac{(7 - 7)^2}{7} + \frac{(10 - 7)^2}{7} + \frac{(5 - 7)^2}{7} \\ &= \frac{(-1)^2}{14} + \frac{0^2}{7} + \frac{3^2}{7} + \frac{(-2)^2}{7} \\ &= \frac{1}{14} + 0 + \frac{9}{7} + \frac{4}{7} \\ &= \frac{27}{14} \approx 1.9286 \end{aligned} \]

Example #2: Math Graduates

How does this test statistic value (\(1.9286\)) compare to the chi-squared distribution with \(k - 1 = 3\) degrees of freedom?

Example #2: Math Graduates

Conclusion:

The Department of Mathematics at Knox College would like to determine if the proportion of its graduates who went to graduate school is twice the proportion of any other post-baccalaureate path. To test this, the department contacted its graduates. Of the \(35\) who responded, \(13\) attended graduate school, \(7\) went into business, \(10\) went into education, and \(5\) were unemployed. We used the chi-squared goodness-of-fit test to determine if the claim by the Department of Mathematics is reasonable.

Because the \(p\)-value of 0.5874 is greater than our selected value of \(\alpha = 0.05\), we cannot reject the null hypothesis. The claim made by the Department of Mathematics that twice as many of its graduates go to graduate school than any other category is reasonable, given the data.

Example #2: Math Graduates

R Code:

obs = c(13, 7, 10, 5)
exp = c(14, 7, 7, 7)

TS = sum( (obs-exp)^2 / exp )

TS
[1] 1.928571
1 - pchisq(TS, df=3)
[1] 0.5873635
chisq.test(x = c(13, 7, 10, 5), p = c(14, 7, 7, 7)/35 )

    Chi-squared test for given probabilities

data:  c(13, 7, 10, 5)
X-squared = 1.9286, df = 3, p-value = 0.5874

Example #3: Knox College Demographics

One initiative of Knox College is to become more representative of the US population. This raises a question of whether we have succeeded in terms of numbers.

According to Fall 2019 domestic numbers, the data are:

Black Asian Hispanic White
Observed \(109\) \(71\) \(194\) \(635\)
Population \(0.1606\) \(0.0709\) \(0.2312\) \(0.5389\)

Note: Here, we only focus on these four groups. While there are others, their numbers are low.

Example #3: Knox College Demographics

Here is a graphic showing where we were in Fall 2019. The dots represent the observed proportion.

Example #3: Knox College Demographics

In terms of the calculations, we have:

chisq.test(x = c(109, 73, 214, 635), 
           p = c(0.1606, 0.0709, 0.2312, 0.5389))
Error in chisq.test(x = c(109, 73, 214, 635), p = c(0.1606, 0.0709, 0.2312, : probabilities must sum to 1.

What happened? How do we fix it?

Example #3: Knox College Demographics

The fix to the problem of probabilities not summing to one is to include the flag:

chisq.test(x = c(109, 73, 214, 635), 
           p = c(0.1606, 0.0709, 0.2312, 0.5389), 
           rescale.p = TRUE)

    Chi-squared test for given probabilities

data:  c(109, 73, 214, 635)
X-squared = 33.22, df = 3, p-value = 2.895e-07

Example #3: Knox College Demographics

Conclusion:

We would like to test if the proportions of selected racial-ethnic groupings for domestic students are the same between Knox College and the population of the United States. To test this, we examined the racial-ethnic distribution of students at Knox College in 2019 and compared these counts to the expected counts using the chi-squared goodness-of-fit test.

Because the \(p-\)value of less than 0.0001 is much less than our selected value of \(\alpha = 0.05\), we reject the null hypothesis. The distribution of domestic students at Knox College does not currently match the demographic distribution of people in the United States.