- A+
统计学中离群值被定义为离开大部分观测较远的样本点,多数是由于测量误差而产生。因此,数据分析中离群值的识别和移除(如有必要)是很重要的一个步骤。
鉴定离群值的方法有很多种,包括基于标准差的方法和基于四分位距的Tukey法。本文我将使用不依赖余数据分布类型的Tukey法做演示,该方法的另一个优势是无需考虑数据均值和方差,而这两个统计量恰恰很容易被极端值(离群值)影响。
我写了一个脚本来识别、描述、绘制并移除离群值。我先利用箱线图来鉴定样本点是否为离群值。在R中可以使用boxplot.stats()$out
命令来绘制这个图,该图能利用Tukey法将游离在1.5倍四分位距外的样本点单独绘制出来。
在描述数据时,我喜欢汇报数据中离群值的百分比和其均值,同时我也会分别计算包含和移除离群值后数据的均值。而在各种数据图中,箱线图和直方图值最能体现离群值的存在感,因此在下面的脚本中,我将绘制包含和移除离群值的这两类图形。
最后,在Selva的帮助下,我添加了一个简单问题(是/否)来询问是否要保留离群值。如果选择是,那么离群值将被NA
取代。
自定义函数
- outlierKD <- function(dt, var) {
- var_name <- eval(substitute(var),eval(dt))
- tot <- sum(!is.na(var_name))
- na1 <- sum(is.na(var_name))
- m1 <- mean(var_name, na.rm = T)
- par(mfrow=c(2, 2), oma=c(0,0,3,0))
- boxplot(var_name, main="With outliers")
- hist(var_name, main="With outliers", xlab=NA, ylab=NA)
- outlier <- boxplot.stats(var_name)$out
- mo <- mean(outlier)
- var_name <- ifelse(var_name %in% outlier, NA, var_name)
- boxplot(var_name, main="Without outliers")
- hist(var_name, main="Without outliers", xlab=NA, ylab=NA)
- title("Outlier Check", outer=TRUE)
- na2 <- sum(is.na(var_name))
- cat("Outliers identified:", na2 - na1, "\n")
- cat("Propotion (%) of outliers:", round((na2 - na1) / tot*100, 1), "\n")
- cat("Mean of the outliers:", round(mo, 2), "\n")
- m2 <- mean(var_name, na.rm = T)
- cat("Mean without removing outliers:", round(m1, 2), "\n")
- cat("Mean if we remove outliers:", round(m2, 2), "\n")
- response <- readline(prompt="Do you want to remove outliers
- and to replace with NA? [yes/no]: ")
- if(response == "y" | response == "yes"){
- dt[as.character(substitute(var))] <- invisible(var_name)
- assign(as.character(as.list(match.call())$dt), dt, envir = .GlobalEnv)
- cat("Outliers successfully removed", "\n")
- return(invisible(dt))
- } else{
- cat("Nothing changed", "\n")
- return(invisible(var_name))
- }
- }
你可以直接运行这些代码,只要把其中的数据集和变量的名称替换下。
- source("http://goo.gl/UUyEzD")
- outlierKD(dat, variable)
一个例子:
- Outliers identified: 58
- Propotion (%) of outliers: 3.8
- Mean of the outliers: 108.1
- Mean without removing outliers: 53.79
- Mean if we remove outliers: 52.82
- Do you want to remove outliers and to replace with NA? [yes/no]: y
- Outliers successfully removed
绘制的图形如下:
我知道这个脚本还可以添加不少功能和细节,很多地方还能优化。欢迎各位给我提供反馈。
你可以直接在下方留言,或者在Twitter上联系我。
本译文转载自图灵社区。原文Identify, describe, plot, and remove the outliers from the dataset,作者Klodian Dhana,译者钱亦欣。转载请注明链接http://www.ituring.com.cn/article/215618
支付宝打赏
微信打赏
赏