代码之家  ›  专栏  ›  技术社区  ›  Yehuda

用'boxplot.stats删除r中的数据帧异常值`

  •  1
  • Yehuda  · 技术社区  · 6 年前

    我在R是比较新的,所以请容忍我。

    我正在使用Ames数据集(数据集的完整描述 here ;链接到数据集下载 here )

    我正在尝试创建一个子集数据框架,它将允许我运行线性回归分析,并且我正在尝试使用 boxplot.stats 功能。我使用以下代码创建了一个包含示例的框架:

    regressionFrame <- data.frame(subset(ames_housing_data[,c('SalePrice','GrLivArea','LotArea')] , BldgType == '1Fam'))
    

    我的下一个目标是删除异常值,因此我尝试使用 which() 功能:

    regressionFrame <- regressionFrame[which(regressionFrame$GrLivArea != boxplot.stats(regressionFrame$GrLivArea)$out),]
    

    不幸的是,这产生了

    lapply()

    1 回复  |  直到 6 年前
        1
  •  2
  •   Zheyuan Li    6 年前

    boxplot.stats

    != $out 1:5 != 1:3 !(1:5 %in% 1:3)

    regressionFrame <- subset(regressionFrame,
                              subset = !(GrLivArea %in% boxplot.stats(GrLivArea)$out))
    

    1:6 != 1:3 1:3 1:6 1:6 != c(1:3, 1:3) .


    一个简单的例子。

    x <- c(1:10/10, 101, 102, 103)  ## has three outliers: 101, 102 and 103
    out <- boxplot.stats(x)$out  ## `boxplot.stats` has picked them out
    x[x != out]  ## this gives a warning and wrong result
    x[!(x %in% out)]  ## this removes them from x