Example Data

All code in this manual runs on one simulated dataset, so you can run every example yourself. The data are made up: they were generated by the code below, so we know the true relationships behind them.

Load the Data

There are three files in the data folder:

  • mydata.csv: the raw data, with item-level responses and missing values. Use it for data cleaning, missing values, and reliability.
  • mydata_scored.csv: the same data plus scale scores. Use it for the analysis chapters.
  • mydata_imputed.csv: mydata_scored.csv with the missing values filled in, the first of five datasets imputed with mice. Use it only where a method needs complete data, such as SEM by Hand; a real analysis uses all imputed datasets (Missing Values).
mydata <- read.csv("data/mydata.csv")
mydata_scored <- read.csv("data/mydata_scored.csv")
mydata_imputed <- read.csv("data/mydata_imputed.csv")

Codebook

Variable Description Values
id Participant ID 1-500
age Age in years 18-80
age_group Age group 1 = 18-24, 2 = 25-29, 3 = 30-34, 4 = 35-44, 5 = 45-54, 6 = 55-64, 7 = 65-74, 8 = 75 and older
gender Gender “female”, “male”
race Race/ethnicity 1 = European American/Caucasian/White, 2 = African American/Black, 3 = Hispanic/Latino(a), 4 = Asian/Pacific Islander, 5 = Other
income Income level 1 (lowest) to 5 (highest); 3% missing
education Education level 1 to 4
marital Marital status 1 to 3
group Group 1 to 4
A1-A18 Scale A items 0 to 4; subscales A1-A6, A7-A12, A13-A18; A4 and A11 are reverse-keyed
B1-B17 Scale B items 0 to 4; subscales B1-B5, B6-B12, B13-B17
C1-C10 Checklist C items 0 = no, 1 = yes
y1-y3 Binary outcomes 0 = no, 1 = yes

mydata_scored.csv also has A_s1-A_s3, A_total, B_s1-B_s3, B_total, and C_total. In this file, A4 and A11 are already reverse-scored.

How the Data Were Generated

  • Seven correlated latent variables: three for the subscales of scale A, three for scale B, and one for checklist C.
  • Each Likert item is the latent variable times a loading of 0.7 (-0.7 for the reverse-keyed items) plus noise, cut into five categories (0 to 4).
  • The outcomes come from logistic models with known coefficients on the latent variables: y1 from C (0.5) and the second A subscale (0.4), y2 from C (0.5) and the first B subscale (0.4), and y3 from the first A subscale (0.3) and the third B subscale (0.3).
  • Younger participants skip more items, so the items are missing at random (MAR) depending on age. Income is missing completely at random (MCAR).

Simulation Code

Run this from the project folder to recreate all three files. set.seed() makes the result the same every time.

library(MASS) # mvrnorm()
set.seed(2024)
n <- 500

# Latent variables: three subscales of scale A, three subscales of scale B, and checklist C
latent_cor <- matrix(c(
  1.0, 0.5, 0.5, 0.4, 0.4, 0.4, 0.3,
  0.5, 1.0, 0.5, 0.4, 0.4, 0.4, 0.3,
  0.5, 0.5, 1.0, 0.4, 0.4, 0.4, 0.3,
  0.4, 0.4, 0.4, 1.0, 0.6, 0.6, 0.3,
  0.4, 0.4, 0.4, 0.6, 1.0, 0.6, 0.3,
  0.4, 0.4, 0.4, 0.6, 0.6, 1.0, 0.3,
  0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 1.0
), nrow = 7)
latent <- mvrnorm(n, mu = rep(0, 7), Sigma = latent_cor)
colnames(latent) <- c("fA1", "fA2", "fA3", "fB1", "fB2", "fB3", "fC")

# Turn a latent variable into a 0-4 Likert item
likert_item <- function(f, loading = 0.7) {
  x <- loading * f + rnorm(length(f), sd = sqrt(1 - loading^2))
  cut(x, breaks = c(-Inf, -1.2, -0.4, 0.4, 1.2, Inf), labels = FALSE) - 1
}

# Scale A: 18 items, three subscales of 6 items; A4 and A11 are reverse-keyed
A_factor <- rep(c("fA1", "fA2", "fA3"), each = 6)
A_loading <- ifelse(1:18 %in% c(4, 11), -0.7, 0.7)
A <- sapply(1:18, function(i) likert_item(latent[, A_factor[i]], A_loading[i]))
colnames(A) <- paste0("A", 1:18)

# Scale B: 17 items, three subscales of 5, 7, and 5 items
B_factor <- rep(c("fB1", "fB2", "fB3"), times = c(5, 7, 5))
B <- sapply(1:17, function(i) likert_item(latent[, B_factor[i]]))
colnames(B) <- paste0("B", 1:17)

# Checklist C: 10 yes/no items (0/1)
C <- sapply(1:10, function(i) rbinom(n, 1, plogis(-1 + latent[, "fC"] - 0.1 * i)))
colnames(C) <- paste0("C", 1:10)

# Demographics (codes: see the table below)
age <- round(runif(n, 18, 80))
age_group <- cut(age, breaks = c(17, 24, 29, 34, 44, 54, 64, 74, Inf), labels = FALSE)
gender <- sample(c("female", "male"), n, replace = TRUE, prob = c(0.6, 0.4))
race <- sample(1:5, n, replace = TRUE, prob = c(0.5, 0.2, 0.15, 0.1, 0.05))
income <- sample(1:5, n, replace = TRUE)
education <- sample(1:4, n, replace = TRUE)
marital <- sample(1:3, n, replace = TRUE)
group <- sample(1:4, n, replace = TRUE)

# Binary outcomes from logistic models with known (true) coefficients
y1 <- rbinom(n, 1, plogis(-1.0 + 0.5 * latent[, "fC"] + 0.4 * latent[, "fA2"]))
y2 <- rbinom(n, 1, plogis(-1.5 + 0.5 * latent[, "fC"] + 0.4 * latent[, "fB1"]))
y3 <- rbinom(n, 1, plogis(-1.2 + 0.3 * latent[, "fA1"] + 0.3 * latent[, "fB3"]))

mydata <- data.frame(id = 1:n, age, age_group, gender, race, income, education, marital, group, A, B, C, y1, y2, y3)

# Missing items: younger participants skip more items (missing at random, MAR)
miss_prob <- plogis(-4 + 0.04 * (50 - age))
for (v in c(colnames(A), colnames(B), colnames(C))) {
  mydata[[v]][runif(n) < miss_prob] <- NA
}
# Missing income: 3% at random (missing completely at random, MCAR)
mydata$income[runif(n) < 0.03] <- NA

write.csv(mydata, "data/mydata.csv", row.names = FALSE)

# Scored version: sum scores, prorated when at least 80% of the items are answered
score <- function(items) {
  answered <- rowSums(!is.na(items))
  ifelse(answered >= 0.8 * ncol(items), rowMeans(items, na.rm = TRUE) * ncol(items), NA)
}
mydata_scored <- mydata
mydata_scored$A4 <- 4 - mydata_scored$A4 # reverse-score
mydata_scored$A11 <- 4 - mydata_scored$A11
mydata_scored$A_s1 <- score(mydata_scored[, paste0("A", 1:6)])
mydata_scored$A_s2 <- score(mydata_scored[, paste0("A", 7:12)])
mydata_scored$A_s3 <- score(mydata_scored[, paste0("A", 13:18)])
mydata_scored$A_total <- score(mydata_scored[, paste0("A", 1:18)])
mydata_scored$B_s1 <- score(mydata_scored[, paste0("B", 1:5)])
mydata_scored$B_s2 <- score(mydata_scored[, paste0("B", 6:12)])
mydata_scored$B_s3 <- score(mydata_scored[, paste0("B", 13:17)])
mydata_scored$B_total <- score(mydata_scored[, paste0("B", 1:17)])
mydata_scored$C_total <- score(mydata_scored[, paste0("C", 1:10)])

write.csv(mydata_scored, "data/mydata_scored.csv", row.names = FALSE)

# Imputed version: the first of five imputed datasets, for methods that need complete data
library(mice)
to_impute <- mydata_scored[, c("age", "gender", "race", "income", "education", "marital", "group",
                               colnames(A), colnames(B), colnames(C), "y1", "y2", "y3")]
to_impute$gender <- factor(to_impute$gender)
imp <- mice(to_impute, m = 5, maxit = 20, method = "pmm", seed = 2024, printFlag = FALSE)
mydata_imputed <- mydata_scored
mydata_imputed[names(to_impute)] <- complete(imp, 1)
mydata_imputed$gender <- as.character(mydata_imputed$gender)
mydata_imputed$A_s1 <- score(mydata_imputed[, paste0("A", 1:6)])
mydata_imputed$A_s2 <- score(mydata_imputed[, paste0("A", 7:12)])
mydata_imputed$A_s3 <- score(mydata_imputed[, paste0("A", 13:18)])
mydata_imputed$A_total <- score(mydata_imputed[, paste0("A", 1:18)])
mydata_imputed$B_s1 <- score(mydata_imputed[, paste0("B", 1:5)])
mydata_imputed$B_s2 <- score(mydata_imputed[, paste0("B", 6:12)])
mydata_imputed$B_s3 <- score(mydata_imputed[, paste0("B", 13:17)])
mydata_imputed$B_total <- score(mydata_imputed[, paste0("B", 1:17)])
mydata_imputed$C_total <- score(mydata_imputed[, paste0("C", 1:10)])

write.csv(mydata_imputed, "data/mydata_imputed.csv", row.names = FALSE)