使用boxplotting方法从数据框中删除异常值

问题描述 投票:0回答:2

我有一个包含大约15个变量的数据框。我必须从变量中删除异常值。

在web上的教程之后,我使用boxplotting方法来删除异常值。我正在使用一种堆叠的方式从数据框中逐个删除异常值,直到所有数据都被处理。

这是我的代码。我的问题是,它是删除异常值或如何改进代码的好方法。

#removong outliers from the columns
outliers <- boxplot(outlier_H_rem$var1, plot=FALSE)$out
if(length(outliers) == 0){ outlier_H_rem1<-outlier_H_rem
boxplot(outlier_H_rem1$var1)} else { 
outlier_H_rem1<-outlier_H_rem[-which(outlier_H_rem$var1 %in% outliers),]
var1<-outlier_H_rem1$var1}
boxplot(outlier_H_rem1$var1)

outliers <- boxplot(outlier_H_rem1$var2, plot=FALSE)$out
if(length(outliers) == 0){ outlier_H_rem2<-outlier_H_rem1
boxplot(outlier_H_rem2$var2)} else { 
outlier_H_rem2<-outlier_H_rem1[-which(outlier_H_rem1$var2 %in% outliers),]
moisture2<-outlier_H_rem2$var2}
boxplot(outlier_H_rem2$var2)

outlier_H_rem是我每次使用下一个var outlier_H_rem1 $ var1,outlier_H_rem2 $ var2,outlier_H_rem3 $ var3到最后一个var进行测试的堆叠数据帧。 outlier_H_rem15 $ var15是使用所有变量处理的最后一个堆叠数据帧。

r outliers
2个回答
0
投票

我可以从你对@Humpelstielzchen的回答中读到你想把变量作为单独的向量来处理,所以我会按照这个回答,但请记住,后续的变量合并可能很困难,因为当你失去值的位置顺序时将它们作为单独的向量提取,然后删除一些观察。

在下面的示例中,我创建了一些示例数据来说明此问题。请注意,var3没有异常值。您将如何合并数据(它们将具有不同的长度)?此外,即使var1和var2在异常值移除后最终都有11个观察值,矢量中的最后一个位置来自原始数据中的位置11和12。

鉴于您仍然可以使用此方法,那么您的方法将起作用。我已经对你的代码发表了一些评论。

library(tidyverse)

set.seed(1)

outlier_H_rem <- tibble(
  var1 = rnorm(10, 0, 1),
  var2 = rnorm(10, 0, 1),
  var3 = rnorm(10, 0, 1)) %>% 
  #Introduce outliers
  rbind(c(5, 0, 0), c(0,7, 0))

outlier_H_rem

#removeing outliers from the columns
outliers <- boxplot(outlier_H_rem$var1, plot=FALSE)$out

if(length(outliers) == 0){ 
  outlier_H_rem1 <- outlier_H_rem
  #boxplot(outlier_H_rem1$var1) - This line is irrelevant as you create the plot again after the if else call
  } else { 
  outlier_H_rem1 <- outlier_H_rem[-which(outlier_H_rem$var1 %in% outliers),]
  var1 < -outlier_H_rem1$var1 #What is the purpose of this line?
  }

boxplot(outlier_H_rem1$var1)

0
投票

我可以建议采用略有不同的方法。

将数据从宽变换为长形,然后使用分位数和分位数范围计算异常值。

然后过滤掉异常值并转换回宽泛的形式。删除带有异常值的行会使您获得所需的结果

建立在@Steen Harsted上

library(tidyverse)

set.seed(1)

outlier_H_rem <- tibble(
  var1 = rnorm(10, 0, 1),
  var2 = rnorm(10, 0, 1),
  var3 = rnorm(10, 0, 1)) %>% 
  #Introduce outliers
  rbind(c(5, 0, 0), c(0,7, 0))

outlier_H_rem

# A tibble: 12 x 3
var1    var2    var3
<dbl>   <dbl>   <dbl>
  1 -0.626  1.51    0.919 
2  0.184  0.390   0.782 
3 -0.836 -0.621   0.0746
4  1.60  -2.21   -1.99  
5  0.330  1.12    0.620 
6 -0.820 -0.0449 -0.0561
7  0.487 -0.0162 -0.156 
8  0.738  0.944  -1.47  
9  0.576  0.821  -0.478 
10 -0.305  0.594   0.418 
11  5      0       0     
12  0      7       0 

outlier_H_rem %>% 
  # Collect dat in tidy form
  tidyr::gather("Feature", "Value", everything()) %>%
  ggplot2::ggplot(aes(x=Feature, y=Value)) +geom_boxplot()

enter image description here

现在,这里是如何使用tidyverse中的工具识别异常值

outlier_H_rem %>% 
  # Collect data in tidy form
  tidyr::gather("Feature", "Value", everything()) %>% 
  # Group by "Feature" and calculate outliers using iqr and quantiles
  # Also adding a row counter
  group_by(Feature) %>% 
  mutate(r=1:n()) %>%
  mutate(q1 = quantile(Value,probs=0.25),
         q3 = quantile(Value,probs=0.75),
         iqr = IQR(Value),
         outlier = if_else((q1-1.5*iqr)>Value | (q3+1.5*iqr)<Value, TRUE, FALSE)) %>% 
  # Filter out the ouliers
  filter(!outlier) %>% 
  # deselect calculated rows
  select(-q1, -q3, -iqr, -outlier) %>%
  # Spread the results again. 
  # optionally remove rows with rows with NA (contained outliers) using na.omit()
  spread(Feature, Value) %>% 
  # remove row counter
  select(-r)

# A tibble: 12 x 3
var1     var2     var3
*   <dbl>    <dbl>    <dbl>
  1  -0.626   1.51     0.919 
2   0.184   0.390    0.782 
3  -0.836  -0.621    0.0746
4   1.60   NA       NA     
5   0.330   1.12     0.620 
6  -0.820  -0.0449  -0.0561
7   0.487  -0.0162  -0.156 
8   0.738   0.944   NA     
9   0.576   0.821   -0.478 
10  -0.305   0.594    0.418 
11  NA       0        0     
12   0      NA        0 
© www.soinside.com 2019 - 2024. All rights reserved.