Step 1:Install the Required Packages
install.packages("readxl")
## Installing package into 'C:/Users/pavan/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'readxl' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'readxl'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\pavan\AppData\Local\R\win-library\4.5\00LOCK\readxl\libs\x64\readxl.dll
## to C:\Users\pavan\AppData\Local\R\win-library\4.5\readxl\libs\x64\readxl.dll:
## Permission denied
## Warning: restored 'readxl'
##
## The downloaded binary packages are in
## C:\Users\pavan\AppData\Local\Temp\RtmpwbXUgY\downloaded_packages
install.packages("ggplot2")
## Installing package into 'C:/Users/pavan/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'ggplot2' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\pavan\AppData\Local\Temp\RtmpwbXUgY\downloaded_packages
install.packages("rcompanion")
## Installing package into 'C:/Users/pavan/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'rcompanion' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\pavan\AppData\Local\Temp\RtmpwbXUgY\downloaded_packages
Step 2:Open the Installed Packages
library(readxl)
library(ggplot2)
library(rcompanion)
Step 3:importing the DATASET A2
DatasetA2 <- read_excel("C:/Users/pavan/Desktop/DatasetA2.xlsx")
#DATASET A2 imported successfully
Step 4:Create a Frequency Table
table(DatasetA2$FavoriteDrink)
##
## Coffee Soda Tea Water
## 26 29 28 17
Step 5:Create Bar Charts
ggplot(DatasetA2, aes(x = FavoriteDrink, fill = FavoriteDrink)) +
geom_bar() +
labs(
x = "FavoriteDrink",
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 the Chi-Square Goodness-of-Fit Test
observed <- c(26, 29, 28, 17)
expected <- c(0.25, 0.25, 0.25, 0.25)
chisq.test(x = observed, p = expected)
##
## Chi-squared test for given probabilities
##
## data: observed
## X-squared = 3.6, df = 3, p-value = 0.308
Step 7: Calculate Cohen’s W (Effect Size)
table2 <- table(DatasetA2$StudentID, DatasetA2$FavoriteDrink)
chi_result <- chisq.test(table2)
## Warning in chisq.test(table2): Chi-squared approximation may be incorrect
w <- sqrt(chi_result$statistic / sum(table2))
w
## X-squared
## 1.732051
A chi-square goodness-of-fit test indicated that the observed frequencies were different from the expected frequencies, χ²(2) = 3.6, p = 0.308.The association between the two variables was moderate (Cohen’s W = .40)