Installing packages and loading them
library(readxl)
library(ggpubr)
## Loading required package: ggplot2
library(rcompanion)
Loading the data
data1 <- read_excel("A5Q1.xlsx")
observed <- table(data1$flavor)
observed
##
## Chocolate Mango Strawberry Vanilla
## 87 32 57 74
Bar chart
barplot(observed,
main = "flavor",
xlab = "flavor",
ylab = "Count",
col = rainbow(length(observed)))
Create the Expected Data
expected <- c(0.20, 0.20, 0.20, 0.40)
Conduct the Chi-Square Goodness-of-Fit Test
chi_result <- chisq.test(observed, p = expected)
chi_result
##
## Chi-squared test for given probabilities
##
## data: observed
## X-squared = 41.6, df = 3, p-value = 4.878e-09
Calculate Cohen’s W (Effect Size)
w <- sqrt(as.numeric(chi_result$statistic) / sum(observed))
w
## [1] 0.4079216