Skip to content

R - Chi Square Tests

The Chi-Squared (χ²) test of independence is a statistical hypothesis test used to determine whether there is a significant association between two categorical variables. For example, you could use it to test if there’s a relationship between a person’s favorite ice cream flavor (a categorical variable) and their gender (another categorical variable). The variables should be drawn from the same population.

The test works by comparing the observed frequencies in a contingency table with the frequencies that would be expected if there were no association between the variables.

The core function is chisq.test(). It is typically applied to a contingency table (a matrix or table object).

chisq.test(table_object)
  • table_object: A contingency table containing the frequency counts of the categorical variables.

We’ll use the built-in Cars93 dataset (from the MASS package) to investigate if there is a relationship between the Type of car (e.g., Compact, Sporty) and the type of AirBags it has.

Hypotheses:

  • Null Hypothesis (H₀): There is no association between car Type and AirBags (they are independent).
  • Alternative Hypothesis (H₁): There is an association between car Type and AirBags (they are dependent).
# Load the MASS package for the dataset
library(MASS)
# 1. Create a contingency table of observed frequencies
# We use the table() function for this.
car_table <- table(Cars93$Type, Cars93$AirBags)
# Print the table to see the observed counts
print(car_table)
# 2. Perform the Chi-Squared test
chi_test_result <- chisq.test(car_table)
# Print the full test results
print(chi_test_result)

Executing the code produces the following output:

# The contingency table
Driver & Passenger Driver only None
Compact 2 9 5
Large 4 7 0
Midsize 7 11 4
Small 0 5 16
Sporty 3 8 3
Van 0 3 6
# The test result
Pearson's Chi-squared test
data: car_table
X-squared = 33.001, df = 10, p-value = 0.0002723
Warning message:
In chisq.test(car_table) : Chi-squared approximation may be incorrect
  • X-squared = 33.001: This is the test statistic. A larger value indicates a greater difference between observed and expected frequencies.
  • df = 10: The degrees of freedom, calculated as (rows - 1) * (cols - 1) = (6 - 1) * (3 - 1) = 10.
  • p-value = 0.0002723: This is the probability of observing a test statistic as extreme as 33.001 (or more) if the null hypothesis were true. Since this p-value is very small (typically, less than a significance level of 0.05), we reject the null hypothesis. We conclude that there is a statistically significant association between the type of car and its airbag configuration.

Best Practice: Handling the Low Count Warning

Section titled “Best Practice: Handling the Low Count Warning”

The warning Chi-squared approximation may be incorrect appears when some cells in the table have low expected counts (a common rule is if any expected count is < 5). The Chi-squared test relies on an approximation that is less accurate with small samples. You can view the expected counts like this:

# Check the expected frequencies
print(chi_test_result$expected)

When you see this warning, you have two better alternatives:

  1. Fisher’s Exact Test: It calculates an exact p-value and is preferred for small sample sizes. The syntax is very similar.
  2. Monte Carlo Simulation: You can instruct chisq.test to simulate the p-value, which is also more reliable with low counts.
# Alternative 1: Fisher's Exact Test (recommended for small tables)
fisher.test(car_table)
# Alternative 2: Chi-Squared test with simulated p-value
chisq.test(car_table, simulate.p.value = TRUE, B = 10000) # B is number of simulations