# R for Psychological Research, Lesson 17: Chi-square 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(LearningStyle = case_when(
    LearningStyle == 0 ~ "Visual",
    LearningStyle == 1 ~ "Kinaesthetic",
    LearningStyle == 2 ~ "Auditory",
    LearningStyle == 3 ~ "Reading/Writing"
  ))


# ---- Count ----

style_table <- table(students$Gender, students$LearningStyle)
style_table

prop.table(style_table, margin = 1) %>% round(2)

ggplot(students, aes(x = Gender, fill = LearningStyle)) +
  geom_bar(position = "fill") +
  labs(y = "Proportion", fill = "Learning style")


# ---- Test ----

chi <- chisq.test(style_table)
chi

chi$expected

cramer_v(style_table)
