# R for Psychological Research, Lesson 16: Paired t-test
# Code from the lesson page, in order. Run it line by line with Ctrl+Enter (Cmd+Enter on a Mac).
# Lines that start with # are comments: R ignores them.


# ---- Set up ----

library(tidyverse)
library(rstatix)
students <- read_csv("data/students.csv", show_col_types = FALSE)


# ---- The question ----

workshop <- students %>% filter(Workshop == "Yes")
nrow(workshop)


# ---- Wide to long ----

workshop_long <- workshop %>%
  select(ID, Wellbeing_T1, Wellbeing_T2) %>%
  pivot_longer(cols = c(Wellbeing_T1, Wellbeing_T2),
               names_to = "Time",
               names_prefix = "Wellbeing_",
               values_to = "Wellbeing")
workshop_long


# ---- Run the test ----

workshop_long %>%
  group_by(Time) %>%
  get_summary_stats(Wellbeing, type = "mean_sd")

t_ws <- workshop_long %>% t_test(Wellbeing ~ Time, paired = TRUE)
t_ws
workshop_long %>% cohens_d(Wellbeing ~ Time, paired = TRUE)

ggplot(workshop_long, aes(x = Time, y = Wellbeing)) +
  geom_boxplot() +
  labs(subtitle = get_test_label(t_ws, detailed = TRUE), y = "Wellbeing (1 to 7)")
