# 1. Load packages
pacman::p_load( # Load several R packages using the pacman package manager
tidyverse, # A collection of packages for data manipulation and visualization
ggplot2, # Powerful package for creating static, animated and interactive visualizations
estimatr, # Package for fast estimators for regression with weighted data
modelr, # Provides functions for modelling and prediction
kableExtra, # Enhances table creation in R
modelsummary) # Creates tables and plots to summarize statistical models
# 2. read the APAD data
load("APAD.RData") # Read APAD data,
# 3. Process APAD data
APAD <- APAD %>% mutate( # Process APAD data
# Convert news consumption to minutes
# Example: 2 hours and 30 minutes becomes 2*60 + 30 = 150 minutes
news = news_hrs * 60 + news_mins,
# Create binary variable for news consumption
news_yn = case_when(
news < 15 ~ 0, # 0 if less than 15 minutes
news >= 15 ~ 1, # 1 if 15 minutes or more
TRUE ~ as.numeric(NA)), # Handle other cases as missing
# Calculate average discrimination index across multiple domains
dis_index = rowMeans( # Calculate the mean for each row (participant)
select(., # Choose specific discrimination columns from "." (APAD)
dis_trainee, dis_job, dis_school,
dis_house, dis_gov, dis_public),
na.rm = TRUE), # Ignore NA values
# Standardize discrimination index
# scale() standardizes; as.numeric() converts to numeric vector
z_dis_index = scale(dis_index) %>% as.numeric()
)
Stuck?
- Did the
pacman::p_load(...)line run without red error messages? If not, fix that first.load("APAD.RData")fails? The file must sit in your project folder — download it from Absalon and check the Files pane.- Strange results? Session → Restart R, then rerun your script from the top.
- Still stuck? Ask (and answer!) in the t-R-ouble forum on Absalon.
candy in APAD that
randomly "gives" candy to about half of the respondents. Use the coin
from the lecture: rbinom(), flipped once per respondent
(nrow(APAD) flips), so that 1 = "received candy" and 0 =
"no candy".APAD <- APAD %>% mutate( # Add new 'candy' variable to APAD dataset
candy = rbinom( # Generate random binary values (0 or 1)
n = nrow(APAD), # One flip per respondent
size = 1, # Each trial has two outcomes
prob = 0.5)) # 50% chance of getting 1 (candy)
APAD %>%
# Select variables for which I want my balance test,
select(candy, appearance, gender, gewFAKT) %>%
rename(weights = gewFAKT) %>% # Rename the weights variable!
datasummary_balance( # Make a balance table,
formula = ~ candy, # by candy Yes/No
data = . , # Pipe the APAD data here
title = "Gender and physical appearance of those who got candy and those who didn't")
| 0 | 1 | ||||
|---|---|---|---|---|---|
| N | Pct. | N | Pct. | ||
| appearance | Arabic | 29 | 5.3 | 43 | 7.8 |
| Asian | 62 | 11.4 | 52 | 9.5 | |
| Black | 27 | 5.0 | 13 | 2.4 | |
| Southern | 161 | 29.5 | 160 | 29.2 | |
| White | 258 | 47.3 | 273 | 49.8 | |
| gender | Männlich | 279 | 51.2 | 284 | 51.8 |
| Weiblich | 266 | 48.8 | 264 | 48.2 | |
# comment) with your conclusion from the balance table.
Then compare:Because the candy was assigned by a (simulated) coin flip, the groups look similar on gender and physical appearance — randomization balanced them without us controlling for anything. The remaining small differences are chance: with a finite sample, randomization balances groups only on average (and larger groups balance better than small ones).