1. Use the APAD survey dataset from Absalon. Replicate the variable preparation steps as demonstrated in my slides.
# 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?

  1. Create a new variable called APAD$candy that simulates randomly "giving" candy to half of the respondents in your dataset. Follow these steps: a) Use the rbinom() function to generate this variable. b) To understand how rbinom() works, you can use R's built-in help function by typing ?rbinom in the console, search online for "R rbinom() examples", or use an AI assistant like ChatGPT or Google Gemini for an explanation. c) Make sure your code assigns a value of 1 (representing "received candy") to approximately 50% of the respondents, and 0 to the others.
APAD <- APAD %>% mutate( # Add new 'candy' variable to APAD dataset
  candy = rbinom( # Generate random binary values (0 or 1)
    nrow(APAD), # One value per row
    size = 1, # Each trial has two outcomes
    prob = 0.5)) # 50% chance of getting 1 (candy)
  1. In the APAD survey, we also asked respondents about their gender and how their physical appearance is typically described in Germany, with answers ranging from White, over Black, to Asian. Conduct a balance test to determine if racial appearance is evenly distributed across getting candy. What do you conclude from the balance table? Also, which groups seem rather well, and which ones seem less well balanced?
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")
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 Mnnlich 279 51.2 284 51.8
Weiblich 266 48.8 264 48.2
  1. Write one or two sentences in your R script (as a # 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).