Code
# Load and prepare epilepsy data with download fallback
data_url <- "https://content.sph.harvard.edu/fitzmaur/ala2e/epilepsy.txt"
data_file <- if (file.exists("../../data/epilepsy.txt")) "../../data/epilepsy.txt" else "data/epilepsy.txt"
if (!file.exists(data_file)) {
dir.create("data", showWarnings = FALSE)
download.file(data_url, data_file)
}
epilepsy_raw <- read.table(data_file,
skip = 35, # Skip header text
col.names = c("id", "trt", "age", "base",
"y1", "y2", "y3", "y4"))
# Convert to long format
epilepsy <- epilepsy_raw %>%
pivot_longer(cols = y1:y4,
names_to = "visit",
values_to = "seizures") %>%
mutate(
visit_num = as.numeric(gsub("y", "", visit)),
time = (visit_num - 1) * 2, # weeks since randomization
trt = factor(trt, levels = c(0, 1), labels = c("Placebo", "Progabide")),
log_base = log(base / 4), # log baseline rate per 2 weeks
age_c = age - mean(age) # centered age
)
glimpse(epilepsy)Rows: 236
Columns: 10
$ id <int> 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, …
$ trt <fct> Placebo, Placebo, Placebo, Placebo, Placebo, Placebo, Placeb…
$ age <int> 31, 31, 31, 31, 30, 30, 30, 30, 25, 25, 25, 25, 36, 36, 36, …
$ base <int> 11, 11, 11, 11, 11, 11, 11, 11, 6, 6, 6, 6, 8, 8, 8, 8, 66, …
$ visit <chr> "y1", "y2", "y3", "y4", "y1", "y2", "y3", "y4", "y1", "y2", …
$ seizures <int> 5, 3, 3, 3, 3, 5, 3, 3, 2, 4, 0, 5, 4, 4, 1, 4, 7, 18, 9, 21…
$ visit_num <dbl> 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, …
$ time <dbl> 0, 2, 4, 6, 0, 2, 4, 6, 0, 2, 4, 6, 0, 2, 4, 6, 0, 2, 4, 6, …
$ log_base <dbl> 1.0116009, 1.0116009, 1.0116009, 1.0116009, 1.0116009, 1.011…
$ age_c <dbl> 2.1694915, 2.1694915, 2.1694915, 2.1694915, 1.1694915, 1.169…