Boxplot.stats R 不识别异常值

Boxplot.stats R not identifying outliers

我已经使用 boxplot.stats$out 来获取 R 中列表的异常值。但是我注意到很多时候它无法识别异常值。例如:

list = c(3,4,7,500)
boxplot.stats(list)

$`stats`
[1]   3.0   3.5   5.5 253.5 500.0

$n
[1] 4

$conf
[1] -192  203

$out
numeric(0)

quantile(list)

    0%    25%    50%    75%   100% 
  3.00   3.75   5.50 130.25 500.00 

130.25+1.5*IQR(list) = 320

如您所见,boxplot.stats() 函数未能找到离群值 500,即使当我查看文档时他们使用的是 Q1/Q3+/-1.5*IQR 方法。所以 500 应该被确定为异常值,但它显然没有找到它,我不确定为什么?

我已经尝试使用 5 个元素而不是 4 个元素的列表,或者使用非常小而不是非常大的离群值,但我仍然遇到同样的问题。

如果您仔细阅读帮助页面 help("boxplot.stats"),return 值部分说明如下。我的重点。

stats
a vector of length 5, containing the extreme of the lower
whisker, the lower ‘hinge’, the median, the upper ‘hinge’ and
the extreme of the upper whisker.

然后,在同一部分,再次强调我的重点。

out
the values of any data points which lie beyond the extremes of the whiskers (if(do.out)).

您的数据有4分。正如在列表成员 $stats 中 return 编辑的那样,上胡须的 极值 500.0,这是您的数据的最大值。没有错误。

注意 "stats" 部分的第三个数字是 253.5,而不是 130.25 boxplot.stats 的文档说:

The two ‘hinges’ are versions of the first and third quartile, i.e., close to quantile(x, c(1,3)/4). The hinges equal the quartiles for odd n (where n <- length(x)) and differ for even n. Whereas the quartiles only equal observations for n %% 4 == 1 (n = 1 mod 4), the hinges do so additionally for n %% 4 == 2 (n = 2 mod 4), and are in the middle of two observations otherwise

换句话说,对于您的数据,它使用 (500+7)/2 作为 Q3 值
(顺便说一句 (3+4)/2 = 3.5 作为 Q1,而不是你从 quantile)。箱线图将使用边界 253.5 + 1.5*(253.5 - 3.5) = 628.5

试试这个,

library (car)
Boxplot (Petal.Length ~ Species, id = list (n=Inf))

识别所有异常值