Pearson's Chi-squared test with Yates' continuity correction
data: obs
X-squared = 1.1052, df = 1, p-value = 0.2931
STAT 200 – Introductory Statistics
February 24, 2026
Parametric Procedure: Chi-Square Test of Independence
mosaicplot(table(x, y))barplot(table(x, y), beside=TRUE)R function: chisq.test(table(x, y))Note: This function is what Hawkes covers (with an adjustment). For R to give you Hawkes’ resultsm you will need to use correct=FALSE in the function.
I would like to test if there is a relationship between whether a person has blue eyes and whether that person is a MNS major.
To do this, I asked 100 people at Knox College and measure if they have blue eyes and if their major is in MNS. This is the contingency table:
| Blue | Not Blue | |
|---|---|---|
| MNS Major | \(7\) | \(28\) |
| Not MNS Major | \(13\) | \(52\) |
Let us ask ourselves this question: “What does it mean for two categorical variables to be independent?”
It means that knowledge of one variable does not provide information about the other.
In this specific example, it means that the probability of a student being an MNS major does not depend on whether that student is blue-eyed or not.
In the language of statistics and random variables, this would mean that the distribution of MNS majors is the same for blue-eyed students as it is for non-blue-eyed students.
First, here is a mosaic plot for this data:
Here is a different mosaic plot (a different view) for this data:
What is the proportion of blue-eyed people who are MNS majors?
\[ \frac{7}{7+13} = 35\% \]
What is the proportion of non-blue-eyed people who are MNS majors?
\[ \frac{28}{28+52} = 35\% \]
It’s a match! Therefore, knowledge of eye color gives no information about major.
What is the proportion of MNS majors who are blue-eyed?
\[ \frac{7}{7+28} = 20\% \]
What is the proportion of non-MNS majors who are blue-eyed?
\[ \frac{13}{13+52} = 20\% \]
It’s a match! Therefore, knowledge of major gives no information about eye color.
So:
In other words: The two variables are independent. Knowledge of one result does not affect the probability of the other.
Now that we’ve seen an example, let us generalize it. Here is some generic data:
| \(A_1\) | \(A_2\) | \(A_3\) | |
|---|---|---|---|
| \(B_1\) | \(x_{11}\) | \(x_{12}\) | \(x_{13}\) |
| \(B_2\) | \(x_{21}\) | \(x_{22}\) | \(x_{23}\) |
Now that we’ve seen an example, let us generalize it. Here is some generic data:
| \(A_1\) | \(A_2\) | \(A_3\) | Row Sum | |
|---|---|---|---|---|
| \(B_1\) | \(x_{11}\) | \(x_{12}\) | \(x_{13}\) | \(r_1\) |
| \(B_2\) | \(x_{21}\) | \(x_{22}\) | \(x_{23}\) | \(r_2\) |
| Column Sum | \(c_1\) | \(c_2\) | \(c_3\) | \(n\) |
If the two variables are perfectly independent, the expected values \(\mu_{ij}\) would be:
| \(A_1\) | \(A_2\) | \(A_3\) | |
|---|---|---|---|
| \(B_1\) | \(\mu_{11} = n \cdot \frac{r_1}{n} \cdot \frac{c_1}{n}\) | \(\mu_{12} = n \cdot \frac{r_1}{n} \cdot \frac{c_2}{n}\) | \(\mu_{13} = n \cdot \frac{r_1}{n} \cdot \frac{c_3}{n}\) |
| \(B_2\) | \(\mu_{21} = n \cdot \frac{r_2}{n} \cdot \frac{c_1}{n}\) | \(\mu_{22} = n \cdot \frac{r_2}{n} \cdot \frac{c_2}{n}\) | \(\mu_{23} = n \cdot \frac{r_2}{n} \cdot \frac{c_3}{n}\) |
The previous slide stated “If the two variables are perfectly independent, the expected values would be…”.
This comes from one definition of independent events: “Events \(A\) and \(B\) are independent if \(\mathbb{P}(A \textrm{ and } B) = \mathbb{P}(A) \times \mathbb{P}(B)\)}.
Thus, under the null hypothesis that the two variables are independent, the probability of a person being in the \(A_1 B_1\) cell is the probability of being \(A_1\) times the probability of being \(B_1\). And then, the expected number of people in that cell is just \(n\) times that probability:
\[ \mu_{11} = n \times \mathbb{P}(A_1) \times \mathbb{P}(B_1) = n \left( \frac{c_1}{n} \right) \left( \frac{r_1}{n} \right) \]
However, since the sample is random, two independent variables cannot always give such perfect results.
Thus, we need to create a test statistic to determine if the differences are likely due to random chance or something systematic.
As before, that test statistic is:
\[ \chi^2 = \sum \frac{(x_i - \mu_i)^2}{\mu_i} \]
As before in the goodness-of-fit test, we are summing up over all of the cells.
The test statistic has a chi-squared distribution with \(df = (r-1)(c-1)\) degrees of freedom.
Continuing the previous example:
\[ \begin{aligned} \chi^2 &= \sum \frac{(x_i - \mu_i)^2}{\mu_i} \\ &= \frac{\left( 7 - \frac{20 \times 35}{100} \right)^2}{\frac{20 \times 35}{100}} + \frac{\left( 28 - \frac{80 \times 35}{100} \right)^2}{\frac{80 \times 35}{100}} \\ &\qquad + \frac{\left( 13 - \frac{20 \times 65}{100} \right)^2}{\frac{20 \times 65}{100}} + \frac{\left( 52 - \frac{65 \times 80}{100} \right)^2}{\frac{65 \times 80}{100}} \\ &= \frac{\left( 7 - 7 \right)^2}{7} + \frac{\left( 28 - 28 \right)^2}{28} + \frac{\left( 13 - 13 \right)^2}{13} + \frac{\left( 52 - 52 \right)^2}{52} \\ &= \frac{0}{7} + \frac{0}{28} + \frac{0}{13} + \frac{0}{52} \\ &= 0 \end{aligned} \]
This test statistic follows a chi-squared distribution with \((2-1)(2-1) = 1\) degree of freedom.
This illustrates the \(p\)-value. Since the value of the test statistic is 0, the entire graphic is shaded, meaning the \(p\)-value is 1.00.
I would like to determine if the proportion of males who wear hats is the same as the proportion of females who do. To test this, I sample 100 males and 100 females. Ten males and 16 females were wearing hats.
The table of observed values:
| Hat | No Hat | Row Sum | |
|---|---|---|---|
| Female | \(16\) | \(84\) | \(100\) |
| Male | \(10\) | \(90\) | \(100\) |
| Column Sum | \(26\) | \(174\) | \(200\) |
This is a mosaic plot of the data:
The table of expected values:
| Hat | No Hat | Row Sum | |
|---|---|---|---|
| Female | \(200 \left( \frac{100}{200} \right) \left( \frac{26}{200} \right)\) | \(200 \left( \frac{100}{200} \right) \left( \frac{174}{200} \right)\) | \(100\) |
| Male | \(200 \left( \frac{100}{200} \right) \left( \frac{26}{200} \right)\) | \(200 \left( \frac{100}{200} \right) \left( \frac{174}{200} \right)\) | \(100\) |
| Column Sum | \(26\) | \(174\) | \(200\) |
The table of expected values:
| Hat | No Hat | Row Sum | |
|---|---|---|---|
| Female | \(13\) | \(87\) | \(100\) |
| Male | \(13\) | \(87\) | \(100\) |
| Column Sum | \(26\) | \(174\) | \(200\) |
The test statistic value is:
\[ \begin{aligned} \chi^2 &= \sum \frac{(x_i - \mu_i)^2}{\mu_i} \\ &= \frac{(16-13)^2}{13} + \frac{(84-87)^2}{87} + \frac{(10-13)^2}{13} + \frac{(90-87)^2}{87} \\ &= \frac{9}{13} + \frac{9}{87} + \frac{9}{13} + \frac{9}{87} \\ &= 0.6923 + 0.1034 + 0.6923 + 0.1034 \\ &= 1.5915 \end{aligned} \]
This test statistic follows a chi-squared distribution with \((2-1)(2-1) = 1\) degree of freedom.
This illustrates the \(p\)-value for a chi-squared distribution of 1 degree of freedom and a test statistic of 1.5915:
The \(p\)-value is:
\[ \begin{aligned} p &= \mathbb{P}(\chi^2 \ge 1.5915) \\ &= \texttt{1 - pchisq(1.5915, df=1)} \\ &= 0.2071 \end{aligned} \]
Brief Conclusion: Because the \(p\)-value of 0.2071 is greater than our usual alpha of \(\alpha = 0.05\), we cannot reject the null hypothesis. There is not significant evidence that the hat-wearing rate differs between males and females.
The R code is:
I would like to determine if different major types have a different grade distribution in my STAT 200 sections.
The table of observed values:
| Major Type | A | B | C | D | F |
|---|---|---|---|---|---|
| MNS | \(57\) | \(72\) | \(61\) | \(40\) | \(22\) |
| HSS | \(11\) | \(21\) | \(39\) | \(28\) | \(18\) |
| HUM | \(10\) | \(11\) | \(21\) | \(15\) | \(20\) |
| ART | \(13\) | \(11\) | \(14\) | \(21\) | \(11\) |
This is a mosaic plot of the data:
This is a mosaic plot of the data:
The R code for the Hawkes test is:
grades = matrix(
c(57, 72, 61, 40, 22, # MNS
11, 21, 39, 28, 18, # HSS
10, 11, 21, 15, 20, # HUM
13, 11, 14, 21, 11), # ART
ncol=5, byrow=TRUE)
rownames(grades) = c("MNS", "HHS", "HUM", "ART")
colnames(grades) = c("A", "B", "C", "D", "F")
chisq.test(grades)
Pearson's Chi-squared test
data: grades
X-squared = 41.672, df = 12, p-value = 3.781e-05
Brief Conclusion:
Because the \(p\)-value of \(3.781 \times 10^{-5}\) is less than our usual alpha of \(\alpha = 0.05\), we should reject the null hypothesis. There is significant evidence that the grade distribution differs among the major types.
This illustrates the \(p\)-value for a chi-squared distribution of 12 degrees of freedom and a test statistic of 41.672.
The Democratic Peace Thesis states that democratic countries are more peaceful than non-democratic countries. It was assumed to be common sense until the early 1970s, when researchers decided to test it and discover that it was not true. This has led to 50+ years of researchers determining under which conditions it is true.
In this example, let us define “peaceful” as a country not being the first users of force in a militarized interstate dispute (MID). The data set includes all MIDs from the 1960s until the mid-2000s.
Let us determine if there is a relationship between government type and whether or not the country was the first user of force in a MID.
Here is a contigency table of the data:
| Government Type | First User | Not First User |
|---|---|---|
| Democracy | \(128\) | \(590\) |
| Anocracy | \(77\) | \(241\) |
| Autocracy | \(220\) | \(368\) |
I obtained this table using the following code:
This is a mosaic plot of the data:
The R code for the Hawkes test is:
Pearson's Chi-squared test
data: dataTable
X-squared = 64.976, df = 2, p-value = 7.773e-15
Brief Conclusion:
Because the \(p\)-value of \(7.77 \times 10^{-15}\) is so muchless than our usual alpha of \(\alpha = 0.05\), we should reject the null hypothesis. There is significant evidence that of a relationship between government type and whether that country was the first user of force in a MID.
This illustrates the \(p\)-value for a chi-squared distribution of 3 degrees of freedom and a test statistic of 64.976.