Friedman Test

Non-parametric equivalent to repeated measures ANOVA

Importing Libraries

library(haven) 
## Warning: package 'haven' was built under R version 4.4.3
library(ez) 
## Warning: package 'ez' was built under R version 4.4.3
library(tidyverse) 
## Warning: package 'ggplot2' was built under R version 4.4.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ 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) 
## Warning: package 'ggpubr' was built under R version 4.4.3
library(rstatix) 
## Warning: package 'rstatix' was built under R version 4.4.3
## 
## Attaching package: 'rstatix'
## 
## The following object is masked from 'package:stats':
## 
##     filter
library(PMCMRplus)
## Warning: package 'PMCMRplus' was built under R version 4.4.3

Reading source files

paus_df <- read_sav("files/PAUS_English_Translated.sav")

Creating a copy of the mother file

paus_df_copy <- paus_df

Functions

remove_nas <- function(columns, data) {   
  return(data %>% filter(if_all(all_of(columns), ~ !is.na(.)))) 
} 
transform_numeric_to_categorical <- function(column, data) {          
  col_data <-data[[column]]   
  levels <- sort(unique(col_data))   
  label_vec <- as_factor(sort(unique(col_data)))   
  return(ordered(col_data, levels = levels, labels = label_vec)) 
} 
find_shapiro_normal_columns <- function(data) {   
  for (col_name in names(data)) {   
    if (is.numeric(data[[col_name]])) {     
      values <- na.omit(data[[col_name]])          
      if (length(values) >= 3 && length(unique(values)) > 1) {       
        shapiro_result <- shapiro.test(values)              
        if (shapiro_result$p.value > 0.05) {         
          cat("\nColumn:", col_name,             
              "\nW =", round(shapiro_result$statistic, 4),             
              "p-value =", round(shapiro_result$p.value, 4), "\n")       
        }     
      }   
    } 
  } 
}

Converting to Long format

data_long <- paus_df_copy %>%
  gather(key = "time", value = "sleep_sufficiency_score", 
        recovery1.1, recovery1.2, recovery1.3) %>%
  convert_as_factor(sysUserID, time)
data_long$sleep_sufficiency_score <- as.numeric(data_long$sleep_sufficiency_score)
data_long <- remove_nas(c("sysUserID", "chronotyp_tric.1", "time","sleep_sufficiency_score"),data_long)
data_long <- data_long[c("sysUserID", "chronotyp_tric.1", "time","sleep_sufficiency_score")]
data_long <- data_long %>%
  group_by(sysUserID) %>%
  filter(n_distinct(time) == 3) %>%
  ungroup()
data_long <- droplevels(data_long)

Friedman Test

friedman.test(sleep_sufficiency_score ~ time | sysUserID, data = data_long)
## 
##  Friedman rank sum test
## 
## data:  sleep_sufficiency_score and time and sysUserID
## Friedman chi-squared = 10.916, df = 2, p-value = 0.004262

Because the p-value is < 0.05, we can reject the null hypothesis that the mean sleep sufficiency score is the same for the 3 assessments.

To determine which groups contains significant differences, we can do a post-hoc test.For a Friedman Test, the appropriate post-hoc test is the pairwise Wilcoxon rank sum test with a bonferroni correction.

Reference: How to Perform the Friedman Test in R

Post-hoc Test

pairwise.wilcox.test(data_long$sleep_sufficiency_score, data_long$time, p.adj="bonf", paired=TRUE)
## 
##  Pairwise comparisons using Wilcoxon signed rank test with continuity correction 
## 
## data:  data_long$sleep_sufficiency_score and data_long$time 
## 
##             recovery1.1 recovery1.2
## recovery1.2 0.2350      -          
## recovery1.3 0.0047      0.4778     
## 
## P value adjustment method: bonferroni

The post-hoc test reveal that the sleep sufficiency scores in first and third significantly differ similar to the post-hoc results from the two-way ANOVA with repeated measures I have conducted.