Generates a synthetic version of a data.frame, with similar characteristics to the original. See Details for the algorithm used.

synthetic(
  data,
  model_expression = ranger(x = x, y = y),
  predict_expression = predict(model, data = xsynth)$predictions,
  missingness_expression = NULL,
  verbose = TRUE
)

Arguments

data

A data.frame of which to make a synthetic version.

model_expression

An R-expression to estimate a model. Defaults to ranger(x = x, y = y), which uses the fast implementation of random forests in ranger. The expression is evaluated in an environment containing objects x and y, where x is a data.frame with the predictor variables, and y is a vector of outcome values (see Details).

predict_expression

An R-expression to generate predicted values based on the model estimated by model_expression. Defaults to predict(model, data = xsynth)$predictions. This expression must return a vector of predicted values. The expression is evaluated in an environment containing objects model and xsynth, where model is the model estimated by model_expression, and xsynth is the data.frame of synthetic data used to predict the next column (see Details).

missingness_expression

Optional. An R-expression to impute missing values. Defaults to NULL, which means listwise deletion is used. The expression is evaluated in an environment containing the object data, as specified in the call to synthetic. It must return a data.frame with the same dimensions and column names as the original data. For example, use missingness_expression = missRanger::missRanger(data = data) for a fast implementation of the excellent 'missForest' single imputation technique.

verbose

Logical, Default: TRUE. Whether to show a progress bar while running the algorithm and provide informative messages.

Value

A data.frame with synthetic data, based on data.

Details

Based on the work by Nowok, Raab, and Dibben (2016), this function uses a simple algorithm to generate a synthetic dataset with similar characteristics to the original. The algorithm is as follows:

  1. Let x be the original data.frame, with columns 1:j

  2. Let xsynth be a synthetic data.frame, with columns 1:j

  3. Column 1 of xsynth is a bootstrapped version of column 1 of x

  4. Using model_expression, a predictive model is built for column c, for c along 2:j, with c predicted from columns 1:(c-1) of the original data.

  5. Using predict_expression, columns 1:(c-1) of the synthetic data are used to predict synthetic values for column c.

Variables are thus imputed in order of occurrence in the data.frame. To impute in a different order, reorder the data.

Note that, for data synthesis to work properly, it is essential that the class of variables is defined correctly. The default algorithm ranger supports numeric, integer, and factor types. Other types of variables should be converted to one of these types, or users can use a custom model_expression and predict_expressio when calling synthetic.

Note that for data synthesis to work properly, it is essential that the class of variables is defined correctly. The default algorithm ranger supports numeric, integer, factor, and logical data. Other types of variables should be converted to one of these types.

Users can provide use a custom model_expression and predict_expression to use a different algorithm when calling synthetic.

As demonstrated in the example, users could call lm as a model_expression to use linear regression, which preserves linear marginal relationships but can give rise to values out of range of the original data. Or users could call sample as a predict_expression to bootstrap each variable, a very quick solution that maintains univariate distributions but loses all marginal relationships. These examples are not exhaustive, and users can even create custom functions.

References

Nowok, B., Raab, G.M and Dibben, C. (2016). synthpop: Bespoke creation of synthetic data in R. Journal of Statistical Software, 74(11), 1-26. <doi:10.18637/jss.v074.i11>.

Examples

# Example using the iris dataset and default ranger algorithm iris_syn <- synthetic(iris)
#> | | | 0% | |============== | 20% | |============================ | 40% | |========================================== | 60% | |======================================================== | 80% | |======================================================================| 100%
# Example using lm as prediction algorithm (only works for numeric variables) # note that, within the model_expression, a new data.frame is created because # lm() requires a separate data argument: dat <- iris[, 1:4] synthetic(dat, model_expression = lm(.outcome ~ ., data = data.frame(.outcome = y, xsynth)), predict_expression = predict(model, newdata = xsynth))
#> | | | 0% | |================== | 25% | |=================================== | 50%
#> Warning: prediction from a rank-deficient fit may be misleading
#> | |==================================================== | 75%
#> Warning: prediction from a rank-deficient fit may be misleading
#> | |======================================================================| 100%
#> Sepal.Length Sepal.Width Petal.Length Petal.Width #> 1 6.3 3.071978 3.579072 1.1078482 #> 2 6.3 3.071978 3.579072 1.1078482 #> 3 7.0 3.091870 3.336026 0.9835799 #> 4 6.8 3.086186 3.405468 1.0190852 #> 5 6.9 3.089028 3.370747 1.0013325 #> 6 5.6 3.052086 3.822118 1.2321165 #> 7 5.1 3.037877 3.995722 1.3208795 #> 8 5.8 3.057769 3.752676 1.1966113 #> 9 6.1 3.066294 3.648514 1.1433534 #> 10 6.3 3.071978 3.579072 1.1078482 #> 11 5.1 3.037877 3.995722 1.3208795 #> 12 5.5 3.049244 3.856839 1.2498691 #> 13 6.9 3.089028 3.370747 1.0013325 #> 14 6.1 3.066294 3.648514 1.1433534 #> 15 5.0 3.035035 4.030443 1.3386322 #> 16 6.8 3.086186 3.405468 1.0190852 #> 17 7.0 3.091870 3.336026 0.9835799 #> 18 5.7 3.054927 3.787397 1.2143639 #> 19 5.2 3.040719 3.961001 1.3031269 #> 20 4.8 3.029352 4.099884 1.3741374 #> 21 6.7 3.083345 3.440189 1.0368378 #> 22 7.0 3.091870 3.336026 0.9835799 #> 23 4.8 3.029352 4.099884 1.3741374 #> 24 7.4 3.103237 3.197143 0.9125695 #> 25 6.7 3.083345 3.440189 1.0368378 #> 26 6.0 3.063453 3.683234 1.1611060 #> 27 5.2 3.040719 3.961001 1.3031269 #> 28 4.6 3.023668 4.169326 1.4096426 #> 29 4.9 3.032193 4.065164 1.3563848 #> 30 6.7 3.083345 3.440189 1.0368378 #> 31 5.5 3.049244 3.856839 1.2498691 #> 32 6.2 3.069136 3.613793 1.1256008 #> 33 7.2 3.097553 3.266585 0.9480747 #> 34 6.9 3.089028 3.370747 1.0013325 #> 35 4.6 3.023668 4.169326 1.4096426 #> 36 6.4 3.074819 3.544351 1.0900956 #> 37 6.5 3.077661 3.509630 1.0723430 #> 38 4.6 3.023668 4.169326 1.4096426 #> 39 5.8 3.057769 3.752676 1.1966113 #> 40 6.2 3.069136 3.613793 1.1256008 #> 41 5.9 3.060611 3.717955 1.1788587 #> 42 5.4 3.046402 3.891559 1.2676217 #> 43 5.8 3.057769 3.752676 1.1966113 #> 44 6.9 3.089028 3.370747 1.0013325 #> 45 5.2 3.040719 3.961001 1.3031269 #> 46 6.0 3.063453 3.683234 1.1611060 #> 47 6.2 3.069136 3.613793 1.1256008 #> 48 5.6 3.052086 3.822118 1.2321165 #> 49 6.2 3.069136 3.613793 1.1256008 #> 50 4.6 3.023668 4.169326 1.4096426 #> 51 6.1 3.066294 3.648514 1.1433534 #> 52 5.4 3.046402 3.891559 1.2676217 #> 53 6.4 3.074819 3.544351 1.0900956 #> 54 4.8 3.029352 4.099884 1.3741374 #> 55 5.0 3.035035 4.030443 1.3386322 #> 56 6.0 3.063453 3.683234 1.1611060 #> 57 5.4 3.046402 3.891559 1.2676217 #> 58 6.8 3.086186 3.405468 1.0190852 #> 59 6.4 3.074819 3.544351 1.0900956 #> 60 6.2 3.069136 3.613793 1.1256008 #> 61 6.0 3.063453 3.683234 1.1611060 #> 62 5.0 3.035035 4.030443 1.3386322 #> 63 7.7 3.111762 3.092980 0.8593117 #> 64 6.0 3.063453 3.683234 1.1611060 #> 65 6.9 3.089028 3.370747 1.0013325 #> 66 5.2 3.040719 3.961001 1.3031269 #> 67 5.2 3.040719 3.961001 1.3031269 #> 68 5.7 3.054927 3.787397 1.2143639 #> 69 6.5 3.077661 3.509630 1.0723430 #> 70 6.4 3.074819 3.544351 1.0900956 #> 71 5.5 3.049244 3.856839 1.2498691 #> 72 4.6 3.023668 4.169326 1.4096426 #> 73 7.2 3.097553 3.266585 0.9480747 #> 74 6.0 3.063453 3.683234 1.1611060 #> 75 5.0 3.035035 4.030443 1.3386322 #> 76 6.1 3.066294 3.648514 1.1433534 #> 77 6.2 3.069136 3.613793 1.1256008 #> 78 5.0 3.035035 4.030443 1.3386322 #> 79 5.5 3.049244 3.856839 1.2498691 #> 80 5.0 3.035035 4.030443 1.3386322 #> 81 6.2 3.069136 3.613793 1.1256008 #> 82 4.9 3.032193 4.065164 1.3563848 #> 83 6.1 3.066294 3.648514 1.1433534 #> 84 5.2 3.040719 3.961001 1.3031269 #> 85 5.0 3.035035 4.030443 1.3386322 #> 86 5.5 3.049244 3.856839 1.2498691 #> 87 5.9 3.060611 3.717955 1.1788587 #> 88 6.4 3.074819 3.544351 1.0900956 #> 89 5.0 3.035035 4.030443 1.3386322 #> 90 6.7 3.083345 3.440189 1.0368378 #> 91 5.2 3.040719 3.961001 1.3031269 #> 92 6.4 3.074819 3.544351 1.0900956 #> 93 7.7 3.111762 3.092980 0.8593117 #> 94 4.5 3.020826 4.204047 1.4273952 #> 95 6.9 3.089028 3.370747 1.0013325 #> 96 5.4 3.046402 3.891559 1.2676217 #> 97 5.1 3.037877 3.995722 1.3208795 #> 98 4.9 3.032193 4.065164 1.3563848 #> 99 5.2 3.040719 3.961001 1.3031269 #> 100 5.4 3.046402 3.891559 1.2676217 #> 101 5.6 3.052086 3.822118 1.2321165 #> 102 6.3 3.071978 3.579072 1.1078482 #> 103 4.9 3.032193 4.065164 1.3563848 #> 104 5.7 3.054927 3.787397 1.2143639 #> 105 5.6 3.052086 3.822118 1.2321165 #> 106 5.0 3.035035 4.030443 1.3386322 #> 107 4.8 3.029352 4.099884 1.3741374 #> 108 6.7 3.083345 3.440189 1.0368378 #> 109 6.3 3.071978 3.579072 1.1078482 #> 110 6.1 3.066294 3.648514 1.1433534 #> 111 5.8 3.057769 3.752676 1.1966113 #> 112 5.0 3.035035 4.030443 1.3386322 #> 113 6.4 3.074819 3.544351 1.0900956 #> 114 6.5 3.077661 3.509630 1.0723430 #> 115 5.5 3.049244 3.856839 1.2498691 #> 116 4.4 3.017985 4.238768 1.4451478 #> 117 4.3 3.015143 4.273489 1.4629004 #> 118 5.5 3.049244 3.856839 1.2498691 #> 119 5.4 3.046402 3.891559 1.2676217 #> 120 5.4 3.046402 3.891559 1.2676217 #> 121 6.2 3.069136 3.613793 1.1256008 #> 122 5.0 3.035035 4.030443 1.3386322 #> 123 4.9 3.032193 4.065164 1.3563848 #> 124 5.0 3.035035 4.030443 1.3386322 #> 125 5.1 3.037877 3.995722 1.3208795 #> 126 5.0 3.035035 4.030443 1.3386322 #> 127 4.6 3.023668 4.169326 1.4096426 #> 128 4.6 3.023668 4.169326 1.4096426 #> 129 5.6 3.052086 3.822118 1.2321165 #> 130 6.3 3.071978 3.579072 1.1078482 #> 131 6.6 3.080503 3.474910 1.0545904 #> 132 7.2 3.097553 3.266585 0.9480747 #> 133 4.9 3.032193 4.065164 1.3563848 #> 134 6.7 3.083345 3.440189 1.0368378 #> 135 6.7 3.083345 3.440189 1.0368378 #> 136 4.3 3.015143 4.273489 1.4629004 #> 137 6.7 3.083345 3.440189 1.0368378 #> 138 4.9 3.032193 4.065164 1.3563848 #> 139 6.4 3.074819 3.544351 1.0900956 #> 140 5.0 3.035035 4.030443 1.3386322 #> 141 6.0 3.063453 3.683234 1.1611060 #> 142 6.4 3.074819 3.544351 1.0900956 #> 143 5.9 3.060611 3.717955 1.1788587 #> 144 6.5 3.077661 3.509630 1.0723430 #> 145 5.7 3.054927 3.787397 1.2143639 #> 146 4.6 3.023668 4.169326 1.4096426 #> 147 6.6 3.080503 3.474910 1.0545904 #> 148 5.6 3.052086 3.822118 1.2321165 #> 149 5.7 3.054927 3.787397 1.2143639 #> 150 5.0 3.035035 4.030443 1.3386322
# Example using bootstrapping: synthetic(iris, model_expression = NULL, predict_expression = sample(y, size = length(y), replace = TRUE))
#> | | | 0% | |============== | 20% | |============================ | 40% | |========================================== | 60% | |======================================================== | 80% | |======================================================================| 100%
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 5.2 3.2 4.7 2.5 setosa #> 2 6.2 3.0 1.4 2.0 virginica #> 3 5.0 2.8 4.7 0.2 versicolor #> 4 4.8 2.9 4.1 1.6 virginica #> 5 5.6 2.7 5.1 2.0 versicolor #> 6 6.3 3.0 4.1 0.2 setosa #> 7 6.3 3.7 1.4 0.4 setosa #> 8 4.6 3.8 1.4 1.1 virginica #> 9 5.4 3.1 4.9 0.2 virginica #> 10 5.4 2.7 4.5 1.3 setosa #> 11 5.5 3.3 4.0 1.3 virginica #> 12 5.6 3.0 4.8 0.4 versicolor #> 13 5.7 3.0 1.5 0.1 virginica #> 14 5.7 3.4 5.6 1.2 setosa #> 15 5.5 3.0 5.0 1.3 setosa #> 16 5.6 3.7 4.0 1.5 virginica #> 17 5.1 3.5 1.4 0.2 virginica #> 18 5.7 3.8 4.7 0.2 versicolor #> 19 5.7 3.0 1.7 1.8 versicolor #> 20 5.7 3.2 5.1 0.4 setosa #> 21 5.9 3.3 5.1 2.4 versicolor #> 22 6.4 2.9 1.5 1.8 setosa #> 23 5.4 4.4 1.4 0.3 setosa #> 24 5.0 3.1 1.4 1.2 virginica #> 25 4.7 3.0 3.6 1.4 virginica #> 26 6.2 2.7 4.9 1.4 setosa #> 27 4.8 2.4 1.5 2.0 setosa #> 28 5.0 3.2 4.4 1.5 virginica #> 29 5.4 2.5 4.5 1.8 setosa #> 30 4.8 3.4 5.5 1.3 setosa #> 31 6.3 3.0 5.5 0.2 setosa #> 32 6.4 3.6 6.1 1.8 virginica #> 33 6.3 2.7 4.4 0.5 virginica #> 34 6.7 2.5 3.9 0.6 setosa #> 35 5.1 2.9 1.6 1.4 virginica #> 36 5.4 3.0 5.1 0.3 setosa #> 37 6.3 2.5 5.1 2.0 versicolor #> 38 5.5 3.4 1.6 1.3 virginica #> 39 5.1 2.5 1.3 1.5 setosa #> 40 4.5 3.2 6.4 0.2 versicolor #> 41 4.9 3.1 1.4 2.1 versicolor #> 42 5.7 3.8 1.3 2.1 versicolor #> 43 5.5 3.0 1.5 1.0 virginica #> 44 5.4 4.0 6.0 1.3 versicolor #> 45 6.7 3.1 1.6 1.5 setosa #> 46 5.0 3.0 1.6 1.7 versicolor #> 47 5.1 3.5 1.5 2.1 setosa #> 48 5.5 3.0 4.1 1.8 setosa #> 49 5.6 3.8 4.0 0.4 setosa #> 50 6.0 3.8 3.7 2.0 virginica #> 51 5.0 2.9 5.1 0.2 virginica #> 52 6.7 3.2 3.3 0.4 versicolor #> 53 6.5 2.8 4.4 2.3 versicolor #> 54 6.4 2.8 4.4 1.8 virginica #> 55 5.0 2.4 5.5 2.1 virginica #> 56 4.8 3.3 4.5 2.1 versicolor #> 57 6.5 3.7 1.4 1.3 virginica #> 58 4.9 3.0 6.7 2.0 virginica #> 59 6.7 2.5 5.1 1.3 setosa #> 60 5.0 3.6 6.1 2.1 versicolor #> 61 4.6 2.6 4.5 0.4 virginica #> 62 4.9 3.4 6.6 2.1 virginica #> 63 5.7 3.0 4.8 2.3 setosa #> 64 5.0 2.9 5.3 0.2 virginica #> 65 5.5 2.6 5.5 1.3 setosa #> 66 5.1 2.7 6.6 2.0 virginica #> 67 7.7 2.8 1.5 1.5 setosa #> 68 6.1 2.5 4.4 2.5 setosa #> 69 4.8 2.8 4.5 0.1 virginica #> 70 5.1 2.3 4.9 1.0 versicolor #> 71 4.6 3.1 1.2 2.1 versicolor #> 72 6.4 2.5 1.5 1.2 setosa #> 73 5.0 2.8 3.9 0.1 versicolor #> 74 7.7 2.8 4.8 1.4 versicolor #> 75 5.2 3.0 1.6 2.3 virginica #> 76 6.9 2.9 1.4 0.2 versicolor #> 77 5.0 2.7 5.1 0.3 setosa #> 78 5.5 3.0 4.4 0.2 setosa #> 79 6.3 3.0 4.4 0.2 setosa #> 80 5.9 2.8 6.7 2.3 versicolor #> 81 6.0 3.3 5.7 0.4 setosa #> 82 6.1 3.0 1.4 0.2 setosa #> 83 7.2 2.2 1.0 0.2 versicolor #> 84 6.5 2.6 5.4 2.3 virginica #> 85 5.1 2.9 1.2 1.0 setosa #> 86 5.4 3.2 1.5 1.3 setosa #> 87 6.3 3.0 4.2 1.5 virginica #> 88 5.0 2.7 1.5 1.6 virginica #> 89 6.4 3.6 5.7 0.2 versicolor #> 90 7.2 2.4 4.0 1.5 versicolor #> 91 5.1 3.5 4.0 1.9 versicolor #> 92 7.9 2.7 6.3 1.2 setosa #> 93 5.7 3.0 4.0 0.2 virginica #> 94 5.2 3.4 1.4 0.2 versicolor #> 95 6.2 3.1 5.1 2.1 setosa #> 96 6.0 3.7 4.1 2.5 virginica #> 97 6.7 3.6 4.9 1.3 setosa #> 98 5.7 3.0 4.6 1.4 versicolor #> 99 6.3 2.5 1.5 1.6 versicolor #> 100 4.6 2.7 6.9 2.3 virginica #> 101 6.7 2.7 1.1 1.1 setosa #> 102 5.5 2.3 4.5 2.3 virginica #> 103 6.7 3.4 4.0 1.5 virginica #> 104 5.0 3.7 4.8 1.3 versicolor #> 105 6.1 2.7 1.5 2.3 virginica #> 106 5.2 2.0 4.0 0.2 setosa #> 107 5.1 2.9 4.9 0.2 virginica #> 108 6.4 2.2 6.1 2.3 virginica #> 109 4.3 3.0 1.4 1.0 virginica #> 110 6.1 2.8 5.9 0.2 setosa #> 111 6.5 3.3 4.8 1.9 virginica #> 112 5.1 3.3 1.4 0.2 virginica #> 113 6.8 3.8 3.3 1.1 versicolor #> 114 6.7 2.5 5.6 1.4 virginica #> 115 6.1 2.6 4.4 2.4 virginica #> 116 4.4 3.4 6.1 1.8 setosa #> 117 5.2 2.8 4.0 1.8 virginica #> 118 5.1 3.1 1.5 1.8 versicolor #> 119 6.9 2.4 4.5 1.4 virginica #> 120 4.7 3.1 4.3 0.2 versicolor #> 121 5.0 3.6 5.8 0.2 setosa #> 122 6.1 2.8 1.5 0.3 versicolor #> 123 5.7 2.9 1.6 0.2 versicolor #> 124 5.5 3.1 4.0 0.1 virginica #> 125 5.0 3.1 1.7 1.4 versicolor #> 126 6.5 2.8 1.3 2.4 setosa #> 127 6.2 2.8 4.4 0.2 versicolor #> 128 7.0 3.2 3.0 0.2 versicolor #> 129 6.4 3.0 1.1 1.8 virginica #> 130 6.5 3.7 4.5 1.2 virginica #> 131 5.8 3.7 1.6 1.5 virginica #> 132 5.1 3.3 4.3 2.0 virginica #> 133 6.8 3.2 1.4 0.2 virginica #> 134 5.6 3.1 1.5 0.2 virginica #> 135 6.0 3.5 4.9 1.3 setosa #> 136 6.7 2.8 1.5 1.8 virginica #> 137 5.6 2.7 6.4 1.1 versicolor #> 138 5.2 3.5 5.9 2.1 setosa #> 139 6.5 4.1 1.5 2.2 setosa #> 140 5.0 2.3 1.5 1.1 virginica #> 141 5.7 2.8 4.5 2.2 virginica #> 142 4.8 2.7 1.7 1.4 versicolor #> 143 4.4 3.3 5.1 1.0 setosa #> 144 4.6 3.0 1.6 1.7 virginica #> 145 5.8 2.7 1.3 1.5 setosa #> 146 6.9 2.8 4.0 2.3 versicolor #> 147 4.9 2.7 5.2 1.6 virginica #> 148 5.1 3.1 1.5 0.4 versicolor #> 149 5.1 3.3 4.4 2.2 versicolor #> 150 7.3 3.4 1.6 0.4 setosa
# Example with missing data, no imputation iris_missings <- iris for(i in 1:10){ iris_missings[sample.int(nrow(iris_missings), 1, replace = TRUE), sample.int(ncol(iris_missings), 1, replace = TRUE)] <- NA } iris_miss_syn <- synthetic(iris_missings)
#> X Argument 'data' has missing values, but no 'missingness_expression' is specified. Listwise deletion is used. #> | | | 0% | |============== | 20% | |============================ | 40% | |========================================== | 60% | |======================================================== | 80% | |======================================================================| 100%
# Example with missing data, imputation by median/mode substitution # First, define a simple function for median/mode substitution: imp_fun <- function(x){ if(is.data.frame(x)){ return(data.frame(sapply(x, imp_fun))) } else { out <- x if(inherits(x, "numeric")){ out[is.na(out)] <- median(x[!is.na(out)]) } else { out[is.na(out)] <- names(sort(table(out), decreasing = TRUE))[1] } out } } # Then, call synthetic() with this function as missingness_expression: iris_miss_syn <- synthetic(iris_missings, missingness_expression = imp_fun(data))
#> Error in imp_fun(data): could not find function "imp_fun"