Skip to contents

Beyond composite prefix trees: rel_schema()

Real clinical and operational databases frequently use 3NF surrogate primary keys, Star schemas (a central fact table referencing multiple entity and fixed vocabulary lookup dimensions), Fan schemas (one parent hub fanning out to correlated sibling child tables or sum-conserving line items), and Diamond foreign-key DAGs. rel_schema() declares these structures explicitly for synth_linked().

dim_patient <- data.frame(
  patient_id = 1:80,
  peds_flag  = rep(c(TRUE, FALSE), each = 40),
  age        = c(sample(2:15, 40, TRUE), sample(45:80, 40, TRUE))
)

dim_icd <- data.frame(
  icd_code = c("Q21.0", "I21.0"),
  chapter  = c("peds", "cardiac"),
  stringsAsFactors = FALSE
)

fact_adm <- data.frame(
  admission_id = 1:80,
  patient_id   = 1:80,
  icd_code     = ifelse(dim_patient$peds_flag, "Q21.0", "I21.0"),
  admit_date   = as.Date("2024-01-01") + sample(0:60, 80, TRUE),
  total_cost   = round(ifelse(dim_patient$peds_flag,
                              rnorm(80, 500, 50), rnorm(80, 3000, 300)), 2),
  stringsAsFactors = FALSE
)

# Fan-out sibling 1: procedures (with Diamond shortcut FK patient_id)
procedures <- data.frame(
  proc_id      = 1:80,
  admission_id = 1:80,
  patient_id   = 1:80,
  kind         = ifelse(dim_patient$peds_flag, "echo", "PCI"),
  stringsAsFactors = FALSE
)

# Fan-out sibling 2: claim_lines conserving fact_adm$total_cost
claim_lines <- do.call(rbind, lapply(seq_len(nrow(fact_adm)), function(i) {
  w <- runif(2)
  cost_parts <- round(fact_adm$total_cost[i] * w / sum(w), 2)
  cost_parts[2] <- round(fact_adm$total_cost[i] - cost_parts[1], 2)
  data.frame(admission_id = fact_adm$admission_id[i], line_cost = cost_parts)
}))
claim_lines$line_id <- seq_len(nrow(claim_lines))

tabs <- list(
  dim_patient = dim_patient,
  dim_icd     = dim_icd,
  fact_adm    = fact_adm,
  procedures  = procedures,
  claim_lines = claim_lines
)

Declaring Star + Fan + Diamond + Sum Conservation

sch <- rel_schema(
  tables = tabs,
  roles = list(
    dim_patient = "entity",
    dim_icd     = "lookup",
    fact_adm    = "fact",
    procedures  = "fact",
    claim_lines = "fact"
  ),
  pk = list(
    dim_patient = "patient_id",
    dim_icd     = "icd_code",
    fact_adm    = "admission_id",
    procedures  = "proc_id",
    claim_lines = "line_id"
  ),
  fks = list(
    rel_fk("fact_adm",    "patient_id",   "dim_patient", "patient_id", driver = TRUE),
    rel_fk("fact_adm",    "icd_code",     "dim_icd",     "icd_code"),
    rel_fk("procedures",  "admission_id", "fact_adm",    "admission_id", driver = TRUE),
    rel_fk("procedures",  "patient_id",   "dim_patient", "patient_id"),
    rel_fk("claim_lines", "admission_id", "fact_adm",    "admission_id", driver = TRUE)
  ),
  sibling_bridge = "cluster",
  sum_rules = list(
    rule_sum("fact_adm", "total_cost", "claim_lines", "line_cost")
  )
)
sch
#> <rel_schema> 5 table(s); sibling_bridge: cluster 
#>   - dim_patient    pk=(patient_id)  [entity]
#>   - dim_icd        pk=(icd_code)  [lookup]
#>   - fact_adm       pk=(admission_id)  [fact] -> dim_patient
#>   - procedures     pk=(proc_id)  [fact] -> fact_adm
#>   - claim_lines    pk=(line_id)  [fact] -> fact_adm

Joint synthesis and multi-table pooled inference

res <- synth_linked(tabs, schema = sch, m = 3, seed = 1)
syn1 <- res$syn[[1]]

# Lookup table is untouched; check_linkage verifies all multi-parent FKs
identical(syn1$dim_icd, dim_icd)
#> [1] TRUE
check_linkage(res$syn[[1]], keys = sch)
#> Linkage OK: keys unique, no orphan child rows.

# Diamond coherence holds by construction
identical(
  syn1$procedures$patient_id,
  syn1$fact_adm$patient_id[match(syn1$procedures$admission_id, syn1$fact_adm$admission_id)]
)
#> [1] TRUE

# Pool a joined model across all m synthetic databases
pool_synth(res, function(tl) {
  m_df <- merge(tl$dim_patient, tl$fact_adm, by = "patient_id")
  lm(total_cost ~ peds_flag, data = m_df)
})
#> <flexsynth_pool> pooled inference from 3 synthetic dataset(s)
#>   rule: synthpop   population inference   (n = 80, k = 80)
#>           term  estimate std.error statistic   p.value  conf.low conf.high
#>    (Intercept)  3078.285  91.26116    33.731 2.06e-249  2899.417  3257.154
#>  peds_flagTRUE -2371.846 131.05375   -18.098  3.29e-73 -2628.707 -2114.986