Skip to contents

A linked cardiac schema

Real clinical data rarely lives in one table. Here we build a small, fully synthetic cardiac example (never real patient data): patients, their cardiac admissions, and the labs taken during each admission.

patients (id)
  └─ admissions (id / admission_id)
       └─ labs    (id / admission_id / lab_number)
n_pat <- 50
patients <- data.frame(
  id     = seq_len(n_pat),
  sex    = sample(c("F", "M"), n_pat, replace = TRUE, prob = c(0.45, 0.55)),
  age    = round(rnorm(n_pat, 64, 11)),
  smoker = sample(c(FALSE, TRUE), n_pat, replace = TRUE, prob = c(0.7, 0.3))
)

adm <- do.call(rbind, lapply(patients$id, function(pid) {
  k <- 1 + rpois(1, 0.6)
  t0 <- as.Date("2024-01-01") + sample(0:120, k, replace = TRUE)
  los <- 1 + rpois(k, 3)
  data.frame(id = pid, admission_id = seq_len(k),
             admit_date = t0, los_days = los,
             discharge_date = t0 + los)
}))

labs <- do.call(rbind, lapply(seq_len(nrow(adm)), function(i) {
  k <- 1 + rpois(1, 2)
  span <- as.numeric(adm$discharge_date[i] - adm$admit_date[i])
  off <- sample(0:max(0, span), k, replace = TRUE)
  data.frame(id = adm$id[i], admission_id = adm$admission_id[i],
             lab_number = seq_len(k),
             lab_date = adm$admit_date[i] + off,
             analyte = sample(c("troponin", "LDL", "creatinine"), k, replace = TRUE),
             value   = round(abs(rnorm(k, 2, 1.5)), 2))
}))

tables <- list(patients = patients, admissions = adm, labs = labs)
vapply(tables, nrow, integer(1))
#>   patients admissions       labs 
#>         50         82        236

Joint synthesis with synth_linked()

synth_linked() takes a structures formula per table and the keys, and reads the hierarchy from them: each table’s key is its parent’s key plus one more column. Root tables are synthesised with the single-table engine; child tables are generated from their synthetic parent, with foreign keys copied over so referential integrity holds by construction.

structures <- list(
  patients   = ~ id,
  admissions = ~ id / admission_id,
  labs       = ~ id / admission_id / lab_number
)
keys <- list(
  patients   = "id",
  admissions = c("id", "admission_id"),
  labs       = c("id", "admission_id", "lab_number")
)

res <- synth_linked(tables, structures = structures, keys = keys, seed = 1)
res
#> <synth_linked_result>
#>   track        : A (high-utility; NOT differentially private)
#>   datasets (m) : 1 
#>   tables       : 3 
#>     - patients           50 rows  (input 50)  (root)
#>     - admissions         86 rows  (input 82)  child of patients
#>     - labs              256 rows  (input 236)  child of admissions
#>   method       : cart 
#> 
#> Get the tables with as.list(x)  
#> Verify linkage with check_linkage(x)

The number of children per parent is drawn from a learned count model (including parents with no children), and child variables are conditioned on the parent’s synthesised attributes.

Verifying linkage

check_linkage() confirms key uniqueness and the absence of orphan child rows.

syn <- as.list(res)
check_linkage(res)
#> Linkage OK: keys unique, no orphan child rows.

# every synthetic lab points at a real (synthetic) admission — no orphans
adm_idx <- match(paste(syn$labs$id, syn$labs$admission_id),
                 paste(syn$admissions$id, syn$admissions$admission_id))
all(!is.na(adm_idx))
#> [1] TRUE

# native Date columns keep chronological & parent-interval integrity automatically
all(syn$admissions$admit_date <= syn$admissions$discharge_date)
#> [1] TRUE
all(syn$labs$lab_date >= syn$admissions$admit_date[adm_idx] &
    syn$labs$lab_date <= syn$admissions$discharge_date[adm_idx])
#> [1] TRUE

Per-table diagnostics

diagnose() and disclosure_risk() accept the whole result and report one block per table.

diagnose(tables, res)
#> <flexsynth_diagnostics_list> 3 tables
#> 
#> == patients ==
#> <flexsynth_diagnostics>
#>   rows        : real 50  synthetic 50 
#>   variables   : 4 
#> 
#> Univariate fit (smaller = closer):
#>  variable        type metric distance
#>        id     numeric     ks     0.00
#>       sex categorical    tvd     0.08
#>       age     numeric     ks     0.18
#>    smoker     logical    tvd     0.04
#>   mean distance: 0.0750   worst: age (0.1800)
#> 
#> Correlation structure (2 numeric vars):
#>   Frobenius diff: 0.6370   mean |diff|: 0.4504   max |diff|: 0.4504
#> 
#> Categorical association (Cramer's V, 2 vars):
#>   mean |diff|: 0.1625   max |diff|: 0.1625
#> 
#> Propensity utility (pMSE, logistic; descriptive, in-sample):
#>   pMSE: 0.00264   expected: 0.00500   ratio: 0.53 (1 = indistinguishable)
#> 
#> == admissions ==
#> <flexsynth_diagnostics>
#>   rows        : real 82  synthetic 86 
#>   variables   : 5 
#> 
#> Univariate fit (smaller = closer):
#>        variable     type metric distance
#>              id  numeric     ks   0.0729
#>    admission_id  numeric     ks   0.0332
#>      admit_date temporal     ks   0.0522
#>        los_days  numeric     ks   0.0871
#>  discharge_date temporal     ks   0.0644
#>   mean distance: 0.0619   worst: los_days (0.0871)
#> 
#> Correlation structure (5 numeric vars):
#>   Frobenius diff: 0.3451   mean |diff|: 0.0702   max |diff|: 0.1236
#> 
#> Propensity utility (pMSE, logistic; descriptive, in-sample):
#>   pMSE: 0.00461   expected: 0.00363   ratio: 1.27 (1 = indistinguishable)
#> 
#> == labs ==
#> <flexsynth_diagnostics>
#>   rows        : real 236  synthetic 256 
#>   variables   : 6 
#> 
#> Univariate fit (smaller = closer):
#>      variable        type metric distance
#>            id     numeric     ks   0.0867
#>  admission_id     numeric     ks   0.0452
#>    lab_number     numeric     ks   0.0448
#>      lab_date    temporal     ks   0.0597
#>       analyte categorical    tvd   0.0695
#>         value     numeric     ks   0.0569
#>   mean distance: 0.0605   worst: id (0.0867)
#> 
#> Correlation structure (5 numeric vars):
#>   Frobenius diff: 0.6002   mean |diff|: 0.1045   max |diff|: 0.2796
#> 
#> Propensity utility (pMSE, logistic; descriptive, in-sample):
#>   pMSE: 0.00288   expected: 0.00170   ratio: 1.69 (1 = indistinguishable)

disclosure_risk() is most meaningful per table with the genuinely identifying columns named (excluding surrogate keys such as id, which are regenerated):

disclosure_risk(patients, syn$patients,
                quasi = c("age", "sex", "smoker"), seed = 1)
#> <flexsynth_disclosure>
#>   rows            : real 50  synthetic 50 
#>   quasi-identifiers: age, sex, smoker 
#> 
#> Replicated uniques (identity risk):
#>   real sample-uniques : 33
#>   reproduced in syn   : 0  (0.00% of uniques, 0.00% of real rows)
#>   syn rows copying a real row: 0  (0.00% of syn)
#> 
#> Distance to closest record (Gower, 0 = exact copy):
#>   syn->real  : median 0.0065   5th pct 0.0008   exact copies 0.00%
#>   real->real : median 0.0081   (baseline)
#>   median syn distance is smaller than the real-neighbour baseline
#>   descriptive only: inspect lower-tail distances and exact copies; this is not a safety guarantee
#> 
#> Membership inference: not run (supply `holdout` of non-training records).
#> 
#> Attribute disclosure: not run (supply `target` = a sensitive column).

Passing the whole objects (disclosure_risk(tables, res)) also works and defaults to all shared columns per table — quick, but it will include surrogate keys, so prefer naming quasi for a real release. Constraints (rule()) currently apply to single-table synth(); enforce cross-table logic on the parent table before linking.

The example data here is synthetic. Track A output is high-utility but carries no formal privacy guarantee — always read the disclosure diagnostics before sharing.