- 加载并查看数据基本情况
library(VIM)
data(sleep)
str(sleep)
summary(sleep)
head(sleep)
一、处理缺失值
- 查看NA的分布情况,有一个直观了解
library('mice')
md.pattern(sleep)
matrixplot(sleep)
- 根据NA的分布情况,获取数据子集
#统计每一列NA的数量
na_flag <- apply(is.na(sleep), 2, sum)
# na_flag <- md.pattern(sleep,plot = F) %>% .[nrow(.),-ncol(.)]#同上
library('dplyr')
#获取含有NA的列和不含NA的列
na_col = na_flag[na_flag > 0] %>% names()
full_col = setdiff(names(sleep),na_col)
# fill_col = names(sleep)[!(names(sleep) %in% na_col)]同上
# 获取所有含有NA的行
na_df = sleep[!complete.cases(sleep),]
#获取所有不含NA的行
full_df = na.omit(sleep)
#fill_df = sleep[complete.cases(sleep),]同上
#对变量进行重新排序
sleep = sleep[,c(na_col,full_col)]
1. 删除法
当缺失值占比不大时,直接删除缺失部分是最简单的办法
1.1 针对特定变量删除NA
sleep_del_Gest = sleep[!is.na(sleep$Gest),]
md.pattern(sleep_del_Gest)
1.2 删除所有含NA的行
sleep_del_all = na.omit(sleep)
# sleep_del_all = sleep[complete.cases(sleep),]#同上
md.pattern(sleep_del_all)
1.3 观察变化前后变量之间的相关性
library(PerformanceAnalytics)
chart.Correlation(sleep,histogram = T)
chart.Correlation(sleep_del_all,histogram = T)
2. 插补法
- 若缺失值占比过大,直接删除会损失大量的信息。这种情况应该对缺失值进行填补。
- 若属性是连续的,则使用该属性存在值的均值去插补缺失值;
- 若属性是离散的,则可取该属性的众数来插补缺失值。
2.1. 使用均值,中值等针对某一个变量填值
#观察数据分布情况
NonD_var = c(var = 'NonD',
mean = mean(sleep$NonD,na.rm = T),
median = median(sleep$NonD,na.rm = T),
quantile(sleep$NonD,c(0,0.01,0.1,0.25,0.5,0.75,0.9,1),na.rm = T),
max = max(sleep$NonD,na.rm = T),
missing = sum(is.na(sleep$NonD)))
View(t(NonD_var))
#简单可视化
op <- par(mfrow = c(1,2))
hist(sleep$NonD,freq = F,col = 'lightblue',main = 'Befor')
#使用该变量现有数据的均值替换缺失值
library('Hmisc')
sleep$NonD = impute(sleep$NonD, fun = mean)#impute(x,2.5), impute(x,mean), impute(x,"random")
hist(NonD,freq = F,col = 'pink',main = 'After')
2.2. 基于kmeans均值算法填值
- 只适用于数值型缺失
####重新加载原始数据并对变量进行排序
data("sleep",package = 'VIM')
sleep = sleep[,c(na_col,full_col)]
library('DMwR')
#以距离最近的3个值根据距离进行加权平均来填值
sleep_fill_knn = knnImputation(sleep, k = 10, meth = 'weighAvg')
md.pattern(sleep_fill_knn)
sleep_fill_knn = sleep_fill_knn[,names(sleep)]
#观察变换前后数据的分布情况
op <- par(mfrow = c(5,2))
for(fct in na_col){
hist(sleep[,fct],col = 'lightblue',freq = F,xlab = fct,main = 'Befor')
hist(sleep_fill_knn[,fct],col = 'pink',freq = F,xlab = fct,main = 'After')
}
par(op)
#观察变换前后变量间的相关性
library(PerformanceAnalytics)
chart.Correlation(sleep,histogram = T)
chart.Correlation(sleep_fill_knn,histogram = T)
2.3. 基于回归算法填值
f = as.formula(paste(paste(na_col,collapse = ' + '),'~',paste(fill_col,collapse = ' + ')))
sleep_fill_reg = regressionImp(f,data = sleep)
#这里的formular = y ~ .,y是response variable,即要插补的变量。
#我这里使用了所有包含NA的变量来对所有不含NA的变量进行回归,有些极端,仅供参考。
sleep_fill_reg = sleep_fill_reg[,names(sleep)]
md.pattern(sleep_fill_reg)
#观察变换前后数据的分布情况,变量间的相关性
library(PerformanceAnalytics)
chart.Correlation(sleep,histogram = T)
chart.Correlation(sleep_fill_reg,histogram = T)
2.4. 基于随机森林替换
if(!require('randomForest'))(
install.packages('randomForest')
)
sleep_fill_rf = rfImpute(Danger ~ .,sleep)
sleep_fill_rf = sleep_fill_rf[,names(sleep)]
#观察变换前后数据的分布情况,变量间的相关性
library(PerformanceAnalytics)
chart.Correlation(sleep,histogram = T)
chart.Correlation(sleep_fill_rf,histogram = T)
二、处理异常值
1.1 单变量异常值检测
lb = c('ggplot2','reshape2','dplyr')
lapply(lb,require,character.only = T)
boxplot(sleep_fill_rf,frame = T)
sleep_fill_rf %>% melt() %>%
ggplot(aes(NULL,value)) +
geom_boxplot(aes(fill = variable)) +
facet_wrap(variable ~. , scales = 'free_y')
1.2 盖帽法处理异常值
#采用盖帽法,用10%处的数据覆盖分布在10%以下的数据,用90%处的数据覆盖分布在99%以上的数据。
#这里的10%和90%取值有些极端,及供参考。
block<-function(x,lower=T,upper=T){
if(lower){
q1<-quantile(x,0.1)
x[x<=q1]<-q1
}
if(upper){
q99<-quantile(x,0.90)
x[x>q99]<-q99
}
return(x)
}
sleep_fill_rf_blk = sapply(sleep_fill_rf,block)
boxplot(sleep_fill_rf_blk,frame = T)
- 参考
apricoter