library(readxl)
library(ggpubr)
## Loading required package: ggplot2
library(effsize)
library(rstatix)
## 
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
## 
##     filter
KetoDiet <- read_excel("C:/Users/tawan/OneDrive - Saint Louis University/AA 5221/Assignment 6/A6Q2.xlsx")
View(KetoDiet)

KetoBefore <- KetoDiet$Before
KetoAfter <- KetoDiet$After

Differences <- KetoAfter - KetoBefore

mean(KetoBefore, na.rm = TRUE)
## [1] 76.13299
median(KetoBefore, na.rm = TRUE)
## [1] 75.95988
sd(KetoBefore, na.rm = TRUE)
## [1] 7.781323
mean(KetoAfter, na.rm = TRUE)
## [1] 57.17874
median(KetoAfter, na.rm = TRUE)
## [1] 58.36459
sd(KetoAfter, na.rm = TRUE)
## [1] 14.39364
hist(Differences,
     breaks = 15,
     col = "blue",
     border = "white")

#Data for the difference KetoWeight appears [abnormally] distributed.

boxplot(Differences,
        main = "Distribution of KetoWeight Differences (After - Before)",
        ylab = "Difference in KetoWeight",
        col = "blue",
        border = "darkblue")

# The difference scores boxplot does not have outliers.

shapiro.test(Differences)
## 
##  Shapiro-Wilk normality test
## 
## data:  Differences
## W = 0.89142, p-value = 0.02856
#Shapiro-Wilk Difference Scores
#The data is abnormally distributed, (p < .05).

#Wilcoxon test
wilcox.test(KetoBefore, KetoAfter, paired = TRUE, na.action = na.omit)
## 
##  Wilcoxon signed rank exact test
## 
## data:  KetoBefore and KetoAfter
## V = 210, p-value = 1.907e-06
## alternative hypothesis: true location shift is not equal to 0
#effect size
df_long <- data.frame(id = rep(1:length(KetoBefore), 2), time = rep(c("KetoBefore", "KetoAfter"), each = length(KetoBefore)), score = c(KetoBefore, KetoAfter))

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 KetoAfter KetoBefore   0.877    20    20 large
#A Wilcoxon Signed-Rank Test was conducted to determine if there was a difference in body-weight before Keto Diet versus after body-weight.
#Before weight (Mdn = 75.96) were significantly different from after weight (Mdn = 58.36), V = 210, p < .001.
#The effect size was medium, r = .89.