10.52 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. 2022). 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] 544454
# 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


Breiman, Leo, Adele Cutler, Andy Liaw, and Matthew Wiener. 2022. randomForest: Breiman and Cutler’s Random Forests for Classification and Regression. https://www.stat.berkeley.edu/~breiman/RandomForests/.

Your donation will support ongoing availability and give you access to the PDF version of this 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-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0