R语言做生信R

R语言之缺失值和异常值处理

2019-03-14  本文已影响359人  Oodelay
library(VIM)
data(sleep)
str(sleep)
summary(sleep)
head(sleep)

一、处理缺失值

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)
对特定变量删除NA后

1.2 删除所有含NA的行

sleep_del_all = na.omit(sleep)
# sleep_del_all = sleep[complete.cases(sleep),]#同上
md.pattern(sleep_del_all)
删除所有NA

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))
变量NonD数据分布情况
#简单可视化
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')

填值前后变量NonD数据分布变化

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)

before_knnImputation

after_knnImputation

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)

befor_regressionImp

after_regressionImp

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)

befor_rf

after_rf


二、处理异常值

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')

sigular_boxplot

sigular_ggboxplot

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)

after_block

上一篇下一篇

猜你喜欢

热点阅读