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

#Create Groups
Before <- A6Q2$Before
After <- A6Q2$After

Differences <- After - Before

#Calculate the Descriptive Statistics
mean(Before, na.rm = TRUE)
## [1] 76.13299
median(Before, na.rm = TRUE)
## [1] 75.95988
sd(Before, na.rm = TRUE)
## [1] 7.781323
mean(After, na.rm = TRUE)
## [1] 57.17874
median(After, na.rm = TRUE)
## [1] 58.36459
sd(After, na.rm = TRUE)
## [1] 14.39364
# Histogram
hist(Differences,
     breaks = 15,
     col = "blue",
     border = "white")

#Reporting
#Data for the difference in body weight before and after keto diet appears normally distributed.

#Boxplot
boxplot(Differences,
        main = "Body Weight Differences (After - Before)",
        ylab = "Difference in Weight",
        col = "blue",
        border = "darkblue")

#Reporting
#The difference in body weight boxplot has one outlier.

# Shapiro-Wilk
shapiro.test(Differences)
## 
##  Shapiro-Wilk normality test
## 
## data:  Differences
## W = 0.89142, p-value = 0.02856
#Reporting
#Shapiro-Wilk Body Weight Diffrences.
#The data is abnormally distributed, (p =0.023 ).

#Conduct the Wilcoxon Signed-Rank Test
wilcox.test(Before, After, paired = TRUE, na.action = na.omit)
## 
##  Wilcoxon signed rank exact test
## 
## data:  Before and After
## V = 210, p-value = 1.907e-06
## alternative hypothesis: true location shift is not equal to 0
#Calculate the Effect Size
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.877    20    20 large
#Report the Wilcoxon Signed-Rank Test
#A Wilcoxon Signed-Rank Test was conducted to determine if there was a difference in body weight before and after the keto diet.
#Before scores (Mdn = 75,96) were significantly different from after scores (Mdn = 58.36), V = 210, p = <.001.
#The effect size was Large, r = 0.88.