3

I have looked at a set of data and decided it would be good to remove outliers, with an outlier having the definition of being 2SD away from the mean.

If I have a set of data, say 500 rows with 15 different attributes, how can I remove all the rows which have 1 or more attribute which is 2 standard deviations away from the mean?

Is there an easy way to do this using R? Thanks,

4

2 に答える 2

3

おそらく多くの方法があり、おそらくこれに対処するためにパッケージを追加します。最初にこれを試すことをお勧めします:

library(sos); findFn("outlier")

scaleベクトルを標準化できる関数を使用して、求めていることを実行できる方法を次に示します。

#create a data set with outliers
set.seed(10)
dat <- data.frame(sapply(seq_len(5), function(i) 
    sample(c(1:50, 100:101), 200, replace=TRUE)))

#standardize each column (we use it in the outdet function)
scale(dat)

#create function that looks for values > +/- 2 sd from mean
outdet <- function(x) abs(scale(x)) >= 2
#index with the function to remove those values
dat[!apply(sapply(dat, outdet), 1, any), ]

したがって、あなたの質問に答えるには、はい、これを行うコードを1行のコードに要約できる簡単な方法があります。

dat[!apply(sapply(dat, function(x) abs(scale(x)) >= 2), 1, any), ]

そして、これ以上のことを行うパッケージがあると思います。このsosパッケージは、必要なことを行う関数を見つけるのに最適です (IMHO)。

于 2012-05-13T00:19:40.677 に答える
2
na.rm = TRUE, ...) {
qnt <- quantile(x, probs=c(.25, .75), na.rm = na.rm, ...)
H <- 1.5 * IQR(x, na.rm = na.rm)
y <- x
y[x < (qnt[1] - H)] <- NA
y[x > (qnt[2] + H)] <- NA
y
}
于 2013-01-02T21:44:59.843 に答える