Skip to contents

Add a custom per-variable synthesiser to the method registry so it can be selected by name via the method argument of synth() / synth_linked() or the per-variable method in synth_control(). Registering a name that already exists overwrites it, so you can override a built-in.

Usage

register_method(
  name,
  fit,
  draw,
  numeric = TRUE,
  categorical = TRUE,
  needs_predictors = TRUE
)

Arguments

name

Method name (a single string) used to select it.

fit

A function function(y, x, control) returning a fitted model.

draw

A function function(model, x, n, control) returning n values.

numeric, categorical

Logical flags declaring which target types the method supports (used for validation). Both default to TRUE.

needs_predictors

Logical; if TRUE (default) the engine falls back to marginal sampling when the variable has no predictors.

Value

Invisibly, the method name.

Details

A method is a pair of functions:

fit(y, x, control)

fits a model of the target y on the predictor data.frame x (which may have zero columns) and returns any object;

draw(model, x, n, control)

returns a length-n vector of synthetic values for new predictor rows x.

control is the synth_control() object, so a method can honour proper, read its own hyperparameters, and so on.

Examples

# A trivial "mean" method for numeric variables.
register_method(
  "constant_mean",
  fit  = function(y, x, control) mean(y),
  draw = function(model, x, n, control) rep(model, n),
  categorical = FALSE
)
"constant_mean" %in% list_methods()
#> [1] TRUE