This vignette provides a brief demonstration of rMIDAS. We show how to use the package to multiply impute missing values in the Adult census dataset, which is commonly used for benchmarking machine learning tasks.
rMIDAS relies on Python to implement the MIDAS imputation algorithm, so you should ensure you have a Python 3.X environment installed on your machine. When the package is first loaded, it will try and automatically locate a suitable Python environment; if this fails, you will receive a warning message. When this occurs, users can manually specify a Python binary using
set_python_env() or via reticulate directly (see this vignette for more information).
Once rMIDAS is initialized, we can load our data. For the purpose of this example, we’ll use a subset of the Adult data:
As the dataset has a very low proportion of missingness (one of the reasons it is favored for machine learning tasks), we randomly set 10% of observed values as missing in each column using the rMIDAS’
Next, we make a list of all categorical and binary variables, before preprocessing the data for training using the
convert() function. Setting the
minmax_scale argument to
TRUE ensures that continuous variables are scaled between 0 and 1, which can substantially improve convergence in the training step. All pre-processing steps can be reversed after imputation:
The data are now ready to be fed into the MIDAS algorithm, which involves a single call of the
train() function. At this stage, we specify the dimensions, input corruption proportion, and other hyperparameters of the MIDAS neural network as well as the number of training epochs:
Once training is complete, we can generate any number of completed datasets using the
complete() function (below we generate 10). The completed dataframes can also be saved as ‘.csv’ files using the
file_root arguments (not demonstrated here). By default,
complete() unscales continuous variables and converts binary and categorical variables back to their original form.
Since the MIDAS algorithm returns predicted probabilities for binary and categorical variables, imputed values of such variables can be generated using one of two options. When
fast = FALSE (the default),
complete() uses the predicted probabilities for each category level to take a weighted random draw from the set of all levels. When
fast = TRUE, the function selects the level with the highest predicted probability. If completed datasets are very large or
complete() is taking a long time to run, users may benefit from choosing the latter option:
combine() function allows users to estimate regression models on the completed datasets with Rubin’s combination rules. This function wraps the
glm() package, whose arguments can be used to select different families of estimation methods (gaussian/OLS, binomial etc.) and to specify other aspects of the model: