【问题标题】:Removing outliers using box plot使用箱线图去除异常值
【发布时间】:2021-03-01 12:47:38
【问题描述】:

我正在尝试从我的数据集中删除异常值,但我似乎没有工作。我已经在数字向量上测试了这种技术并且它有效。但是,当尝试将其应用于来自 csv 文件的数据集时。它似乎没有做任何事情。我做错了什么还是我完全错误地接近异常值?

  • 原始数据集将“样本”编号作为 csv 文件中的第一列,将“类”(货车、汽车等)作为最后一列,因此我从数据集中删除了这些列

去除异常值后(不确定是否正确)

CSV 文件数据测试

vehicles <- read.csv("C:/Users/Documents/vehiclescsv.csv") #load csv file
vehicles2 <- vehicles[,-1] #remove first column "Sample" number
vehicles3 <- vehicles2[,-19] #remove final column "Class" name
vehData <- vehicles3
vehClass <- vehicles$Class #store final column, class name

boxplot(vehData) #plot data to see outliers
OutVals <- boxplot(vehData)$out #find and store outliers
OutVals
vehDataRemoveOut <- vehData[!(vehData%in%OutVals)] #remove the outliers
length(vehData) - length(vehDataRemoveOut)# see if outliers have been removed
boxplot(vehDataRemoveOut) #re-plot to see changes

向量测试

x <- c(10,20,30,40,50,1000)
boxplot(x)
OutVals <- boxplot(x)$out
OutVals

RemovedOut <- x[!(x %in%OutVals)]
length(x) - length(RemovedOut)

boxplot(RemovedOut)

用于测试的数据 dput(head()) - 根据要求

 vehData <-
  structure(
    list(
      Samples = 1:6,
      Comp = c(95L, 91L, 104L, 93L, 85L,
               107L),
      Circ = c(48L, 41L, 50L, 41L, 44L, 57L),
      D.Circ = c(83L,
                 84L, 106L, 82L, 70L, 106L),
      Rad.Ra = c(178L, 141L, 209L, 159L,
                 205L, 172L),
      Pr.Axis.Ra = c(72L, 57L, 66L, 63L, 103L, 50L),
      Max.L.Ra = c(10L,
                   9L, 10L, 9L, 52L, 6L),
      Scat.Ra = c(162L, 149L, 207L, 144L, 149L,
                  255L),
      Elong = c(42L, 45L, 32L, 46L, 45L, 26L),
      Pr.Axis.Rect = c(20L,
                       19L, 23L, 19L, 19L, 28L),
      Max.L.Rect = c(159L, 143L, 158L, 143L,
                     144L, 169L),
      Sc.Var.Maxis = c(176L, 170L, 223L, 160L, 241L, 280L),
      Sc.Var.maxis = c(379L, 330L, 635L, 309L, 325L, 957L),
      Ra.Gyr = c(184L,
                 158L, 220L, 127L, 188L, 264L),
      Skew.Maxis = c(70L, 72L, 73L,
                     63L, 127L, 85L),
      Skew.maxis = c(6L, 9L, 14L, 6L, 9L, 5L),
      Kurt.maxis = c(16L,
                     14L, 9L, 10L, 11L, 9L),
      Kurt.Maxis = c(187L, 189L, 188L, 199L,
                     180L, 181L),
      Holl.Ra = c(197L, 199L, 196L, 207L, 183L, 183L),
      Class = c("van", "van", "saab", "van", "bus", "bus")
    ),
    row.names = c(NA,
                  6L), class = "data.frame")

【问题讨论】:

  • 不太清楚:有什么问题?你有什么错误吗?
  • 感谢您的回复!没有错误只是在执行 OutVals
  • 你能用dput(head())显示你的数据框的sn-p吗?
  • 我已将 dput(head(vehData)) 的结果添加到帖子中:D
  • 请提供dput 可复制形式(就像代码一样)!

标签: r outliers


【解决方案1】:

据我所知,问题在于您没有从每列中删除异常值。这是base R 解决方案:

数据:

x <- c(1,0.8,3,4,10,0.01)
y <- c(1, 7, 0.5, 0.8,0.7, 1.1)
z <- c(0,0.1,0.2, 5, 0.5,0.6)
df <- data.frame(x,y,z)

箱线图如下所示:

现在将异常值存储在向量中:

a <- boxplot(df)$out

sapply一个函数将它们从df作为一个整体(即每一列)移除:

df_new <- sapply(df, function(x) x[!x %in% a])

移除后的箱线图:

【讨论】:

  • 非常感谢。我认为这可能有效。我尝试了这个并将我的结果添加到帖子的顶部,比较之前和之后。我做对了吗?你也介意解释一下这行代码是如何工作的 df_new
  • 如果有帮助,请考虑接受和/或支持答案。您更新的箱线图看起来相当不错(除了一两个仍然存在的异常值)。
  • 要删除最后的,我会重复这个过程吗?再次感谢您的帮助。我也接受了答案。
  • 好吧,您可以尝试重复该过程,但我不知道这是否适合使用...
  • 再想一想,请注意,删除异常值并不能保证修改后的数据集不会也有异常值,因为 R 会重新计算什么是典型和非典型数据点 每组新数据。例如,一旦删除了一个极端数据点,那么下一个相当不典型的数据点可能会成为新分布中的新异常值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-06
  • 2019-05-21
  • 2017-12-03
  • 1970-01-01
  • 2017-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多