Install the required r packages before running the following scripts

This may take for a few minutes.

if(!require(rstatix)){install.packages("rstatix")}
if(!require(dplyr)){install.packages("dplyr")}
if(!require(lsr)){install.packages("lsr")}
if(!require(ggplot2)){install.packages("ggplot2")}
if(!require(coin)){install.packages("coin")}

Load the required library for plotting

library(rstatix)
library(dplyr)      #data_frame, %>%, filter, summarise, group_by
library(lsr)        # cohen's d
library(ggplot2)    # ggplot, stat_…, geom_…, etc
library(coin)       # wilcox.test

When two conditions are dependent (paired)

E.g., when comparing pre-test and post-test of the same participant

#create a dummy dataset as an example
Dataset <- data.frame(score = c(45 ,85, 66, 78, 92, 94, 91, 85, 62, 60,
                             40, 56, 70, 80, 90, 88, 95, 90, 45, 55,
                             84, 88, 88, 90, 92, 93, 91, 85, 80, 73,
                             97, 100, 93, 91, 90, 87, 94, 83, 92, 93),
                   condition = c(rep('condition1', 20), rep('condition2', 20)))

## view our data
#Dataset

Descriptive statistics

#find sample size, mean, and standard deviation for each group
Dataset %>%
  group_by(condition) %>%
  summarise(
    count = n(),
    mean = mean(score),
    sd = sd(score)
  )
## # A tibble: 2 × 4
##   condition  count  mean    sd
##   <chr>      <int> <dbl> <dbl>
## 1 condition1    20  73.4 18.3 
## 2 condition2    20  89.2  6.09

Check data normality assumption

# Shapiro-Wilk normality test for the differences
normality_check <- shapiro.test(Dataset$score) 

if (normality_check$p.value > 0.05){
  print("The data comes from a population that is normally distributed. Please check the result of paired t-test.")
} else {
  print("The data is not normally distribued. Please check the result of wilcoxon test result.")
}
## [1] "The data is not normally distribued. Please check the result of wilcoxon test result."

Paired T-test with effect size

## paired t-test ##
result_t <- t.test(score ~ condition, data = Dataset, paired = TRUE)
result_t 
## 
##  Paired t-test
## 
## data:  score by condition
## t = -3.6498, df = 19, p-value = 0.001704
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -24.939448  -6.760552
## sample estimates:
## mean of the differences 
##                  -15.85
## effect size: Cohen's d ## 
## https://rcompanion.org/handbook/I_04.html
cohensD(score ~ condition,
       data = Dataset,
       method = "paired")
## Warning in cohensD(score ~ condition, data = Dataset, method = "paired"):
## calculating paired samples Cohen's d using formula input. Results will be
## incorrect if cases do not appear in the same order for both levels of the
## grouping factor
## [1] 0.8161143

Wilcoxon signed-rank test with effect size

NOTE: In my research topic, I just need to know whether p-value <0.5 or 0.01. But if you care about getting the exact p-value, you can set the customized parameter by referring to this: https://qiita.com/MITTI12101/items/94a825ffe6e0c11a211a

# Compute wilcoxon test
result_w <- wilcox.test(score ~ condition, data = Dataset, paired = TRUE)
## Warning in wilcox.test.default(x = c(45, 85, 66, 78, 92, 94, 91, 85, 62, :
## cannot compute exact p-value with ties
## Warning in wilcox.test.default(x = c(45, 85, 66, 78, 92, 94, 91, 85, 62, :
## cannot compute exact p-value with zeroes
result_w 
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  score by condition
## V = 11, p-value = 0.003461
## alternative hypothesis: true location shift is not equal to 0
## effect size ##
wilcox_effsize(score ~ condition, data = Dataset, paired = TRUE)
## # A tibble: 1 × 7
##   .y.   group1     group2     effsize    n1    n2 magnitude
## * <chr> <chr>      <chr>        <dbl> <int> <int> <ord>    
## 1 score condition1 condition2   0.613    20    20 large
# # Compute wilcoxon test
# result_w <- wilcox.test(score ~ condition, data = Dataset, paired = TRUE, exact = FALSE, correct = FALSE)
# result_w 
# 
# ## effect size ##
# wilcox_effsize(score ~ condition, data = Dataset, paired = TRUE, exact = FALSE, correct = FALSE)

Boxplot

boxplot_fig <- ggplot(Dataset, aes(x = condition, y = score)) + 
    geom_boxplot(width=0.3) +
    stat_summary(fun = mean, geom = "point", col = "black") +  # Add points to plot
    stat_summary(fun = mean, geom = "text", col = "black", size = 2.7, # Add text to plot
    vjust = 3, aes(label = paste("Mean:", round(..y.., digits = 2)))) +
    xlab("Conditions")+
    ylab("Performance (score)")

boxplot_fig

Bar chart

### Bar chart
barchart_fig <- ggplot(Dataset, aes(x = condition, y = score)) + 
    geom_bar(position = "dodge",
         stat = "summary",
         fun = "mean",
         width=0.3,
         color="black",
         fill="white")+
    stat_summary(fun = mean, geom = "text", col = "black", size = 3, # Add text to plot
    vjust = -0.5, aes(label = paste("Mean:", round(..y.., digits = 2)))) +
    xlab("Conditions")+
    ylab("Performance (score)")

barchart_fig