Data Science Desktop Survival Guide
by Graham Williams |
|||||
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. 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()
# Impute missing values.
ds[vars] %<>% na.roughfix() # Confirm that no missing values remain. ds[vars] %>% is.na() %>% sum()
As foreshadowed we now restore the dataset with its original contents.
|
# Restore the original dataset.
ds <- ods
|