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

Before <- A6Q2$Before
After <- A6Q2$After

# Calculate the difference scores
Differences <- After - Before

# I corrected the difference-score calculation.
# I originally calculated Before minus After.
# The correct calculation from the answer key is After minus Before.

# Descriptive statistics before the keto diet
mean(Before, na.rm = TRUE)
## [1] 76.13299
median(Before, na.rm = TRUE)
## [1] 75.95988
sd(Before, na.rm = TRUE)
## [1] 7.781323
# Descriptive statistics after the keto diet
mean(After, na.rm = TRUE)
## [1] 57.17874
median(After, na.rm = TRUE)
## [1] 58.36459
sd(After, na.rm = TRUE)
## [1] 14.39364
# I added na.rm = TRUE to the descriptive-statistics code
# so missing values will be removed if they are present.

# Examine the distribution of the difference scores
hist(
  Differences,
  breaks = 15,
  col = "blue",
  border = "white",
  main = "Distribution of Score Differences (After - Before)",
  xlab = "Difference in Scores"
)

# Data for the difference scores appears abnormally distributed.

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

# The difference-scores boxplot has one outlier.
# I corrected the histogram and boxplot to use After minus Before
# and added the formatting from the answer key.

# Test normality
shapiro.test(Differences)
## 
##  Shapiro-Wilk normality test
## 
## data:  Differences
## 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.

# 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
# I corrected the effect-size code.
# I originally used wilcoxonPairedR(), which produced r = -1.00.
# I used wilcox_effsize() from the answer key, which produces r = .877.

Interpretation

A Wilcoxon Signed-Rank Test was conducted to determine whether there was a difference in participants’ body weight before versus after the keto diet. Body weight before the diet (Mdn = 75.96) was significantly different from body weight after the diet (Mdn = 58.36), V = 210, p < .001. The effect size was large, r = .877. Therefore, the null hypothesis was rejected.

I corrected the difference scores to use After minus Before. I also added na.rm = TRUE, corrected the graphs, and replaced the original effect-size calculation with the method provided in the answer key.