- Advanced Machine Learning with R
- Cory Lesmeister Dr. Sunil Kumar Chinnamgari
- 422字
- 2021-06-24 14:24:33
Handling missing values
Dealing with missing values can be a little tricky as there's a number of ways to approach the task. We've already seen in the section on descriptive statistics that there're missing values. First of all, let's get a full accounting of the missing quantity by feature, then we shall discuss how to deal with them. What I'm going to demonstrate in the following is how to put the count by feature into a dataframe that we can explore within RStudio:
na_count <-
sapply(gettysburg, function(y)
sum(length(which(is.na(
y
)))))
na_df <- data.frame(na_count)
View(na_df)
The following is a screenshot produced by the preceding code, after sorting the dataframe by descending count:

You can clearly see the count of missing by feature with the most missing is ironically named missing with a total of 17 observations.
So what should we do here or, more appropriately, what can we do here? There're several choices:
- Do nothing: However, some R functions will omit NAs and some functions will fail and produce an error.
- Omit all observations with NAs: In massive datasets, they may make sense, but we run the risk of losing information.
- Impute values: They could be something as simple as substituting the median value for the missing one or creating an algorithm to impute the values.
- Dummy coding: Turn the missing into a value such as 0 or -999, and code a dummy feature where if the feature for a specific observation is missing, the dummy is coded 1, otherwise, it's coded 0.
I could devote an entire chapter, indeed a whole book on the subject, delving into missing at random and others, but I was trained—and, in fact, shall insist—on the latter method. It's never failed me and the others can be a bit problematic. The benefit of dummy coding—or indicator coding, if you prefer—is that you don't lose information. In fact, missing-ness might be an essential feature in and of itself.
So, here's an example of how I manually code a dummy feature and turn the NAs into zeroes:
gettysburg$missing_isNA <-
ifelse(is.na(gettysburg$missing), 1, 0)
gettysburg$missing[is.na(gettysburg$missing)] <- 0
The first iteration of code creates a dummy feature for the missing feature and the second changes any NAs in missing to zero. In the upcoming section, where the dataset is fully processed (treated), the other missing values will be imputed.
- Learning SQL Server Reporting Services 2012
- 新型電腦主板關鍵電路維修圖冊
- Python GUI Programming:A Complete Reference Guide
- 深入淺出SSD:固態存儲核心技術、原理與實戰(第2版)
- 數字邏輯(第3版)
- Camtasia Studio 8:Advanced Editing and Publishing Techniques
- Creating Flat Design Websites
- 筆記本電腦使用、維護與故障排除從入門到精通(第5版)
- 電腦高級維修及故障排除實戰
- BeagleBone Robotic Projects
- 基于Proteus仿真的51單片機應用
- 龍芯自主可信計算及應用
- 深入理解序列化與反序列化
- Angular 6 by Example
- 分布式存儲系統:核心技術、系統實現與Go項目實戰