MC³ LabCUHK Psychology

Keep this open while you work

Cheat sheet

Which test?

One group vs a number

One-sample t-test

t_test(y ~ 1, mu = 4)

Two groups of different people

Two-group t-test

t_test(y ~ group)

Same people, twice

Paired t-test

t_test(y ~ Time, paired = TRUE)

Two numbers together?

Correlation

cor.test(x, y)

Two categories related?

Chi-square test

chisq.test(table(a, b))

Set up and load

Task Code
Install a package (once) install.packages("rstatix")
Load a package (each session) library(tidyverse)
Read CSV / Excel / SPSS read_csv("data/file.csv"), read_excel(), read_sav()
Look at data glimpse(d), head(d), View(d)
Count missing values sum(is.na(d$x))

Manage data

Task Code
Keep columns select(ID, Age), select(-ID)
Keep rows filter(Age >= 21, Gender == "Female")
Sort arrange(desc(ExamScore))
New column mutate(Change = T2 - T1)
Two groups mutate(G = if_else(Age >= 21, "Older", "Younger"))
Recode mutate(S = case_when(x == 0 ~ "Visual", x == 1 ~ "Auditory"))
Order groups mutate(M = factor(M, levels = c("Low", "Medium", "High")))
Summary per group group_by(G) %>% summarise(n = n(), m = mean(y, na.rm = TRUE))
Count values count(G)
Wide to long pivot_longer(cols = c(T1, T2), names_to = "Time", values_to = "Score")
Keep the change d <- d %>% mutate(...)

Describe

Task Code
Middle mean(x, na.rm = TRUE), median(x, na.rm = TRUE)
Spread sd(), IQR(), min(), max()
All at once describe() (psych), get_summary_stats(y, type = "mean_sd") (rstatix)
z-scores mutate(z = (x - mean(x)) / sd(x))
z-test z.test(x, mu = , sigma.x = ) (BSDA)

Charts

Chart Code
Histogram ggplot(d, aes(x = y)) + geom_histogram(binwidth = 5)
Bar chart ggplot(d, aes(x = g, fill = h)) + geom_bar(position = "dodge")
Boxplot ggplot(d, aes(x = g, y = y)) + geom_boxplot()
Scatter + line ggplot(d, aes(x = x, y = y)) + geom_point() + geom_smooth(method = "lm")
Labels and look + labs(title = , x = , y = ) + theme_classic()
Save ggsave("chart.png", width = 6, height = 4, dpi = 300)

Tests

Task Code
Sample size pwr.t.test(d = 0.5, sig.level = .05, power = .80, type = "two.sample")
Normality shapiro_test(y), ggplot(d, aes(sample = y)) + stat_qq() + stat_qq_line()
Outliers identify_outliers(y)
Effect size (t-test) cohens_d(y ~ group)
Chart label labs(subtitle = get_test_label(result, detailed = TRUE))
Spearman cor.test(x, y, method = "spearman")
Expected counts chisq.test(t)$expected
Cramér's V cramer_v(t)

Reporting templates

  • t-test: M = 4.43, SD = 1.05, t(299) = 7.10, p < .001, d = 0.41
  • Correlation: r(292) = .69, 95% CI [.62, .74], p < .001
  • Chi-square: χ²(1, N = 300) = 5.33, p = .021, V = .14