Chi-Square Test of Independence

Prepare
Overview of the Test

The Chi-Square Test of Independence is used for categorical data only.

It allows you to determine whether two categorical variables are associated with one another.

In this context, association means that the distribution of one categorical variable differs across the categories of another categorical variable.

The research design is observational. Therefore, the variables are not manipulated by the researcher.

Component General Structure Example
Research Question Is there an association between Variable 1 and Variable 2? Is there an association between gender and voting behavior?
Null Hypothesis There is no association between the two categorical variables. There is no association between gender and voting behavior.
Alternative Hypothesis There is an association between the two categorical variables. There is an association between gender and voting behavior.

The Chi-Square Test of Independence compares the observed frequencies in a contingency table to the frequencies that would be expected if the two variables were completely unrelated.

If the observed frequencies differ substantially from the expected frequencies, the test may indicate that an association exists between the variables.

Independent Variable and Dependent Variable

The Chi-Square Test of Independence does not require a traditional independent variable (IV) and dependent variable (DV).

Because the design is observational, both variables are treated as categorical variables and the goal is to determine whether they are associated.

Assumptions

  • Each observation is independent (each participant appears only once).
  • Categories are mutually exclusive (each participant belongs to only one category within each variable).
  • Expected frequencies should be at least 5 in every cell. You will check this in Step 5.

Interactive Study Tools

Use the Inferential Test Selector to determine which inferential procedure is appropriate for your study.

Inferential Test Selector

Prepare
RScript Code Template

Below is an example of what your code will look like. Your code will be different because the dataset name, variable name, expected proportions, results, and interpretations will depend on your assigned dataset.


library(readxl)

data2026 <- read_excel(
"C:/Users/John/OneDrive/Documents/AA5221/Datasets/political_data.xlsx"
)

polit_table <- table(data2026$gender, data2026$voting)

polit_table

barplot(
polit_table,
beside = TRUE,
col = rainbow(nrow(polit_table)),
legend = rownames(polit_table)
)

chi_result <- chisq.test(polit_table)

chi_result

chi_result$expected

cramer_v <- rcompanion::cramerV(polit_table)

cramer_v

# A Chi-Square Test of Independence was conducted to determine if there was an association between gender and voting behavior.
# The results showed that there was an association between the two variables, χ²(1) = 7.84, p = .005.
# The association was moderate (Cramer's V = .32).

Step 1
Install & Open the Packages

Packages add additional functionality to R.

Package Purpose
readxl Import Excel datasets

Install the Packages

Copy-and-paste the following code into your R Script file.
Run the code once.

install.packages("readxl")

Output

You may see warning messages appear when you install a package. These messages do not necessarily mean that the installation failed. Packages only need to be installed once.


Load the Packages

Copy-and-paste the code below into your R Script.
Keep these lines of code in your R Script.

library(readxl)

Output

No output will appear. This simply opens the packages so you can use their functions in RStudio.

Step 2
Import & Name the Dataset

Although datasets can be imported entirely through code, many students experience difficulty locating file paths. Therefore, this course uses the point-and-click import method to generate the necessary code automatically.

Import the Excel File

  1. Select: File → Import Dataset → From Excel
  2. A new import window will appear. Select: Browse
  3. Locate your assigned Excel dataset.
  4. Select the dataset and choose: Open
  5. Return to the import window and select: Import

Output

Once imported, the dataset will appear in a new tab and will also appear in the Environment pane.

RStudio also automatically generates the code used to import the dataset. This code appears in the Console window. Copy the import code and paste it into your R Script. This allows the dataset to be automatically imported the next time you run your script.

The generated code will vary depending on your file location and file name.

Code Template

DatasetName <- read_excel("filepath")

Example

DatasetName <- read_excel(
"C:/Users/John/OneDrive/Documents/AA5221/Datasets/DatasetName.xlsx"
)
Step 3
Create a Frequency Table

A frequency table summarizes how many observations belong to each category of a categorical variable.

Create the Frequency Table

  1. Copy-and-paste the code template below into your R Script.
  2. Replace dataset with the name of your dataset.
  3. Replace variable1 and variable2 with the names of the two categorical variables you are analyzing.
  4. If you are unsure of the dataset name or variable names, check the Environment pane.

The table() function creates a two-way frequency table showing the number of observations in each combination of the two categorical variables.

Code Template

table(dataset$variable1, dataset$variable2)

Example

Dataset name:
data2026

Variable 1 name:
gender

Variable 2 name:
voting

table(data2026$gender, data2026$voting)

Save the Frequency Table as an Object

Instead of displaying the frequency table only once, it is helpful to save it as an object that can be reused later in your analysis.

Add the <- before the table function to name the object.
The arrow tells RStudio to name the object, and the word it points to is the name you give it.
Here, we have intentionally named the table mytable, because we will use it in the Chi-Square code later.

By writing the word mytable again on a new line, we are "calling" the object and asking RStudio to show it to us in the Console pane.

mytable <- table(dataset$variable1, dataset$variable2)

mytable

Example

polit_table <- table(data2026$gender, data2026$voting)

polit_table

Output

       Voting
Gender  No  Yes
Female  42  158
Male    55  145

This output indicates the number of participants in each combination of gender and voting behavior.

  • 42 female participants reported "No."
  • 158 female participants reported "Yes."
  • 55 male participants reported "No."
  • 145 male participants reported "Yes."

Important:
The order of the categories displayed in the frequency table is important. Later, when interpreting the Chi-Square test, pay attention to the row and column categories shown in your table.

Step 4
Create a Bar Graph

A bar chart provides a visual summary of the frequencies contained in your frequency table. It allows you to quickly compare the number of observations across categories.

The bar chart provides a quick visual comparison of the category counts before conducting the Chi-Square test.


Understanding Bar Charts

Bar charts are appropriate for categorical variables because each category represents a distinct group.

  • Each bar represents one category.
  • The height of the bar represents the frequency (count).
  • Bars are separated because categories are distinct groups.
  • The chart allows you to visually inspect the distribution of the variable.

Create the Bar Chart

Copy and paste the code below into your R Script.

Code Template

barplot(
    mytable,
    beside = TRUE,
    col = rainbow(nrow(mytable)),
    legend = rownames(mytable)
)

Example

barplot(
    polit_table,
    beside = TRUE,
    col = rainbow(nrow(polit_table)),
    legend = rownames(polit_table)
)

Output

The chart displays the observed frequencies from your dataset as bars. Higher bars represent categories with more observations, while shorter bars represent categories with fewer observations.

Step 5
Conduct the Chi-Square Test of Independence

The first line performs the Chi-Square Test of Independence. The second line displays the results.

Code Template

chi_result <- chisq.test(mytable)
chi_result

Example

chi_result <- chisq.test(polit_table)
chi_result

Output

Pearson's Chi-squared test

data:  polit_table
X-squared = 7.84, df = 1, p-value = 0.005

  • X-squared (χ²) = The Chi-Square test statistic. Larger values indicate greater differences between the observed and expected frequencies.
  • Degrees of Freedom (df) = Used to help calculate the p-value. For a Chi-Square Test of Independence, df = (number of rows − 1) × (number of columns − 1).
  • P-Value = Used to determine whether the result is statistically significant.
P-Value Decision Conclusion
p < .05 Statistically Significant There is evidence of an association between the two categorical variables.
p > .05 Not Statistically Significant There is not sufficient evidence of an association between the two categorical variables.

If you need help interpreting your p-value, use the interactive p-value interpreter.

P-Value Interpreter


Check the Expected Counts

The Chi-Square Test of Independence is only trustworthy when the expected count in every cell is 5 or greater. Run the code below to display the expected frequencies.

chi_result$expected

Review the output and confirm that every expected frequency is at least 5. If one or more expected frequencies are below 5, the results may not be trustworthy.

Step 6
Calculate Cramér's V (Effect Size)

For Chi-Square Tests of Independence, the effect size is Cramér's V.

Cramér's V describes the strength of the association between the two categorical variables.

Calculate Cramér's V

Copy-and-paste the following code into your R Script.

The first line calculates Cramér's V. The second line displays the result.

cramer_v <- rcompanion::cramerV(polit_table)

cramer_v

Output

0.40

Interpreting Cramér's V

Use the table below to classify the strength of the association.

Cramér's V Interpretation
Less than 0.10 Negligible Association
0.10 to less than 0.30 Small Association
0.30 to less than 0.50 Moderate Association
0.50 or Higher Large Association
Step 7
Report the Results

After the analyses, report your findings in a few clear sentences in your R Script.

Copy the template and replace the highlighted portions with your output results. There is a standardized method of reporting. DO NOT be creative. Use the provided reporting format.

P-Value Reporting

p-value How to Report
p < .001 Report p < .001
.001 < p < .05 Report the exact p-value to three decimals (example: p = .003)
p > .05 Report p > .05
All other values Report two decimal places (example: .1252 → .13)

Copy-and-paste the template below into your RScript. Replace the bracketed text with information from your analysis.

Chi-Square Reporting Template


# A Chi-Square Test of Independence was conducted to determine if there was an association between [Variable 1] and [Variable 2].
# The results showed that there [was / was not] an association between the two variables, χ²(df) = xx.xx, p = .xxx.
# The association was [small / moderate / large] (Cramer's V = .xx).


Example


# A Chi-Square Test of Independence was conducted to determine if there was an association between gender and voting behavior.
# The results showed that there was an association between the two variables, χ²(1) = 7.84, p = .005.
# The association was moderate (Cramer's V = .32).
Prepare to Publish
Create an RMarkdown File

After completing your R Script, the next step is to convert it into an R Markdown document.

An R Markdown document combines your code, output, and written explanation into a single HTML report. This allows others to reproduce your analysis and view both the code and results in one document.

Before proceeding, review the R Markdown lesson.

R Markdown Files


Create a New R Markdown File

  1. Open RStudio.
  2. Select: File → New File → R Markdown
  3. Ensure that HTML is selected as the output format.
  4. Select OK.

You do not need to complete any of the optional fields. The default settings are sufficient for this assignment.


Save the R Markdown File

  1. Select File → Save As.
  2. Save the file to your desktop.
  3. Delete all of the prewritten sample text generated by RStudio.
  4. Open your completed R Script.
  5. Copy all of your code.
  6. Paste the code into the empty R Markdown document.

Create a Code Chunk

R code inside an R Markdown file must be placed inside a code chunk.

Add the following line at the very beginning of your document:

```{r}

Then add the following line at the very end of your document:

```

All of your code should now be located between the two lines.

Example Structure

```{r}

paste all your code here

```

When the code chunk has been created correctly, the code region will typically appear shaded or highlighted within RStudio.


Step 5: Knit the Document

Once the code chunk has been created, generate the HTML report.

  1. Select the Knit button at the top of the R Markdown document.
  2. The Knit button resembles a ball of yarn with a knitting needle.
  3. Wait for the document to compile.
  4. A new HTML report should open automatically.

The report will display your code, output, tables, charts, and statistical results in a web-friendly format.

Publish
Create an RPubs File

After creating and knitting your R Markdown document, the final step is to publish your report to RPubs.

RPubs allows you to share your analysis as a web page that can be viewed in any browser without requiring RStudio.

Before beginning, review the RPubs lesson.

RPubs


Open Your R Markdown File

  1. Open your completed R Markdown file.
  2. Verify that the document knits successfully and displays all output correctly.
  3. Select the Knit button if you have not already generated the HTML report.

After knitting, the completed HTML report should appear in the Viewer pane or open in a web browser.


Publish to RPubs

  1. Locate the Publish button in the Viewer pane.
  2. Select Publish to RPubs.
  3. Log in to your RPubs account.
  4. If you do not already have an account, create a free RPubs account.
  5. Enter a title for your report.
  6. Optionally enter a description.
  7. Select Publish.

Save the RPubs HTML File

In addition to publishing the report online, save a copy of the HTML file for your records.

  1. Open your published RPubs report in a web browser.
  2. Press Ctrl + S (Windows) or Command + S (Mac).
  3. Choose a location on your computer.
  4. If available, select Webpage, HTML Only.
  5. Save the file.