library(readxl)
library(ggplot2)
library(rcompanion)
DatasetA2 <- read_excel("C:/Users/pooja/Downloads/DatasetA2.xlsx")

Creating a Frequency Table

chi_table <- table(DatasetA2$FavoriteDrink)
chi_table
## 
## Coffee   Soda    Tea  Water 
##     26     29     28     17

Creating a Bar Chart

ggplot(DatasetA2, aes(x = FavoriteDrink, fill = FavoriteDrink)) +
  geom_bar() +
  labs(
    x = "FavoriteDrink",
    y = "StudentID",
    title = "student favorite drink"
  ) +
  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"              
  )

observed <- c(25, 30, 25, 15) 
expected <- c(0.20, 0.25, 0.30,0.25)
chi_result <- chisq.test(x = observed, p = expected)
chi_result
## 
##  Chi-squared test for given probabilities
## 
## data:  observed
## X-squared = 7.193, df = 3, p-value = 0.06599

#Calculate Cohen’s W (Effect Size)

w <- sqrt(chi_result$statistic / sum(chi_table))
w
## X-squared 
## 0.2681974

A Chi-square goodness-of-fit test indicated that the observed frequencies were different from the expected frequencies, χ²(3) = 5.53, p < .06. The association between the two variables was strong (Cohen’s W = 1.7)