# Ensure data/ is ignored in git before writing any output files
if (file.exists(".gitignore")) {
gi_lines <- readLines(".gitignore", warn = FALSE)
if (!any(trimws(gi_lines) %in% c("data/", "data"))) {
write("data/", file = ".gitignore", append = TRUE)
}
} else {
writeLines("data/", ".gitignore")
}
# ============================================================================
# PART 1: GENERATE SAMPLE DATASETS
# ============================================================================
set.seed(42)
# Create Texas Government Employee Data (TRS - Teacher Retirement System)
create_trs_data <- function(n = 500) {
first_names <- c("John", "Maria", "Robert", "Jennifer", "Michael", "Sarah",
"David", "Patricia", "James", "Linda", "Daniel", "Barbara")
last_names <- c("Smith", "Johnson", "Williams", "Brown", "Jones", "Garcia",
"Miller", "Davis", "Rodriguez", "Martinez", "Hernandez", "Lopez")
trs <- tibble(
record_id = paste0("TRS_", 1:n),
first_name = sample(first_names, n, replace = TRUE),
last_name = sample(last_names, n, replace = TRUE),
dob = sample(seq(as.Date("1950-01-01"), as.Date("1985-12-31"), by = "day"), n),
years_service = sample(5:40, n, replace = TRUE),
salary_group = sample(c("Elementary", "Secondary", "Administration"), n, replace = TRUE),
retirement_date = NA_Date_
) %>%
mutate(
# Some retirements for older employees
retirement_date = case_when(
years_service > 30 ~ as.Date(paste0(2020 + sample(0:4, n, replace = TRUE),
"-",
sample(1:12, n, replace = TRUE, prob = rep(1/12, 12)),
"-01")),
TRUE ~ NA_Date_
)
)
return(trs)
}
# Create Education Agency Data (TEA - Texas Education Agency)
create_tea_data <- function(trs, n = 500, num_matches = 100, corruption_rate = 0.2) {
first_names <- c("John", "Maria", "Robert", "Jennifer", "Michael", "Sarah",
"David", "Patricia", "James", "Linda", "Daniel", "Barbara",
"Jon", "Mary", "Rob", "Jen", "Mike", "Sara") # Include variations
last_names <- c("Smith", "Smithe", "Smyth", "Johnson", "Jonson", "Williams",
"Brown", "Jones", "Garcia", "Miller", "Davis", "Rodriguez",
"Martinez", "Hernandez", "Lopez", "Sanchez")
tea <- tibble(
record_id = paste0("TEA_", 1:n),
first_name = sample(first_names, n, replace = TRUE),
last_name = sample(last_names, n, replace = TRUE),
dob = sample(seq(as.Date("1950-01-01"), as.Date("1985-12-31"), by = "day"), n),
certification = sample(c("Science", "Math", "English", "Social Studies", "Special Ed"), n, replace = TRUE),
school_district = sample(paste0("District_", 1:50), n, replace = TRUE),
hire_date = sample(seq(as.Date("1990-01-01"), as.Date("2020-12-31"), by = "day"), n)
)
# Make the first num_matches TEA records true matches of TRS_1..TRS_num_matches.
# The remaining TEA records are unrelated people (true non-matches).
tea$first_name[1:num_matches] <- trs$first_name[1:num_matches]
tea$last_name[1:num_matches] <- trs$last_name[1:num_matches]
tea$dob[1:num_matches] <- trs$dob[1:num_matches]
# Introduce data quality issues in the matched records only
# About 20% of matched records have corruption or variation
corruption_indices <- sample(1:num_matches, size = round(num_matches * corruption_rate), replace = FALSE)
for (idx in corruption_indices) {
flip <- sample(1:3, 1)
if (flip == 1) {
# Introduce name variation
tea$first_name[idx] <- abbreviate(tea$first_name[idx], minlength = 3)
} else if (flip == 2) {
# Introduce small typo in last name
last_chars <- nchar(tea$last_name[idx])
if (last_chars > 1) {
typo_pos <- sample(1:last_chars, 1)
wrong_char <- sample(letters[!(letters %in% unlist(strsplit(tea$last_name[idx], "")))], 1)
tea$last_name[idx] <- paste0(
substr(tea$last_name[idx], 1, typo_pos - 1),
wrong_char,
substr(tea$last_name[idx], typo_pos + 1, last_chars)
)
}
} else {
# Shift DOB slightly (data entry error)
tea$dob[idx] <- tea$dob[idx] + sample(c(-1, 1), 1)
}
}
return(tea)
}
# Create a subset of KNOWN matches for validation
create_validation_set <- function(trs, tea, num_matches = 100) {
# Take first 100 records from TRS
trs_subset <- trs %>% slice(1:num_matches)
# These are the "true" matches in TEA
true_matches <- tea %>%
slice(1:num_matches) %>%
rename_with(~paste0("tea_", .), everything()) %>%
rename(tea_record_id = tea_record_id)
# Create ground truth
validation <- tibble(
trs_record_id = trs_subset$record_id,
tea_record_id = true_matches$tea_record_id,
is_match = TRUE
)
return(validation)
}
set.seed(42)
cat("Generating sample datasets...\n")