Statistical inference : Test for associations
2025-07-07
Chi-square Test for Associations
Pearson Chi-square Test for independence
- The chi-square independence test tests whether observed joint frequency counts \(O_{ij}\) differ from expected frequency counts \(E_{ij}\) under the independence model (the model of independent explanatory variables, \(\pi_{ij} = \pi_{i+} \pi_{+j}\). \(H_0\) is \(O_{ij} = E_{ij}\).
- Determine whether an association exists or whether two variables are dependent on each other or not + Sometimes, \(H_0\) represents the model whose validity is to be tested. Contrast this with the conventional formulation of \(H_0\) as the hypothesis that is to be disproved. The goal in this case is not to disprove the model, but to see whether data are consistent with the model and if deviation can be attributed to chance.
Measures of Occurrence
Measures of Effect
Measures of Impact
Questions
- These tests measure the strength of an association.
- The [Pearson Chi-square Test] is not appropriate when more than about 20% of the cells have an expected cell frequency of less than 5
- When the sample size is small the exact p-values can be calculated (this is prohibitive for large samples); calculation of the exact p-values assumes that the column totals and row totals are fixed.
\[X^2 = \sum \frac{(O_{ij} - E_{ij})^2}{E_{ij}}\]
where \(O_j = p_j n\) and \(E_j = \pi_j n\). The sampling distribution of \(X^2\) approaches the \(\chi_{J-1}^2\) as the sample size \(n \rightarrow \infty\).
Example 1: Question (Taken from Zimbabwe Open University)
A ZOU Regional Coordinator observed that on one weekend school, 150 students who turned up were categorized by gender and programme as shown in
| Gender | Physcology | Development studies | R & A Management | Total |
|---|---|---|---|---|
| Male | 46 | 29 | 28 | 103 |
| Female | 27 | 14 | 6 | 47 |
| Total | 73 | 43 | 34 | 150 |
- this is made up of rows and columns.
record the data in R
- how many males are there?
- how many Physcology students are there?
view the observed values in a neat table
contigency table for the data is given above
Look at the Observed proportions
Calculating expected frequencies
\[ E_{ij}=\frac{O_i\times O_{ij}}{n} \]
\[ E_{11}=\frac{73\times 103}{150}=50.12667 \]
\[ E_{12}=\frac{43\times 103}{150}=29.52667 \]
\[ E_{11}=\frac{34\times 103}{150}=23.34667 \]
\[ E_{11}=\frac{73\times 47}{150}=22.87333 \]
\[ E_{22}=\frac{43\times 47}{150}=13.47333 \]
\[ E_{23}=\frac{34\times 47}{150}=10.65333 \]
Doing this in R yeilds
- fill in the blank with
%*%
Define the hypothesis
- \(H_0\) : There is no association between gender and student programme choice
- \(H_1\) : There is an association between gender and student programme choice
- The test statistic is calculated as follows \[\chi^2=\sum_{j=1}^{c}\sum_{i=1}^{r}\frac{(O_{ij}-E_{ij})^2}{E_{ij}}\]
We reject the null hypothesis at 5% level of significance if \[\chi_2>\chi^{2}_{0.05}(r-1)(c-1)\]
calculating \(\chi^2\)
\[\chi^2=\frac{(46-50.12667)^2}{50.12667}+\frac{(29-29.52667)^2}{29.52667}\\ +\frac{(28-23.34667)^2}{23.34667}+\frac{(27-22.87333)^2}{22.87333}+\\ \frac{(14-13.47333)^2}{13.47333}+\frac{(6-10.65333)^2}{10.65333}\]
\[\chi^2=4.07425\] and in R We can
- The degrees of freedom is
- critical chi-squared value is ?
\[\chi^2_{0.05}(2-1)(3-1)=\chi^2_{0.05}(2)=6\]
- conclusion
Since \(\chi^{2}=4.07425<6\) ,we fail to reject \(H_0\) and conclude that there is no association between gender and student programme choice, is this true? .
in R we can do this manually
Conclusion
since the p-value is greater than
0.05we conclude that there is no association between the two variables ,
the deviance statistic is
\[G^2 = 2 \sum_{ij} O_{ij} \log \left( \frac{O_{ij}}{E_{ij}} \right)\]
\(X^2\) and \(G^2\) increase with the disagreement between the saturated model proportions \(p_{ij}\) and the independence model proportions \(\pi_{ij}\).
The associated p-values for the deviance residuals are
Other than doing the manual labor
- R has a built in function called
chisq.test()and it takes in a table object
recall that
The Yates correction yields more conservative p-values.
These p-values are evidence for rejecting the independence model.
Here is the chi-square test applied to the above data. Recall this data set is 2x3, so the degrees of freedom are \((2-1)(3-1) = 3\). The Yates continuity correction does not apply to data other than 2x2, so the correct = c(TRUE, FALSE) has no effect in chisq.test().
Expected frequencies from the output
- fill in with
expected
Example 2: Helment injury vs wearing a helmet
\[P(Helmet) = \frac{147}{793}\]
\[P(Injury) =\frac{235}{793}\]
\[P(Helmet \cap injury) =\frac{17}{793}\]
Testing the Hypothesis
Define the hypothesis
- \(H_0\): Suffering a head injury is not associated with wearing a helmet
- \(H_1\): There is association between wearing a helmet and suffering a head injury.
Decide the level of significance
- at 5% level of significance
Calculate the test statistic
Calculating expected frequencies
\[ E_{ij}=\frac{O_i\times O_{ij}}{n} \]
\[ E_{11}=\frac{235\times 147}{793}=43.56242 \]
\[ E_{12}=\frac{235\times 646}{793}=191.4376 \]
\[ E_{21}=\frac{558\times 147}{793}=103.4376 \]
\[ E_{11}=\frac{558\times 646}{793}=454.5624 \]
Define the matrix
calculate the observed proportions
- The test statistic is calculated as follows \[\chi^2=\sum_{j=1}^{c}\sum_{i=1}^{r}\frac{(O_{ij}-E_{ij})^2}{E_{ij}}\]
We reject the null hypothesis at 5% level of significance if \[\chi_2>\chi^{2}_{0.05}(r-1)(c-1)\]
calculating \(\chi^2\)
\[\chi^2_{uncorrected}=\frac{(17-43.6)^2}{43.6}+\frac{(218-191.4)^2}{191.4}\\ +\frac{(130-103.4)^2}{103.4}+\frac{(428-454.6)^2}{454.6}\]
- The test statistic to 1 decimal place is given by? ?
Chi-Squared distribution
Random variable \(X\) is distributed \(X \sim \chi^2_k\) if
\[f(X)=\frac{1}{2^{k/2}\Gamma (k/2)} x^{k/2-1} e^{-x/2}\]
with \(E(X)=k\) and \(Var(X) = 2k\).
For our example
\((r-1)(c-1)= (2-1)(2-1)\) = 1
Or simulate this by taking the mean of 10,000 random trials.
How large would \(X^2\) have to be to conclude the observed were not in agreement with expectations?
Example 3: Does type of therapy associated with improvement?
| variable | Improvement | no improvemet | Total |
|---|---|---|---|
| therapy x | 28 | 16 | 44 |
| therapy y | 37 | 9 | 46 |
| Total | 65 | 25 | 90 |
Calculating expected frequencies
\[E_{ij}=\frac{O_i\times O_{ij}}{n}\]
\[E_{11}=\frac{65 \times 44}{90}=31.77778\]
\[E_{12}=\frac{25 \times 44}{90}=12.2222\]
\[E_{21}=\frac{65 \times 46}{90}=33.2222\] \[E_{22}=\frac{25 \times 46}{90}=12.77778\]
- \(H_0\) : The two variables are independent
- \(H_1\) : The two variables are dependent
The test statistic is calculated as follows \[\chi^2=\sum_{j=1}^{c}\sum_{i=1}^{r}\frac{(O_{ij}-E_{ij})^2}{E_{ij}}\]
We reject the null hypothesis at 5% level of significance if \[\chi_2>\chi^{2}_{0.05}(r-1)(c-1)\]
summary table
- fill in the blanks , round to 3 decimal places.
| Observed(O) | Expected(E) | (O-E) | [(O-E)^2]/E |
|---|---|---|---|
| 28 | 31.77778 | 14.27177 | |
| 16 | 12.2222 | 14.27177 | |
| 37 | 33.222 | 14.27328 | |
| 9 | 12.77778 | 14.27162 |
\(\chi^2=\) to 2 decimal places and
\(\chi^2_{0.05}(2-1)(2-1)=\chi^2_{0.05}(1)\)=
conclusion
Since the calculated value is less than than the critical value ,we fail to reject to reject the null hypothesis and conclude that the two variables are independent.