Chi-Square Test of Independence
Research Question: A university researcher is interested in examining whether student type is associated with pet ownership.
Step 1: Install Required Packages
install.packages(“readxl”) install.packages(“ggplot2”) install.packages(“rcompanion”)
Step 2: Open Installed Packages
library(readxl)
library(ggplot2)
library(rcompanion)
Step 3: Import and Name Dataset
DatasetB2 <- read_excel("C:/Users/srina/OneDrive/Documents/Madhu Master's/Applied Analytics/Assignment 5/DatasetB2.xlsx")
Step 4: Create a Contingency Table
table(DatasetB2$StudentType, DatasetB2$PetOwnership)
##
## No Yes
## Domestic 27 25
## International 23 25
Step 5: Create Bar Charts
ggplot(DatasetB2, aes(x = StudentType, fill = PetOwnership)) +
geom_bar(position = "dodge") +
labs(x = "Student Type",
y = "Frequency",
title = "PetOwnership by StudentType") +
theme(
text = element_text(size = 12),
axis.title = element_text(size = 12),
axis.text = element_text(size = 12),
plot.title = element_text(size = 12),
legend.position = "none"
)
Step 6: Conduct the Chi-Square Test of Independence
chisq.test(DatasetB2$StudentType, DatasetB2$PetOwnership)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: DatasetB2$StudentType and DatasetB2$PetOwnership
## X-squared = 0.040064, df = 1, p-value = 0.8414
The Chi-Square Test of Independence indicated that there was not a significant association between student type and pet ownership, χ²(1) = 0.04, p = .841.