\(\chi^2 = 0\) means observed = expected exactly (perfect independence)
Larger \(\chi^2\) means greater deviation from independence
Larger \(\chi^2\) → stronger evidence against \(H_0\)
But how large is “large enough” to reject \(H_0\)?
We need a reference distribution → chi-square distribution
Randomization Test for Independence
We can randomly permute the response (opinion) to simulate the null hypothesis being true
For each permuted sample, we calculate value of the \(\chi^2\) statistic
Let’s construct a null distribution for the military spending question
Here is the original GSS data with 5 random permutations.
# A tibble: 149 × 8
id natarms party randPerm1 randPerm2 randPerm3 randPerm4 randPerm5
<int> <fct> <chr> <fct> <fct> <fct> <fct> <fct>
1 1 TOO LITTLE Ind Rep Ind Ind Ind Dem
2 2 TOO MUCH Ind Dem Dem Ind Rep Ind
3 3 TOO MUCH Dem Dem Rep Rep Rep Dem
4 4 TOO MUCH Ind Rep Ind Dem Rep Rep
5 5 TOO MUCH Ind Ind Dem Rep Ind Ind
6 6 TOO MUCH Ind Dem Rep Rep Dem Ind
7 7 TOO MUCH Ind Dem Dem Ind Ind Dem
8 8 TOO MUCH Dem Ind Ind Ind Dem Rep
9 9 TOO LITTLE Dem Ind Dem Ind Rep Ind
10 10 TOO LITTLE Ind Dem Dem Ind Rep Ind
# ℹ 139 more rows
Here is the dotplot of the corresponding \(\chi^2\) statistics.
Here is the resulting histogram of 1000 simulations
Histogram of \(X^2\) statistics for 1,000 random permutations. Observed value (\(18.97\)) indicated by dashed vertical line.
Note that the shape of the histogram is neither symmetric nor bell-shaped. In fact, it only uses non-negative values
The p-value is always in the in the right tail (as large or larger than observed \(\chi^2\) stat)
From the histogram, there were no values of \(\chi^2\) that were as extreme as the observed value
So the p-value is approximately 0
We reject null hypothesis
In the context of the problem, we can conclude that there is strong evidence of an association between opinions on military spending and political party (the two variables are not independent)
Test for Independence Using a Mathematical Model
Chi-squared test for assessing independence between categorical variables
When the null-hypothesis is true and the following conditions are met, \(X^2\) has a Chi-squared distribution with \(df=(r-1)\times(c-1)\) degrees of freedom:
Independent observations
Large samples: at least 5 expected counts in each cell
\(r\) is the number of rows and \(c\) is the number of columns in the two-way table (no totals)
Used for genetics, health studies, or marketing data.
Example: Do observed blood type frequencies in a population match known distribution?
Blood Type
Assume that the expected probabilities of various blood types in the general population are:
A = 0.40, B = 0.11, AB = 0.04, O = 0.45
Suppose we have a random sample of 350 people with the following observed blood types:
Blood Type
Observed Count
A
170
B
120
AB
30
O
80
Total
350
Research Question: Is there significant evidence that the distribution of blood types in the sample is different from the population’s distribution?
We need to calculate the \(\chi^2\)-statistic for this data set.
The expected counts are calculated as “Sample Size” \(\times\) “Assumed Probability”
Blood Type
Observed Count
Expected Counts
A
155
350*0.40 = 140
B
40
350*0.11 = 38.5
AB
15
350*0.04 = 14
O
140
350*0.45 = 157.5
If the validity conditions are met (Expected counts \(\ge\) 5) then the distribution of the test statistic is \(\chi^2(d-1)\), where \(d\) is the number of values in the categorical variable. (In our example \(d = 4\))
Chi-square Calculation and p-value
The value of the \(\chi^2\) statistic is:\[\chi^2=\frac{(155-140)^2}{140}+\frac{(40-38.5)^2}{38.5}+\frac{(15-14)^2}{14}+\frac{(140-157.5)^2}{157.5}=3.6815\]
# Observed dataobserved <-c(A =155, B =40, AB =15, O =140)# Assumed distribution of proportionsexpected_prop <-c(A =0.40, B =0.11, AB =0.04, O =0.45)# Expected countstotal <-sum(observed)expected <- total * expected_prop# Chi-square statisticchi_sq <-sum((observed - expected)^2/ expected)chi_sq
[1] 3.681457
# Degrees of freedom = (categories - 1)df <-length(observed) -1df