【问题标题】:Substitute outliers for each group替换每个组的异常值
【发布时间】:2017-07-01 01:09:17
【问题描述】:

我正在尝试使用 by 函数根据组变量替换数据框中许多变量的异常值。 以下是我的努力。但是,我得到一个错误。

# R code:
library(tidyverse)
library(dplyr)
# outlier function definition
my_outlier <- function(x){
 stats <- boxplot.stats(unlist(x))
 outlier <- stats$out
 outlier_idx <- which(unlist(x) %in% outlier)
 max <- max(x[-outlier_idx]); min <- min(x[-outlier_idx])
 x <- ifelse(x>max, max,ifelse(x < min, min, x) ) 
 return(x)
}
# use the above defined func to  substitue outliers of 1 variable in a dataframe, according to a Group variable.
group_data <- as_tibble(data.frame(x=c(runif(10),2.5,-2.3,runif(10,1,2),3.5,-1.5), group=c(rep(1,12),rep(2,12)) ) )

View(group_data)
by(group_data$x, group_data$group, my_outlier, simplify=FALSE)
# use the above defined func to  substitue outliers of 1+ variable in a dataframe, according to a Group variable.    
group_datas <- as_tibble(data.frame(x=c(runif(10),2.5,-2.3,runif(10,1,2),3.5,-1.5), 
                               y=c(runif(10,2,3),4,-1,runif(10,3,4),6,-1),
                               group=c(rep(1,12),rep(2,12)) ) )
by(group_data[,1:2], group_data$group, my_outlier)

当使用我定义的函数替换数据框中 1+ 变量的异常值时,根据 Group 变量,我得到了一个错误。

我不知道我的代码的哪一部分导致了错误。

【问题讨论】:

  • 您缺少列索引,您要计算max &lt;- max(x[-outlier_idx]); min &lt;- min(x[-outlier_idx]) 中的最大值。如果要考虑包括组列在内的整个矩阵,则需要在outlier_idx 之后添加逗号作为max &lt;- max(x[-outlier_idx,]); min &lt;- min(x[-outlier_idx,])。还要注意ifelse 只返回标量而不是向量。

标签: r outliers


【解决方案1】:

对于多变量异常值,boxplot.stats 不起作用,您可以使用 outliers 包中的 outlier 测试:

library(outliers)
my_outlier2 <- function(x, plot=TRUE){
  x <- as.matrix(x)
  outlier <- rbind(outlier(x),outlier(x,opposite=TRUE))
  outlier_idx <- which(duplicated(rbind(x, outlier), fromLast=TRUE))#which(apply(mapply(x, outlier, FUN="=="), MARGIN=1, FUN=all))
  if (plot) { # if 2-D data, visualize
    plot(x[,1], x[,2], pch=19, xlab='x', ylab='y')
    points(x[outlier_idx,1], x[outlier_idx,2], col='red', pch=8, cex=2)
    legend('topleft', legend=c('non-outlier', 'outlier'), pch=c(19, 8), col=c('black', 'red'))
  }
  x <- x[-outlier_idx,]
  return(x)
}

# use the above defined func to  substitue outliers of 1+ variable in a dataframe, according to a Group variable.    
group_datas <- as_tibble(data.frame(x=c(runif(10),2.5,-2.3,runif(10,1,2),3.5,-1.5), 
                                    y=c(runif(10,2,3),4,-1,runif(10,3,4),6,-1),
                                    group=c(rep(1,12),rep(2,12)) ) )
by(group_datas[,1:2], group_datas$group, my_outlier2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 2018-01-05
    • 2020-06-09
    • 2018-07-12
    相关资源
    最近更新 更多