【问题标题】:Boxplot missing an outlier in R箱线图在 R 中缺少异常值
【发布时间】:2021-01-14 14:12:38
【问题描述】:

我有以下数据:

d = c(58.33333, 58.33333, 56.66667, 45.00000, 60.00000, 70.00000, 61.66667, 51.66667, 58.33333, 71.66667, 50.00000, 63.33333)

数据的汇总统计为:

Min.    1st Qu.  Median    Mean    3rd Qu.    Max. 
45.00   55.42    58.33     58.75   62.08      71.67

当我使用函数boxplot(d, range=1.5) 绘制数据箱线图时,我得到以下信息:

根据我的计算,下胡须只能向下延伸到55.42 - 1.5*(62.08-55.42) = 45.43。话虽如此,最小数据点 (45) 应显示为异常值,但该图实际上将胡须向下延伸到该最小值。为什么会这样?是否有一个我不知道的舍入程序将 45 包含在范围内?比如与两点的接近程度(45.43 和 45)有关的东西?

【问题讨论】:

    标签: r boxplot outliers


    【解决方案1】:

    这里的问题是boxplot 使用与summaryquantile 不同的方法计算四分位数。来自?boxplot.stats

    这两个“铰链”是第一个和第三个四分位数的版本,即接近 quantile(x, c(1,3)/4)。铰链等于奇数 n 的四分位数(其中 n

    例子:

    boxplot.stats(d)$stats
    [1] 45.00000 54.16667 58.33333 62.50000 71.66667
    
    fivenum(d) # the same
    [1] 45.00000 54.16667 58.33333 62.50000 71.66667
    
    quantile(d) # different
          0%      25%      50%      75%     100% 
    45.00000 55.41667 58.33333 62.08333 71.66667 
    

    quantile指定方法(类型)以得到相同的结果:

    quantile(d, type = 2)
          0%      25%      50%      75%     100% 
    45.00000 54.16667 58.33333 62.50000 71.66667
    

    【讨论】:

    • 感谢您的解释。在这方面,如何修改箱线图公式,以便使用summary 的输出计算图?
    • 您需要重写boxplot.stats 函数,将stats <- stats::fivenum(x, na.rm = TRUE) 行替换为stats <- stats::quantile(x, type = 2, na.rm = TRUE)。或者,您可以在ggplot2 中使用geom_boxplot,当我使用您的数据进行尝试时,它确实显示了异常值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 2021-11-06
    • 2020-08-27
    • 2014-06-21
    • 2012-06-23
    相关资源
    最近更新 更多