#install.packages("readxl")
#install.packages("ggpubr")
#install.packages("effectsize")
#install.packages("rstatix")"




library(readxl)
library(ggpubr)
## Loading required package: ggplot2
library(effectsize)
library(rstatix)
## 
## Attaching package: 'rstatix'
## The following objects are masked from 'package:effectsize':
## 
##     cohens_d, eta_squared
## The following object is masked from 'package:stats':
## 
##     filter
Dataset6.4 <- read_excel("/Users/ha113ab/Desktop/datasets/Research Assignment 6/Dataset6.4.xlsx")


Before <- Dataset6.4$Stress_Pre
After <- Dataset6.4$Stress_Post

Differences <- After - Before

#Descriptive Statistics

mean(Before, na.rm = TRUE)
## [1] 51.53601
median(Before, na.rm = TRUE)
## [1] 47.24008
sd(Before, na.rm = TRUE)
## [1] 17.21906
mean(After, na.rm = TRUE)
## [1] 41.4913
median(After, na.rm = TRUE)
## [1] 40.84836
sd(After, na.rm = TRUE)
## [1] 18.88901
# Differences Hisatogram Visualization
hist(Differences,
     main = "Histogram of Difference Scores (Post - Pre)",
     xlab = "Value",
     ylab = "Frequency",
     col = "blue",
     border = "black",
     breaks = 20)

#histogram is not symmetrical and negatively skewed and not normal

#Method 2 Boxplot
boxplot(Dataset6.4$Stress_Post - Dataset6.4$Stress_Pre, 
        main = "Distribution of Difference Scores (Post - Pre)",
        ylab = "Difference in Scores",
        col = "green",
        border = "black")

# Abnormal data Distribution there are outliers on the far end 


# since the data is not normal
# Differences

shapiro.test(Differences)
## 
##  Shapiro-Wilk normality test
## 
## data:  Differences
## W = 0.87495, p-value = 0.0008963
#p-value = 0.0008963 ~ 0.01 less than 0.5 proceed with wilcoxon sign rank test

#Inferential Test

wilcox.test(Before, After, paired = TRUE)
## 
##  Wilcoxon signed rank exact test
## 
## data:  Before and After
## V = 620, p-value = 2.503e-09
## alternative hypothesis: true location shift is not equal to 0
#p-value = 2.503e-09 = 0.000000002503 ~ 0.001

#Since the P value is less than 0.5 we will proceed with calculating the Effect Size with Rank Biserial 

df_long <- data.frame(
  id = rep(1:length(Before), 2),
  time = rep(c("Before", "After"), each = length(Before)),
  score = c(Before, After)
)

wilcox_effsize(df_long, score ~ time, paired = TRUE)
## # A tibble: 1 × 7
##   .y.   group1 group2 effsize    n1    n2 magnitude
## * <chr> <chr>  <chr>    <dbl> <int> <int> <ord>    
## 1 score After  Before   0.844    35    35 large
# 0.844  it was a very large effect size.


#FINAL REPORT
"There is a significant change in stress levels between students 
who participated in the mindfulness training program before and after. 
Prior to the intervention, stress levels were Mdn = 47.24 (M = 51.54, SD = 17.22), 
and after the intervention, stress levels decreased to Mdn = 40.85 (M = 41.49, SD = 18.89). 
A Wilcoxon signed-rank test indicated that this reduction was statistically significant, 
V = 1027, p < .001, with a large effect size (rank biserial correlation = 0.844).

."
## [1] "There is a significant change in stress levels between students \nwho participated in the mindfulness training program before and after. \nPrior to the intervention, stress levels were Mdn = 47.24 (M = 51.54, SD = 17.22), \nand after the intervention, stress levels decreased to Mdn = 40.85 (M = 41.49, SD = 18.89). \nA Wilcoxon signed-rank test indicated that this reduction was statistically significant, \nV = 1027, p < .001, with a large effect size (rank biserial correlation = 0.844).\n\n."