Jointly synthesise multiple linked nested / longitudinal tables
Source:R/synth-linked.R
synth_linked.RdSynthesise several related tables together so that referential integrity and
cross-table statistical relationships are preserved. The table hierarchy is
read from the keys: a table is a child of the table whose full key equals
its own key with the last column dropped (so c("id", "admission_id") is a
child of c("id")). Root tables are synthesised first with the single-table
engine (synth()); each child table is then generated from its synthetic
parent.
Usage
synth_linked(
tables,
structures = NULL,
keys = NULL,
method = "cart",
constraints = NULL,
tuning = synth_control(),
privacy = NULL,
m = 1,
seed = NULL,
...,
schema = NULL
)Arguments
- tables
Named list of input
data.frames (one per table).- structures
Named list of one-sided formulas, one per table, giving each table's nesting hierarchy (e.g.
~ id / admission_id). Optional whenschemais supplied.- keys
Named list of character vectors giving each table's key columns. The last column is the table's own index; the leading columns are the foreign key into its parent. Optional when
schemais supplied.- method
Synthesis method applied per variable unless overridden.
- constraints
Optional cross-table constraints / rules. Constraint enforcement currently applies to single-table
synth()only; a message is emitted if supplied here (Track A) and it is an error underprivacy(DP).- tuning
A
synth_control()object.- privacy
NULLfor Track A, or adp_control()object for Track B.- m
Number of synthetic dataset collections to produce.
- seed
Optional integer seed for reproducibility.
- ...
Reserved for future use.
- schema
Optional
rel_schema()object describing Star, Fan, Snowflake, Associative Bridge, 3NF surrogate PK, or Diamond FK DAG topologies. When supplied, takes precedence overkeys.
Value
A synth_linked_result. Use as.list() (for m == 1) or $syn to
get the named list of synthetic tables.
Details
For every synthetic parent record the number of child rows is drawn from a
learned count distribution (including parents with no children), the
child's foreign key is copied from the parent so it always resolves
(referential integrity), the child's own structural index is regenerated, and
the child's variables are synthesised conditionally on the parent's
synthesised attributes (cross-table predictors), the own index and earlier
child variables. Conditioning is on the immediate parent; a grandparent
reaches a child only through the parent's synthesised values. Use
check_linkage() to verify the result. Track A carries no formal privacy
guarantee.
Passing privacy = dp_control(...) selects differentially private synthesis
(Track B) with a formal (\(\epsilon\), \(\delta\)) guarantee at the
root-entity grain: adding or removing one root individual (its root row
and all descendant rows) changes the release within the budget. Contribution
is bounded hierarchically – max_rows_per_person gives the maximum children
kept per parent for each child table (a single integer for all child tables,
or a named list keyed by table name); the root cap is 1. Each table's variable
marginals and a children-per-parent count histogram are measured under one
exactly-composed budget; synthetic children copy the synthetic parent's
surrogate key so referential integrity still holds by construction. By default
a child's variables are modelled by their own within-table marginals
(referential integrity is preserved, but not cross-table statistical
dependence) and its repeated rows are treated as exchangeable. Two opt-ins in
dp_control() add structure at extra, exactly-composed budget:
cross_table = TRUE conditions each child variable on its synthetic parent's
attributes (so parent \(\to\) child dependence, not just referential
integrity, survives the noise), and longitudinal = TRUE (or a character
vector of child-table names) models a child's repeated rows as a within-unit
DP Markov trajectory – an initial-state model plus per-variable
\(P(v_t \mid v_{t-1})\) transitions – so within-table longitudinal structure
such as visit-to-visit autocorrelation is retained; the two combine. A
longitudinally-modelled child needs a branching cap \(\ge\) 2 in
max_rows_per_person, and its recoverable temporal signal falls as the noise
grows (deeper nesting, larger caps, finer bins, or smaller \(\epsilon\)).
constraints are refused under DP. See vignette("differential-privacy").
Examples
patients <- data.frame(id = 1:20,
sex = sample(c("F", "M"), 20, TRUE),
stringsAsFactors = FALSE)
adm <- do.call(rbind, lapply(patients$id, function(pid) {
n <- 1 + rpois(1, 0.6)
data.frame(id = pid, admission_id = seq_len(n),
los = 1L + rpois(n, 3))
}))
res <- synth_linked(
tables = list(patients = patients, admissions = adm),
structures = list(patients = ~ id, admissions = ~ id / admission_id),
keys = list(patients = "id", admissions = c("id", "admission_id")),
seed = 1
)
check_linkage(res)
#> Linkage OK: keys unique, no orphan child rows.