# Common adverse events with their SOC mappings
ae_terms <- tribble(
~Term, ~SOC,
"Headache", "Nervous system disorders",
"Nausea", "Gastrointestinal disorders",
"Fatigue", "General disorders",
"Dizziness", "Nervous system disorders",
"Diarrhea", "Gastrointestinal disorders",
"Rash", "Skin disorders",
"Back pain", "Musculoskeletal disorders",
"Insomnia", "Psychiatric disorders",
"Cough", "Respiratory disorders",
"Upper respiratory infection", "Infections"
)
# Generate 2-5 AEs per subject (not all subjects have AEs)
subjects_with_ae <- sample(raw_dm$Subject, size = round(n_subjects * 0.8))
raw_ae <- tibble(
Subject = rep(subjects_with_ae, each = 3)
) %>%
slice_sample(n = 60) %>% # ~60 AE records total
mutate(
Site = raw_dm$Site[match(Subject, raw_dm$Subject)],
AE_Num = row_number(),
Term = sample(ae_terms$Term, n(), replace = TRUE),
Severity = sample(c("Mild", "Moderate", "Severe"), n(), replace = TRUE,
prob = c(0.6, 0.3, 0.1)),
Serious = sample(c("No", "Yes"), n(), replace = TRUE, prob = c(0.92, 0.08)),
Related = sample(c("Not Related", "Possibly Related", "Probably Related"), n(),
replace = TRUE, prob = c(0.5, 0.35, 0.15)),
# Start 1-60 days after first dose
StartDay = sample(1:60, n(), replace = TRUE)
) %>%
left_join(
raw_dm %>% select(Subject, FirstDoseDate),
by = "Subject"
) %>%
mutate(
StartDate = as.character(FirstDoseDate + StartDay),
# End date: 1-14 days after start, or ongoing
EndDate = ifelse(
sample(c(TRUE, FALSE), n(), replace = TRUE, prob = c(0.85, 0.15)),
as.character(as.Date(StartDate) + sample(1:14, n(), replace = TRUE)),
NA_character_
)
) %>%
select(Site, Subject, Term, StartDate, EndDate, Severity, Serious, Related)
cat("Raw Adverse Events:", nrow(raw_ae), "records\n\n")