10.50 Missing Values

20180726 Missing values for the variables are an issue for some but not all algorithms. For example randomForest::randomForest() omits observations with missing values by default whilst rpart::rpart() has a particularly well developed approach to dealing with missing values.

We may want to impute missing values in the data (though it is not always wise to do so). Here we do this using randomForest::na.roughfix() from randomForest (Breiman et al. 2018). This function provides, as the name implies, a rather basic algorithm for imputing missing values. Because of this we will demonstrate the process but then restore the original dataset—we will not want this imputation to be included in our actual dataset.

# Backup the dataset so we can restore it as required.

ods <- ds
# Count the number of missing values.

ds[vars] %>%  is.na() %>% sum()
## [1] 442254
# Impute missing values.

ds[vars] %<>% na.roughfix()

# Confirm that no missing values remain.

ds[vars] %>%  is.na() %>% sum()
## [1] 0

As foreshadowed we now restore the dataset with its original contents.

# Restore the original dataset.

ds <- ods


Your donation will support ongoing development and give you access to the PDF version of the book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 1995-2021 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0.