# R for Psychological Research, Lesson 15: Two-group 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) %>%
  mutate(Age_Group = if_else(Age >= 21, "21 and over", "Under 21"))


# ---- Describe and check ----

students %>%
  group_by(Age_Group) %>%
  get_summary_stats(Wellbeing_T1, type = "mean_sd")


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

t_age <- students %>% t_test(Wellbeing_T1 ~ Age_Group)
t_age
students %>% cohens_d(Wellbeing_T1 ~ Age_Group)


# ---- Show it ----

ggplot(students, aes(x = Age_Group, y = Wellbeing_T1)) +
  geom_boxplot() +
  labs(subtitle = get_test_label(t_age, detailed = TRUE),
       x = "Age group", y = "Wellbeing at T1 (1 to 7)")
