7.2 Chi-Square: C -> C, Q -> C
Chi-square is the appropriate statistical test when your hypothesis when both your independent (explanatory) variable and your dependent (response) variable are categorical. The chi-square statistic is based on contingency tables. The PDS Video does a nice job of explaining chi-square and contingency tables. For more information on chi-square go to the OpenStax statistics textbook.
For each test, your procedure is as follows:
- Manage your data as needed.
- Run the chi-square test.
- Examine your data by looking at contingency tables.
- If needed, run the post-hoc test to look at which categories are statistically significant.
To run chi-square and related procedures, you will need to load the Tidyverse and sjPlot packages.
We will look at two examples, both from the World Values Survey.

7.2.1 Example 1: Does Ethiopian’s assessment of respect for human rights vary by education? (WVS)
I will begin by creating a small dataset made up of just the variables I want to use here:
- A respondent’s assessment of the nation’s respect for human rights (Q253)
- A respondent’s sense of national pride (Q254)
- A respondent’s level of education, listed as lower, middle, and higher (Q275R)
We are taking this data from the WVS dataset.
With this example, we will test the following hypothesis: Ethiopians with more education are more likely to think that the nation respects human rights.
7.2.1.1 Run the chi-square test
Next, we will run the chi-square test.
We want to focus on the p-value. If it is smaller than .05, then we can reject the null hypothesis. We reject the null hypothesis here because the p-value is .0338. There is a statistically significant relationship.
##
## Pearson's Chi-squared test
##
## data: ethiopia_small$Q253 and ethiopia_small$Q275R
## X-squared = 13.65, df = 6, p-value = 0.0338
7.2.1.2 Examine your data in contingency tables
We can now look at the contingency table for this data. We will look at three tables. They show the following:
- The observed data from the survey.
- What we would expect to find if there was no relationship between the two variables.
- The column percentages.
By comparing table 1 and table 2, we see modest differences. The expected cell values are not notably different than the observed values for column three. The differences are bigger for column 2, especially.
## Cross-Tabulation, Column Proportions
## Q253 * Q275R
## Data Frame: ethiopia_small
## Label: filelabel
##
##
## ------------------------- ------- -------------- -------------- -------------- ---------------
## Q275R Lower Middle Higher Total
## Q253
## A great deal of respect 124 ( 15.7%) 19 ( 7.9%) 25 ( 13.9%) 168 ( 13.9%)
## Fairly much respect 308 ( 39.1%) 89 ( 36.9%) 68 ( 37.8%) 465 ( 38.5%)
## Not much respect 233 ( 29.6%) 80 ( 33.2%) 56 ( 31.1%) 369 ( 30.5%)
## No respect at all 123 ( 15.6%) 53 ( 22.0%) 31 ( 17.2%) 207 ( 17.1%)
## Total 788 (100.0%) 241 (100.0%) 180 (100.0%) 1209 (100.0%)
## ------------------------- ------- -------------- -------------- -------------- ---------------
##
## ----------------------------
## Chi.squared df p.value
## ------------- ---- ---------
## 13.6497 6 0.0338
## ----------------------------
## ethiopia_small$Q275R
## ethiopia_small$Q253 Lower Middle Higher
## A great deal of respect 109.4988 33.48883 25.01241
## Fairly much respect 303.0769 92.69231 69.23077
## Not much respect 240.5062 73.55583 54.93797
## No respect at all 134.9181 41.26303 30.81886
7.2.1.3 Run the post-hoc test
Next, we will run the post-hoc test with the Bonferroni adjustment. As discussed in the PDS video, we have to adjust the p-value that we will use to test the null hypothesis. That adjustment is computed by taking the original p-value we use (.05) and dividing it by the number of comparisons (3 in this instance). This gives us .017.
In looking at the test below, we focus on the raw.p number. In this case, only the first and second columns, lower and middle education levels have a statistically significant difference.
We cannot reject our hypothesis that Ethiopians with more education are more likely to think that the nation respects human rights. There is not a statistically significant relationship.
source('https://raw.githubusercontent.com/PassionDrivenStatistics/R/master/ChiSquarePostHoc.R')
chisq.post.hoc(respect_human_rights$observed, control="bonferroni", popsInRows=FALSE)[,1:2]## Adjusted p-values used the bonferroni method.
## comparison raw.p
## 1 Lower vs. Middle 0.0026
## 2 Lower vs. Higher 0.8622
## 3 Middle vs. Higher 0.1861

7.2.2 Example 2: Does Ethiopian’s national pride vary by education? (WVS)
With this example we will test the following hypothesis: The more education that an Ethiopian has, the higher will be their national pride.
We are taking this data from the WVS dataset. We managed the data above.
7.2.2.1 Run the chi-square test
Let’s run the chi-square test. We want to focus on the p-value. If it is smaller than .05, then we can reject the null hypothesis. We cannot reject the null hypothesis here, because the p-value is .317.
##
## Pearson's Chi-squared test
##
## data: ethiopia_small$Q254 and ethiopia_small$Q275R
## X-squared = 7.0376, df = 6, p-value = 0.3174
7.2.2.2 Examine your data in contingency tables
We can now look at the contingency table for this data. We will look at three tables. They show the following: 1. The observed data from the survey. 2. What we would expect to find if there was no relationship between the two variables. 3. The column percentages.
## Cross-Tabulation, Column Proportions
## Q254 * Q275R
## Data Frame: ethiopia_small
## Label: filelabel
##
##
## ------------------ ------- -------------- -------------- -------------- ---------------
## Q275R Lower Middle Higher Total
## Q254
## Very proud 683 ( 85.2%) 213 ( 88.4%) 162 ( 90.0%) 1058 ( 86.5%)
## Quite proud 91 ( 11.3%) 21 ( 8.7%) 14 ( 7.8%) 126 ( 10.3%)
## Not very proud 18 ( 2.2%) 6 ( 2.5%) 1 ( 0.6%) 25 ( 2.0%)
## Not at all proud 10 ( 1.2%) 1 ( 0.4%) 3 ( 1.7%) 14 ( 1.1%)
## Total 802 (100.0%) 241 (100.0%) 180 (100.0%) 1223 (100.0%)
## ------------------ ------- -------------- -------------- -------------- ---------------
##
## ----------------------------
## Chi.squared df p.value
## ------------- ---- ---------
## 7.0376 6 0.3174
## ----------------------------
## ethiopia_small$Q275R
## ethiopia_small$Q254 Lower Middle Higher
## Very proud 693.798855 208.48569 155.715454
## Quite proud 82.626329 24.82911 18.544563
## Not very proud 16.394113 4.92641 3.679477
## Not at all proud 9.180703 2.75879 2.060507
7.2.2.3 Run the post-hoc test
Next, we will run the post-hoc test with the Bonferroni adjustment. As discussed in the PDS video, we have to adjust the p-value that we will use to test the null hypothesis. That adjustment is computed by taking the original p-value we use (.05) and dividing it by the number of comparisons (3 in this instance). This gives us .017. So, to be statistically significant a pair would need a p-value of .017 or lower.
In looking at the test below we focus on the raw.p number. In this case, none of the p-values are statistically significant.
We cannot reject the null hypothesis here. There is not a statistically significant relationship.
source('https://raw.githubusercontent.com/PassionDrivenStatistics/R/master/ChiSquarePostHoc.R')
chisq.post.hoc(national_pride$observed, control="bonferroni", popsInRows=FALSE)[,1:2]## Adjusted p-values used the bonferroni method.
## comparison raw.p
## 1 Lower vs. Middle 0.4884
## 2 Lower vs. Higher 0.2076
## 3 Middle vs. Higher 0.2585
