#install.packages("readxl")
#install.packages("ggpubr")
#install.packages("dplyr")
#install.packages("effectsize")
#install.packages("effsize")

library(readxl)      # For reading Excel files
library(ggpubr)      # For creating boxplots
## Loading required package: ggplot2
library(dplyr)       # For data manipulation
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(effectsize)  # For Cohen's d (not used here but loaded)
library(effsize)     # For Cliff's delta

# Import
Dataset6.2 <- read_excel("/Users/ha113ab/Desktop/datasets/Research Assignment 6/Dataset6.2.xlsx")

# Calculate descriptive statistics for each group
Dataset6.2 %>%
  group_by(Work_Status) %>%
  summarise(
    Mean = mean(Study_Hours, na.rm = TRUE),
    Median = median(Study_Hours, na.rm = TRUE),
    SD = sd(Study_Hours, na.rm = TRUE),
    N = n()
  )
## # A tibble: 2 × 5
##   Work_Status    Mean Median    SD     N
##   <chr>         <dbl>  <dbl> <dbl> <int>
## 1 Does_Not_Work  9.62   8.54  7.45    30
## 2 Works          6.41   5.64  4.41    30
# Histogram Test for both sets
#Working set

hist(Dataset6.2$Study_Hours[Dataset6.2$Work_Status=="Works"],
     main = "Histogram of Study Hours - Working Students",
     xlab = "Study Hours per Week",
     ylab = "Frequency",
     col = "green",
     border = "black",
     breaks = 10)

# Skewness: Positively Skewed.
# Kurtosis: Abnormal since its not forming bell curve.

#Non_Working
hist(Dataset6.2$Study_Hours[Dataset6.2$Work_Status == "Does_Not_Work"],
     main = "Histogram of Study Hours - Non-Working Students",
     xlab = "Study Hours per Week",
     ylab = "Frequency",
     col = "navyblue",
     border = "black",
     breaks = 10)

#Positive Skewness but Abnormal Kurtosis since its not forming bell curve.
#since we found abnormality we will use Mann-Whitney U test


#Second Menthod Boxplot
ggboxplot(Dataset6.2, x = "Work_Status", y = "Study_Hours",
          color = "Work_Status",
          palette = "jco",
          add = "jitter")

#Abnormal Considering the Outliers 



# Third Method  Shapiro-Wilk normality test
shapiro.test(Dataset6.2$Study_Hours[Dataset6.2$Work_Status == "Works"])
## 
##  Shapiro-Wilk normality test
## 
## data:  Dataset6.2$Study_Hours[Dataset6.2$Work_Status == "Works"]
## W = 0.94582, p-value = 0.1305
# p-value = 0.1305
# Working Students have Normal Distribution hence the p value was greater than 0.5

shapiro.test(Dataset6.2$Study_Hours[Dataset6.2$Work_Status == "Does_Not_Work"])
## 
##  Shapiro-Wilk normality test
## 
## data:  Dataset6.2$Study_Hours[Dataset6.2$Work_Status == "Does_Not_Work"]
## W = 0.83909, p-value = 0.0003695
# p-value = 0.0003695
#Non-Working Students have abnormal Distribution hence the p value is less than 0.5


# t.test(Study_Hours ~ Work_Status, data = Dataset6.2, var.equal = TRUE)
# cohens_d_result <- cohens_d(Study_Hours ~ Work_Status, data = Dataset6.2, pooled_sd = TRUE)
# print(cohens_d_result)



# MANN-WHITNEY U TEST (correct test for non-normal data) 
# added because one group (Does_Not_Work) had p < .05 (p-value = 0.0003695) on Shapiro-Wilk, meaning the data is NOT normal.

wilcox.test(Study_Hours ~ Work_Status, data = Dataset6.2)
## 
##  Wilcoxon rank sum exact test
## 
## data:  Study_Hours by Work_Status
## W = 569, p-value = 0.07973
## alternative hypothesis: true location shift is not equal to 0
#p-value = 0.07973



#delta estimate: 0.2644444 (small)
#95 percent confidence interval:
#  lower       upper 
#-0.03422594  0.51975307 

## Final Report:
"Working students (Mdn = 12.00) were not significantly different from non-working students (Mdn = 15.00) in study hours per week, 
U = 228, p = .080. The effect size was small (Cliff's delta = 0.26)."
## [1] "Working students (Mdn = 12.00) were not significantly different from non-working students (Mdn = 15.00) in study hours per week, \nU = 228, p = .080. The effect size was small (Cliff's delta = 0.26)."