R中箱线图的下四分位数和上四分位数
lower and upper quartiles in boxplot in R
我有
X=c(20 ,18, 34, 45, 30, 51, 63, 52, 29, 36, 27, 24)
使用 boxplot
,我正在尝试绘制 quantile(X,0.25)
和 quantile(X,0.75)
但这在 R
的箱线图中实际上不是相同的下四分位数和上四分位数
boxplot(X)
abline(h=quantile(X,0.25),col="red",lty=2)
abline(h=quantile(X,0.75),col="red",lty=2)
你知道为什么吗?
方框的值称为铰链,可能与四分位数重合(由 quantile(x, c(0.25, .075))
计算),但计算方式不同。
来自?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.
要查看这些值与奇数个观测值一致,请尝试以下代码:
set.seed(1234)
x <- rnorm(9)
boxplot(x)
abline(h=quantile(x, c(0.25, 0.75)), col="red")
差异源于分位数定义的歧义。没有一种方法是严格正确或不正确的——在分位数与特定数据点不完全重合且必须进行插值的情况下(例如偶数个数据点),只有不同的方法可以估算分位数。有点令人不安的是,boxplot
和 quantile
(以及其他提供汇总统计的函数)使用不同的默认方法来计算分位数,尽管可以使用 type =
中的参数覆盖这些默认值16=]
通过查看在 R 中生成分位数统计信息的各种方法,我们可以更清楚地看到这些差异。
boxplot
和 fivenum
给出相同的值:
boxplot.stats(X)$stats
# [1] 18.0 25.5 32.0 48.0 63.0
fivenum(X)
# [1] 18.0 25.5 32.0 48.0 63.0
在boxplot
和fivenum
中,下(上)四分位数相当于下(上)一半数据的中位数(包括完整数据的中位数):
c(median(X[ X <= median(X) ]), median(X[ X >= median(X) ]))
# [1] 25.5 48.0
但是,quartile
和 summary
做事的方式不同:
summary(X)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 18.00 26.25 32.00 35.75 46.50 63.00
quantile(X, c(0.25,0.5,0.75))
# 25% 50% 75%
# 26.25 32.00 46.50
这与 boxplot
和 fivenum
的结果之间的差异取决于函数如何在数据之间进行插值。 quartile
尝试通过估计累积分布函数的形状进行插值。根据 ?quantile
:
quantile returns estimates of underlying distribution quantiles based
on one or two order statistics from the supplied elements in x at
probabilities in probs. One of the nine quantile algorithms discussed
in Hyndman and Fan (1996), selected by type, is employed.
可在 ?quantile
中找到 quantile
用于估计数据分布函数的九种不同方法的完整详细信息,并且由于篇幅太长而无法在此处完整复制。需要注意的重要一点是,这 9 种方法取自 Hyndman 和 Fan (1996),他们推荐类型 8。quantile
使用的默认方法是类型 7,这是出于与 S 兼容的历史原因。
我们可以看到分位数中不同方法提供的四分位数估计值:
quantile_methods = data.frame(q25 = sapply(1:9, function(method) quantile(X, 0.25, type = method)),
q50 = sapply(1:9, function(method) quantile(X, 0.50, type = method)),
q75 = sapply(1:9, function(method) quantile(X, 0.75, type = method)))
# q25 q50 q75
# 1 24.0000 30 45.000
# 2 25.5000 32 48.000
# 3 24.0000 30 45.000
# 4 24.0000 30 45.000
# 5 25.5000 32 48.000
# 6 24.7500 32 49.500
# 7 26.2500 32 46.500
# 8 25.2500 32 48.500
# 9 25.3125 32 48.375
其中 type = 5
提供与 boxplot
相同的四分位数估计值。但是,当有奇数个数据时,type=7
将与箱线图统计重合。
我们可以通过自动选择类型 5 或 7 来证明这一点,具体取决于数据的数量是奇数还是偶数。下图中的箱线图显示了具有 1 到 30 个值的数据集的分位数,其中 boxplot
和 quantile
为奇数和偶数 N 提供相同的值:
layout(matrix(1:30,5,6, byrow = T), respect = T)
par(mar=c(0.2,0.2,0.2,0.2), bty="n", yaxt="n", xaxt="n")
for (N in 1:30){
X = sample(100, N)
boxplot(X)
abline(h=quantile(X, c(0.25, 0.5, 0.75), type=c(5,7)[(N %% 2) + 1]), col="red", lty=2)
}
Hyndman, R. J. 和 Fan, Y. (1996) 统计包中的样本分位数,美国统计学家 50, 361–365
可以在quantile
中使用type =
参数指定计算样本分位数的方法,但在boxplot
中不能。然而,CRAN 上有一个 R 包,qboxplot
,其中包含一个函数也称为 qboxplot
(因此 qboxplot::qboxplot
),允许使用 [=16= 指定分位数计算方法] 参数。
我有
X=c(20 ,18, 34, 45, 30, 51, 63, 52, 29, 36, 27, 24)
使用 boxplot
,我正在尝试绘制 quantile(X,0.25)
和 quantile(X,0.75)
但这在 R
boxplot(X)
abline(h=quantile(X,0.25),col="red",lty=2)
abline(h=quantile(X,0.75),col="red",lty=2)
方框的值称为铰链,可能与四分位数重合(由 quantile(x, c(0.25, .075))
计算),但计算方式不同。
来自?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.
要查看这些值与奇数个观测值一致,请尝试以下代码:
set.seed(1234)
x <- rnorm(9)
boxplot(x)
abline(h=quantile(x, c(0.25, 0.75)), col="red")
差异源于分位数定义的歧义。没有一种方法是严格正确或不正确的——在分位数与特定数据点不完全重合且必须进行插值的情况下(例如偶数个数据点),只有不同的方法可以估算分位数。有点令人不安的是,boxplot
和 quantile
(以及其他提供汇总统计的函数)使用不同的默认方法来计算分位数,尽管可以使用 type =
中的参数覆盖这些默认值16=]
通过查看在 R 中生成分位数统计信息的各种方法,我们可以更清楚地看到这些差异。
boxplot
和 fivenum
给出相同的值:
boxplot.stats(X)$stats
# [1] 18.0 25.5 32.0 48.0 63.0
fivenum(X)
# [1] 18.0 25.5 32.0 48.0 63.0
在boxplot
和fivenum
中,下(上)四分位数相当于下(上)一半数据的中位数(包括完整数据的中位数):
c(median(X[ X <= median(X) ]), median(X[ X >= median(X) ]))
# [1] 25.5 48.0
但是,quartile
和 summary
做事的方式不同:
summary(X)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 18.00 26.25 32.00 35.75 46.50 63.00
quantile(X, c(0.25,0.5,0.75))
# 25% 50% 75%
# 26.25 32.00 46.50
这与 boxplot
和 fivenum
的结果之间的差异取决于函数如何在数据之间进行插值。 quartile
尝试通过估计累积分布函数的形状进行插值。根据 ?quantile
:
quantile returns estimates of underlying distribution quantiles based on one or two order statistics from the supplied elements in x at probabilities in probs. One of the nine quantile algorithms discussed in Hyndman and Fan (1996), selected by type, is employed.
可在 ?quantile
中找到 quantile
用于估计数据分布函数的九种不同方法的完整详细信息,并且由于篇幅太长而无法在此处完整复制。需要注意的重要一点是,这 9 种方法取自 Hyndman 和 Fan (1996),他们推荐类型 8。quantile
使用的默认方法是类型 7,这是出于与 S 兼容的历史原因。
我们可以看到分位数中不同方法提供的四分位数估计值:
quantile_methods = data.frame(q25 = sapply(1:9, function(method) quantile(X, 0.25, type = method)),
q50 = sapply(1:9, function(method) quantile(X, 0.50, type = method)),
q75 = sapply(1:9, function(method) quantile(X, 0.75, type = method)))
# q25 q50 q75
# 1 24.0000 30 45.000
# 2 25.5000 32 48.000
# 3 24.0000 30 45.000
# 4 24.0000 30 45.000
# 5 25.5000 32 48.000
# 6 24.7500 32 49.500
# 7 26.2500 32 46.500
# 8 25.2500 32 48.500
# 9 25.3125 32 48.375
其中 type = 5
提供与 boxplot
相同的四分位数估计值。但是,当有奇数个数据时,type=7
将与箱线图统计重合。
我们可以通过自动选择类型 5 或 7 来证明这一点,具体取决于数据的数量是奇数还是偶数。下图中的箱线图显示了具有 1 到 30 个值的数据集的分位数,其中 boxplot
和 quantile
为奇数和偶数 N 提供相同的值:
layout(matrix(1:30,5,6, byrow = T), respect = T)
par(mar=c(0.2,0.2,0.2,0.2), bty="n", yaxt="n", xaxt="n")
for (N in 1:30){
X = sample(100, N)
boxplot(X)
abline(h=quantile(X, c(0.25, 0.5, 0.75), type=c(5,7)[(N %% 2) + 1]), col="red", lty=2)
}
Hyndman, R. J. 和 Fan, Y. (1996) 统计包中的样本分位数,美国统计学家 50, 361–365
可以在quantile
中使用type =
参数指定计算样本分位数的方法,但在boxplot
中不能。然而,CRAN 上有一个 R 包,qboxplot
,其中包含一个函数也称为 qboxplot
(因此 qboxplot::qboxplot
),允许使用 [=16= 指定分位数计算方法] 参数。