Chi-Square Goodness-of-Fit Test Research Question: Do students prefer tea, coffee, soda and water equally?
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
DatasetA2 <- read_excel("C:/Users/srina/OneDrive/Documents/Madhu Master's/Applied Analytics/Assignment 5/DatasetA2.xlsx")
Step 4: Create Frequency Table
table(DatasetA2$FavoriteDrink)
##
## Coffee Soda Tea Water
## 26 29 28 17
Step 5: Create Bar Chart
ggplot(DatasetA2, aes(x = FavoriteDrink, fill = FavoriteDrink)) +
geom_bar() +
labs(x = "Favorite Drink",
y = "Frequency",
title = "Distribution of FavoriteDrink") +
theme(
text = element_text(size = 14),
axis.title = element_text(size = 14),
axis.text = element_text(size = 14),
plot.title = element_text(size = 14),
legend.position = "none"
)
Step 6: Conduct Chi-Square Goodness-of-Fit Test
observed <- table(DatasetA2$FavoriteDrink)
expected <- c(1/4, 1/4, 1/4, 1/4)
chisq.test(x = observed, p = expected)
##
## Chi-squared test for given probabilities
##
## data: observed
## X-squared = 3.6, df = 3, p-value = 0.308
A chi-square goodness-of-fit test indicated that the observed frequencies were not significantly different from the expected frequencies, χ²(3) = 3.60, p = .308. The association between the observed and expected distributions was weak (Cohen’s W = .14).