Generate synthetic data for one table, working natively in long format. The
structure formula declares the nesting hierarchy (e.g.
~ id / visit / test_number) so repeated-measures and nested designs are kept
without pivoting to wide format.
Usage
synth(
data,
structure,
method = "cart",
constraints = NULL,
tuning = synth_control(),
privacy = NULL,
m = 1,
seed = NULL,
...
)Arguments
- data
A
data.framein long format.- structure
A one-sided formula giving the nesting hierarchy, e.g.
~ id / visit. The first term is the unit identifier.- method
Synthesis method,
"cart"(default) or"sample", applied per variable unless overridden intuning.- constraints
Optional
rule()(or list of rules) the synthetic data must satisfy. Enforced by rejection sampling at the unit grain (seerule());constraint_max_triesinsynth_control()bounds the effort.- tuning
A
synth_control()object.- privacy
NULLfor Track A, or adp_control()object for Track B.- m
Number of synthetic datasets to produce.
- seed
Optional integer seed for reproducibility.
- ...
Reserved for future use.
Value
A synth_result object. Use as.data.frame() (for m == 1) or
$syn to get the synthetic data.
Details
The default (Track A) engine is sequential conditional synthesis in the
synthpop lineage: the unit identifier is regenerated by resampling whole
units (preserving realistic per-unit block sizes), and each remaining variable
is synthesised in turn from a model fitted on the real data, conditioning on
the structural columns and everything synthesised before it. With
method = "cart" each draw comes from the matching leaf of a regression /
classification tree, so marginal and conditional distributions are preserved
without distributional assumptions. Track A carries no formal privacy
guarantee.
Columns that are constant within every real unit (baseline covariates such as age or sex) are detected automatically and synthesised once per unit, then broadcast across that unit's rows, so a synthetic subject stays internally consistent. Time-varying columns are synthesised with an initial-state model (first row of each unit) plus a Markov transition model that conditions on the previous row within the unit (lag-1 predictors), so autocorrelation across visits is preserved. The number of rows per unit is drawn from the learned count distribution and the structural-index sequence is regenerated for each synthetic unit.
Any column containing NAs is given a missingness model: a companion
indicator is synthesised in sequence just before the column, the column's own
value model is fitted on the observed rows only, and the synthesised indicator
decides which rows are set back to NA. This preserves the missingness rate
and its association with the other variables, without letting missingness
cascade through predictors. The indicators are not returned.
Supplying constraints (see rule()) keeps only synthetic units whose rows
satisfy every rule, regenerating until enough valid units are collected – so
logical and within-unit temporal constraints are honoured without breaking
the nested structure.
Passing privacy = dp_control(...) selects differentially private synthesis
(Track B) with a formal (\(\epsilon\), \(\delta\)) guarantee. A flat
structure = ~ id gives a marginal release; a nesting index
(~ id / visit) additionally engages a DP Markov model that preserves
within-unit temporal structure (length, initial state, and per-variable
transitions), for which max_rows_per_person must be set to the public
maximum rows per person. See vignette("differential-privacy").
Examples
df <- data.frame(
id = rep(1:20, each = 2),
visit = rep(1:2, times = 20),
age = rep(round(rnorm(20, 60, 8)), each = 2),
sbp = round(rnorm(40, 130, 15))
)
res <- synth(df, structure = ~ id / visit, seed = 1)
head(as.data.frame(res))
#> id visit age sbp
#> 1 1 1 72 137.8809
#> 2 1 2 72 121.2988
#> 3 2 1 77 144.0971
#> 4 2 2 77 130.1669
#> 5 3 1 58 130.6243
#> 6 3 2 58 146.6918