Setting up
library(ez)
library(tidyverse)
library(data.table)
rm(list=ls())
This tutorial introduces five core statistical analyses commonly used in psychology and behavioral research. Each scenario gives you:
You’ll use:
Bonus activities:
A cognitive psychologist wants to test whether reaction time (RT) differs across three levels of task difficulty (Easy, Medium, Hard). Each participant completes all three tasks.
set.seed(1)
rm_data <- data.table(
id = rep(1:30, each = 3),
difficulty = rep(c("Easy", "Medium", "Hard"), times = 30),
rt = c(rnorm(30, mean = 500, sd = 40),
rnorm(30, mean = 550, sd = 40),
rnorm(30, mean = 600, sd = 40))
)
head(rm_data)
A sleep study measures accuracy under two lighting conditions (Bright vs Dim) and two times of day (Morning vs Evening). Each participant completes all four combinations.
tw_data[, accuracy := rnorm(.N,
mean = fifelse(light == "Bright" & time == "Morning", 90,
fifelse(light == "Bright", 85,
fifelse(time == "Morning", 80, 75))),
sd = 5)]
head(tw_data)
Group A (Training) and Group B (Control) complete a memory test before and after a training intervention.
set.seed(3)
mx_data <- CJ(id = 1:60, time = c("Pre", "Post"))
mx_data[, group := rep(rep(c("Training", "Control"), each = 30), each = 2)]
mx_data[, score := ifelse(group == "Training" & time == "Post",
rnorm(.N, mean = 85, sd = 5),
rnorm(.N, mean = 75, sd = 5))]
A researcher explores the relationship between screen time and sleep quality.
set.seed(4)
cor_data <- data.table(
screen_time = runif(50, 1, 10)
)
cor_data[, sleep_quality := 100 - screen_time * 6 + rnorm(.N, 0, 5)]
You want to predict exam performance from hours of study.
set.seed(5)
reg_data <- data.table(
study_hours = runif(60, 0, 20)
)
reg_data[, exam_score := 50 + 2.5 * study_hours + rnorm(.N, 0, 5)]