Sequential synthesis requires a series of predictive models for imputation. tidysynthesis aims to bring the full predictive modeling toolkit to data synthesis by leveraging the power of tidymodels1.
4.1 Recipes
Feature and target engineering2 is the addition, deletion, and transformation of variables before applying algorithms to the data for predictive modeling. It is an important tool for improving predictive models.
library(recipes) specifies the relationships in a predictive model and feature and target engineering for tidymodels.
tidysynthesis requires recipes for predictive models. These recipes can include just the formula for each model. They can also include target engineering like a Yeo-Johnson transformation and feature engineering like z-score transformations.
Note: Many methods of target engineering are currently not supported. This is because predict() will generate transformed units for the synthetic data set. tidymodels will eventually natively support reverse target enginnering.
For now, we can use construct_recipes() to create a sequence of formulae without feature or target engineering.
Most predictive models predict values based on a conditional mean or conditional median. Conditional mean synthesis generates synthetic data with too little sample variance and not enough observations from the tails of distributions (Little and Rubin 2019).
tidysynthesis has special methods that synthesize values from a predictive distribution. Consider a regression tree. To make a prediction with a regression tree, it is common to plug in predictors to find a unique final node and then predict the mean of the final node. To generate adequate sample variance, sample_rpart() predicts a randomly drawn observation from the corresponding final node.
It is important to align the sample_*() method with the predictive algorithm used for each variable. Current options are sample_lm(), sample_rpart(), and sample_rf().
4.4 Synthesis Specification
The roadmap, algorithms, recipes, and predict methods all go into a synth_spec object. In this example, there is no feature or target engineering, the predictive model is linear regression, and the prediction methods is a random draw from a normal distribution centered at the conditional mean of the regression line with variance equal to the variance of the residuals of the estimated linear regression model.
It is possible to use custom recipes with synth_spec() for one recipe for all variables and construct_recipes() for different recipes for different variables. To use a custom recipe, just create a custom function like the following:
The above step performs a Yeo-Johnson transformation on bill_depth_mm. library(recipes) has a robust system for selecting variables based on their roles in the recipe in addition to explicitly listing variables.
4.6 Bespoke Combinations
The above examples use the same recipe, algorithm, and sampler for every variable in the visit sequence. There are three ways to specify recipes, algorithms, and samplers. Let’s use samplers as an example:
Use the same sampler for every variable.
Use a default sampler and manually override it for specific variables.
Manually specify the sampler for each variable.
4.6.1 Approach 1
To use the same recipe, algorithm, and/or sampler for each variable, simply pass the object into the synth_spec() function.
Using a default sampler and manually overriding it for specific variables requires the construct_samplers() function. This example uses sample_rpart() for bill_length_mm and flipper_length_mm, and then uses sample_rpart_custom() for body_mass_g and bill_depth_mm.
construct_recipes() and construct_algos() uses the same basic syntax.
4.6.3 Approach 3
It is also possible to manually specify the sampler for each variable. In the following example, bill_length_mm, flipper_length_mm, and body_mass_g use sample_rpart() and bill_depth_mm uses sample_rpart_custom().