使用箱线图去除异常值

克雷莱克斯

我正在尝试从我的数据集中删除异常值,但我似乎没有工作。我已经在数字向量上测试了这种技术并且它有效。但是,当尝试将其应用于来自 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")
克里斯·鲁勒曼

据我所知,问题在于您没有从每列中删除异常值。这是一个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

以及sapplydf整体(即每列)中删除它们的函数

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

移除后的箱线图: 在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章