library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggpubr)
library(rstatix)
## 
## Attaching package: 'rstatix'
## 
## The following object is masked from 'package:stats':
## 
##     filter
library(readxl)
# Data preparation
# Wide format
Libro1 <- read_excel("Libro1.xlsx")
##View(Libro1)
head(Libro1, 3)
## # A tibble: 3 × 5
##      id    X1    X2    X3    X4
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1     1  2.21  3.86  4.56  4.68
## 2     2  2.70  4.05  4.60  4.74
## 3     3  2.78  4.25  4.70  4.88
# Gather columns X1, X2, X3 and X4 into long format
# Convert id and time into factor variables
Libro1 <- Libro1 %>%
  gather(key = "time", value = "score", X1, X2, X3, X4) %>%
  convert_as_factor(id, time)
head(Libro1, 3)
## # A tibble: 3 × 3
##   id    time  score
##   <fct> <fct> <dbl>
## 1 1     X1     2.21
## 2 2     X1     2.70
## 3 3     X1     2.78
Libro1 %>%
  group_by(time) %>%
  get_summary_stats(score, type = "mean_sd")
## # A tibble: 4 × 5
##   time  variable     n  mean    sd
##   <fct> <fct>    <dbl> <dbl> <dbl>
## 1 X1    score       30  2.97 0.297
## 2 X2    score       30  4.50 0.332
## 3 X3    score       30  4.96 0.334
## 4 X4    score       30  5.21 0.311
bxp <- ggboxplot(Libro1, x = "time", y = "score", add = "point")
bxp

Libro1 %>%
  group_by(time) %>%
  identify_outliers(score)
## # A tibble: 1 × 5
##   time  id    score is.outlier is.extreme
##   <fct> <fct> <dbl> <lgl>      <lgl>     
## 1 X1    1      2.21 TRUE       FALSE
Libro1 %>%
  group_by(time) %>%
  shapiro_test(score)
## # A tibble: 4 × 4
##   time  variable statistic     p
##   <fct> <chr>        <dbl> <dbl>
## 1 X1    score        0.977 0.755
## 2 X2    score        0.962 0.348
## 3 X3    score        0.971 0.565
## 4 X4    score        0.963 0.366
ggqqplot(Libro1, "score", facet.by = "time")

res.aov <- anova_test(data = Libro1, dv = score, wid = id, within = time)
get_anova_table(res.aov)
## ANOVA Table (type III tests)
## 
##   Effect DFn DFd        F        p p<.05   ges
## 1   time   3  87 3293.515 2.01e-89     * 0.885
# pairwise comparisons
pwc <- Libro1 %>%
  pairwise_t_test(
    score ~ time, paired = TRUE,
    p.adjust.method = "bonferroni"
    )
pwc
## # A tibble: 6 × 10
##   .y.   group1 group2    n1    n2 statistic    df        p    p.adj p.adj.signif
## * <chr> <chr>  <chr>  <int> <int>     <dbl> <dbl>    <dbl>    <dbl> <chr>       
## 1 score X1     X2        30    30    -61.3     29 3.12e-32 1.87e-31 ****        
## 2 score X1     X3        30    30    -72.4     29 2.58e-34 1.55e-33 ****        
## 3 score X1     X4        30    30    -87.2     29 1.18e-36 7.08e-36 ****        
## 4 score X2     X3        30    30    -22.1     29 1.10e-19 6.60e-19 ****        
## 5 score X2     X4        30    30    -32.4     29 2.58e-24 1.55e-23 ****        
## 6 score X3     X4        30    30     -9.40    29 2.66e-10 1.6 e- 9 ****
# Visualization: box plots with p-values
pwc <- pwc %>% add_xy_position(x = "time")
bxp + 
  stat_pvalue_manual(pwc) +
  labs(
    subtitle = get_test_label(res.aov, detailed = TRUE),
    caption = get_pwc_label(pwc)
  )