library(readxl)
library(rcompanion)

A6Q2 <- read_excel("C:/Users/nehab/OneDrive/A5221/Assignment 6/A6Q2.xlsx")

# Descriptive statistics for body weight before the keto diet
mean(A6Q2$Before)
## [1] 76.13299
sd(A6Q2$Before)
## [1] 7.781323
median(A6Q2$Before)
## [1] 75.95988
# Descriptive statistics for body weight after the keto diet
mean(A6Q2$After)
## [1] 57.17874
sd(A6Q2$After)
## [1] 14.39364
median(A6Q2$After)
## [1] 58.36459
# Calculate the difference scores
A6Q2$difference <- A6Q2$Before - A6Q2$After

# Examine the distribution of the difference scores
hist(
  A6Q2$difference,
  main = "Distribution of Weight Difference Scores",
  xlab = "Weight Before Minus Weight After"
)

boxplot(
  A6Q2$difference,
  main = "Weight Difference Scores",
  ylab = "Before Minus After"
)

# Test normality of the difference scores
shapiro.test(A6Q2$difference)
## 
##  Shapiro-Wilk normality test
## 
## data:  A6Q2$difference
## W = 0.89142, p-value = 0.02856
# The difference scores are not normally distributed because p = .029.
# Therefore, a Wilcoxon Signed-Rank Test will be used.

# Create separate Before and After variables
Before <- A6Q2$Before
After <- A6Q2$After

# Conduct the Wilcoxon Signed-Rank Test
wilcox.test(
  Before,
  After,
  paired = TRUE,
  exact = FALSE
)
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  Before and After
## V = 210, p-value = 9.569e-05
## alternative hypothesis: true location shift is not equal to 0
# Calculate the effect size
wilcoxonPairedR(
  Before,
  After
)
##  r 
## -1

Interpretation

A Wilcoxon Signed-Rank Test was conducted to compare body weight before and after participants followed the keto diet. Body weight was significantly lower after the diet (Mdn = 58.36) than before the diet (Mdn = 75.96), V = 210, p < .001. The effect size was large (r = -1.00). Therefore, the null hypothesis was rejected.