Chi-Square Test of Independence in R (With Examples)


A Chi-Square Test of Independence is used to determine whether or not there is a significant association between two categorical variables.

This tutorial explains how to perform a Chi-Square Test of Independence in R.

Example: Chi-Square Test of Independence in R

Suppose we want to know whether or not gender is associated with political party preference. We take a simple random sample of 500 voters and survey them on their political party preference. The following table shows the results of the survey:

  Republican Democrat Independent Total
Male 120 90 40 250
Female 110 95 45 250
Total 230 185 85 500

Use the following steps to perform a Chi-Square Test of Independence in R to determine if gender is associated with political party preference.

Step 1: Create the data.

First, we will create a table to hold our data:

#create table
data <- matrix(c(120, 90, 40, 110, 95, 45), ncol=3, byrow=TRUE)
colnames(data) <- c("Rep","Dem","Ind")
rownames(data) <- c("Male","Female")
data <- as.table(data)

#view table
data

       Rep Dem Ind
Male   120  90  40
Female 110  95  45

Step 2: Perform the Chi-Square Test of Independence.

Next, we can perform the Chi-Square Test of Independence using the chisq.test() function:

#Perform Chi-Square Test of Independence
chisq.test(data)

	Pearson's Chi-squared test

data:  data
X-squared = 0.86404, df = 2, p-value = 0.6492

The way to interpret the output is as follows:

  • Chi-Square Test Statistic: 0.86404
  • Degrees of freedom: 2 (calculated as #rows-1 * #columns-1)
  • p-value: 0.6492

Recall that the Chi-Square Test of Independence uses the following null and alternative hypotheses:

  • H0: (null hypothesis) The two variables are independent.
  • H1: (alternative hypothesis) The two variables are not independent.

Since the p-value (0.6492) of the test is not less than 0.05, we fail to reject the null hypothesis. This means we do not have sufficient evidence to say that there is an association between gender and political party preference.

In other words, gender and political party preference are independent.

Additional Resources

An Introduction to the Chi-Square Test of Independence
Chi-Square Test of Independence Calculator
How to Calculate the P-Value of a Chi-Square Statistic in R
How to Find the Chi-Square Critical Value in R

3 Replies to “Chi-Square Test of Independence in R (With Examples)”

  1. In your example, there’s not enough data to disprove the null hypothesis (no association between gender and political party). But it doesn’t mean the null hypothesis is necessarily correct.

    Therefore, your second last paragraph is correct. However, your last sentence is not correct. Gender and political party preference are not proven to be independent. There is just not enough sample size to prove they are dependent. There is no way to prove a negative result.

    1. Hi Lydia…You’re absolutely correct about the interpretation of the Chi-Square Test of Independence and the limits of what statistical tests can prove. Let me clarify:

      1. **Null Hypothesis and Independence**: The null hypothesis in a Chi-Square Test of Independence states that two variables are independent (e.g., gender and political party preference). A lack of statistical significance does not “prove” independence; it only indicates that there is insufficient evidence to reject the null hypothesis given the sample size and data.

      2. **Proof and Statistical Testing**: In statistics, we don’t “prove” hypotheses; instead, we assess whether the data provide sufficient evidence to reject the null hypothesis. When the null hypothesis is not rejected, it means the data do not offer strong enough evidence to conclude an association exists—but this does not confirm the null hypothesis as true.

      3. **Sample Size and Power**: A small sample size can result in low statistical power, making it difficult to detect an actual association even if one exists. In such cases, failure to reject the null hypothesis may simply reflect insufficient data, not true independence.

      4. **Misinterpretation of Results**: The final sentence in the example you referenced should emphasize this nuance. Saying “gender and political party preference are proven to be independent” is incorrect. The correct interpretation would be: “The data do not provide sufficient evidence to conclude an association between gender and political party preference, but this does not prove independence.”

      Thank you for highlighting this point! It’s an important distinction in understanding statistical inference and communicating results accurately.

Leave a Reply

Your email address will not be published. Required fields are marked *