4.2 Chi-Squared Test of Independence
In the previous chapter, we used the chi-square statistic to test whether a single categorical variable follows a particular distribution. In this chapter, we extend the chi-square framework to two-way tables, where we have two categorical variables and want to assess whether they are associated. The key question becomes: is the distribution of one variable the same across all levels of the other variable, or does the distribution shift depending on the group? As with our other inference methods, we begin with a simulation-based approach (randomization) and then introduce the mathematical chi-square approximation.
Note that with two-way tables, there is not an obvious single parameter of interest. Instead, research questions focus on how the proportions of the response variable change (or not) across the different levels of the explanatory variable. Because there is no single population parameter to estimate, we focus on hypothesis testing (and not confidence intervals).
Key Concepts
- Test a hypothesis about an association between two categorical variable using a chi-square association test
- Recognize when a chi-square distribution is appropriate for testing an association between two categorical variables
Association Between Categorical Variables
The \(\chi^2\) association test is used to test the relationship between two categorical variables
In this chapter, we will be discussing a new hypothesis test called the \(\chi^2\) (chi-square) association test. The \(\chi^2\) association test is used to test about the relative frequencies (or proportions) of observations for two categorical variables.
Hypotheses
The hypotheses for the \(\chi^2\) association test tends to be described more generally than the other tests that we have seen so far. In fact, rather than using parameters in the hypotheses, we will typically express our hypotheses in words. The hypotheses that we want to test in this test are as follows:
\[ \begin{aligned} H_0{:} & \hspace{.5em} \text{\textit{Variable 1} is not associated with \textit{Variable 2}} \\ H_A{:} & \hspace{.5em} \text{\textit{Variable 1} is associated with \textit{Variable 2}} \end{aligned} \]
If we reject the null hypothesis here, we can only say that there is an association/dependency, but we cannot state exactly what that relationship is.
Class Example 4.2.1: Traffic cameras and injuries
Many cities have introduced traffic cameras at high-risk street intersections in an effort to reduce the severity of traffic accidents. Citizen rights groups have complained about the efficacy of these cameras. In fact, they doubt they have any impact whatsoever. A sample of 2938 reported accidents was collected including the type of accident (Death/Disabling Injury, Evident Injury, or Probable Injury) and camera status at the intersection (Before Camera, After Camera). What are the hypotheses to test the relationship between accident severity and presence of a traffic camera?
Expected counts in two-way tables
As with all hypothesis tests, we assume that the null hypothesis is true when we conduct our test.
The expected counts come from the probability rules. If \(A\) and \(B\) are independent, then \(P(A \cap B) = P(A) * P(B)\)
For the \(\chi^2\) association test, if the null hypothesis is true, the two categorical variables are independent. We use this assumption to figure out the counts we expect to see in the sample. To find the expected count for (Variable 1 group \(i\), Variable 2 group \(j\)):
\[ \begin{aligned} E_{ij} &= n \times p_{ij0} = n \times (p_{i0} \times p_{j0}) \\ &= n \times \left(\frac{\text{Row total}}{n} \times \frac{\text{Column total}}{n}\right) \\ &= \frac{\text{Row total} \times \text{Column total}}{n} \end{aligned} \]
Class Example 4.2.2: Traffic cameras and injuries, continued
The table below summarizes the traffic camera data. Use this data to compute a table of expected counts.
| Before Camera | After Camera | |
|---|---|---|
| Death/Disabling | 61 | 27 |
| Evident | 210 | 136 |
| Probable | 1659 | 845 |
Test statistic
We want to see if the observed counts are far enough away from the expected counts
When we conduct the test, the goal is to see if the counts we observed are farther from the expected counts than we would reasonably expect to see due to random chance. We use the same test-statistic for the \(\chi^2\) test of independence that we used for the \(\chi^2\) goodness-of-fit test:
\[ \chi^2 = \sum \frac{ (\text{Observed} - \text{Expected})^2} {\text{Expected}}. \]
Just like in the last section, a larger test statistic indicates observed data that differs from what \(H_0\) suggests. This should lead us to want to reject \(H_0\). Therefore, this is always considered a right-tailed test.
\(p\)-value for \(\chi^2\) test of independence
To use the \(\chi^2\) distribution, each expected count must be at least 5 or larger
In order for us to be able to use the \(\chi^2\) distribution, all of the expected counts need to be at least 5 or larger. If we have met this condition, then we can use a \(\chi^2\) distribution with \((r-1)\times(c-1)\) degrees of freedom where \(r\) is the number of groups in the first categorical variable (number of rows) and \(c\) is the number of groups in the second categorical variable (number of columns). Our \(p\)-value will be the area in the right-tail beyond our test statistic.
Class Example 4.2.3: Traffic cameras and injuries, continued
Find the \(\chi^2\) test statistic and \(p\)-value using the traffic camera data to test if there is an association between traffic cameras and injury severity. (Check conditions!) What is our conclusion?
Class Example 4.2.4: Star Trek
In Star Trek, the colored uniforms worn by the crew identify their work area. Among Trekkies (fans of the show), they have begun to notice a pattern - they believe there is an association with uniform color and if the character suffers a fatality on the show. The table below gives a breakdown of the various crew colors and the number of fatalities - does this give evidence to suggest they are related?
| Blue | Gold | Red | |
|---|---|---|---|
| Fatality | 7 | 9 | 24 |
| Survival | 129 | 64 | 263 |
Class Activity: Test of Independence
Summary
In this chapter, we extended the chi-square framework to two-way tables, testing whether two categorical variables are independent. The randomization approach shuffles the data under the null hypothesis to build a distribution of the chi-square statistic, while the mathematical approach uses the chi-square distribution with \((R-1) \times (C-1)\) degrees of freedom. Both approaches require that observations are independent and that expected counts are sufficiently large (at least 5 in each cell).
The chi-square test of independence tells us whether two variables are related, but does not tell us how they are related or which groups differ. To understand the nature of the relationship, we must examine the individual cells and their contributions to the chi-square statistic.