The R Project SVN R

Rev

Rev 88751 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/grDevices/man/boxplot.stats.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
88529 hornik 3
% Copyright 1995-2025 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
27442 ripley 6
\name{boxplot.stats}
7
\title{Box Plot Statistics}
8
\usage{
30915 ripley 9
boxplot.stats(x, coef = 1.5, do.conf = TRUE, do.out = TRUE)
27442 ripley 10
}
56186 murdoch 11
\alias{boxplot.stats}
27442 ripley 12
\arguments{
13
  \item{x}{a numeric vector for which the boxplot will
14
    be constructed (\code{\link{NA}}s and \code{\link{NaN}}s are allowed
15
    and omitted).}
42961 ripley 16
  \item{coef}{this determines how far the plot \sQuote{whiskers} extend out
27442 ripley 17
    from the box.  If \code{coef} is positive, the whiskers extend to the
18
    most extreme data point which is no more than \code{coef} times
19
    the length of the box away from the box. A value of zero causes
20
    the whiskers
21
    to extend to the data extremes (and no outliers be returned).}
61168 ripley 22
  \item{do.conf, do.out}{logicals; if \code{FALSE}, the \code{conf} or
27442 ripley 23
    \code{out} component respectively will be empty in the result.}
24
}
25
\description{
34933 murrell 26
  This function is typically called by another function to
27442 ripley 27
  gather the statistics necessary for producing box plots,
28
  but may be invoked separately.
29
}
30
\value{
85232 smeyer 31
  A list with named components as follows:
32
 
27442 ripley 33
  \item{stats}{a vector of length 5, containing the extreme of the
42961 ripley 34
    lower whisker, the lower \sQuote{hinge}, the median, the upper
85232 smeyer 35
    \sQuote{hinge} and the extreme of the upper whisker.
36
    For \code{coef = 0}, this vector is identical to
37
    \code{\link{fivenum}(x, na.rm = TRUE)}.}
27625 ripley 38
  \item{n}{the number of non-\code{NA} observations in the sample.}
42961 ripley 39
  \item{conf}{the lower and upper extremes of the \sQuote{notch}
28474 ripley 40
    (\code{if(do.conf)}). See the details.}
27442 ripley 41
  \item{out}{the values of any data points which lie beyond the
42
    extremes of the whiskers (\code{if(do.out)}).}
43
 
85232 smeyer 44
  Note that \code{stats} and \code{conf} are sorted in \emph{in}creasing
45
  order, unlike S, and that \code{n} and \code{out} include any
27442 ripley 46
  \code{+- Inf} values.
47
}
48
\details{
42961 ripley 49
  The two \sQuote{hinges} are versions of the first and third quartile,
30461 ripley 50
  i.e., close to \code{\link{quantile}(x, c(1,3)/4)}.  The hinges equal
27442 ripley 51
  the quartiles for odd \eqn{n} (where \code{n <- length(x)}) and
45404 ripley 52
  differ for even \eqn{n}.  Whereas the quartiles only equal observations
27442 ripley 53
  for \code{n \%\% 4 == 1} (\eqn{n\equiv 1 \bmod 4}{n = 1 mod 4}),
54
  the hinges do so \emph{additionally} for \code{n \%\% 4 == 2}
55
  (\eqn{n\equiv 2 \bmod 4}{n = 2 mod 4}), and are in the middle of
56
  two observations otherwise.
28474 ripley 57
 
28479 maechler 58
  The notches (if requested) extend to \code{+/-1.58 IQR/sqrt(n)}.
45404 ripley 59
  This seems to be based on the same calculations as the formula with 1.57 in
89404 smeyer 60
  \bibcitet{|R:Chambers+Cleveland+Kleiner:1983|p.\sspace{}62)}, given in
61
  \bibcitet{|R:Mcgill+Tukey+Larsen:1978|p.\sspace{}16)}.
85953 hornik 62
  They are based on asymptotic normality of the median
28474 ripley 63
  and roughly equal sample sizes for the two medians being compared, and
64
  are said to be rather insensitive to the underlying distributions of
65
  the samples.  The idea appears to be to give roughly a 95\% confidence
66
  interval for the difference in two medians.
27442 ripley 67
}
68
\references{
88751 hornik 69
  \bibinfo{R:Tukey:1977}{note}{Section 2C}
88529 hornik 70
 
71
  \bibshow{*,
72
    R:Emerson+Strenio:1983,
73
    R:Tukey:1977,
74
    R:Velleman+Hoaglin:1981}
27442 ripley 75
}
76
\seealso{
74265 hornik 77
  \code{\link{fivenum}},
78
  \code{\link{boxplot}},
79
  \code{\link{bxp}}.
27442 ripley 80
}
81
\examples{
41502 ripley 82
require(stats)
27442 ripley 83
x <- c(1:100, 1000)
27713 ripley 84
(b1 <- boxplot.stats(x))
61153 ripley 85
(b2 <- boxplot.stats(x, do.conf = FALSE, do.out = FALSE))
86
stopifnot(b1 $ stats == b2 $ stats) # do.out = FALSE is still robust
87
boxplot.stats(x, coef = 3, do.conf = FALSE)
85232 smeyer 88
 
27442 ripley 89
## no outlier treatment:
85232 smeyer 90
(b3 <- boxplot.stats(x, coef = 0))
91
stopifnot(b3$stats == fivenum(x))
27442 ripley 92
 
85232 smeyer 93
## missing values are ignored
94
stopifnot(identical(boxplot.stats(c(x, NA)), b1))
95
## ... infinite values are not:
27713 ripley 96
(r <- boxplot.stats(c(x, -1:1/0)))
27442 ripley 97
stopifnot(r$out == c(1000, -Inf, Inf))
98
 
99
%% extended example (for the NG of Rdoc):
100
\dontshow{
101
 ## Difference between quartiles and hinges :
102
 nn <- 1:17 ;  n4 <- nn \%\% 4
103
 hin <- sapply(sapply(nn, seq), function(x) boxplot.stats(x)$stats[c(2,4)])
104
 q13 <- sapply(sapply(nn, seq), quantile, probs = c(1,3)/4, names = FALSE)
105
 m <- t(rbind(q13,hin))[, c(1,3,2,4)]
106
 dimnames(m) <- list(paste(nn), c("q1","lH", "q3","uH"))
61168 ripley 107
 stopifnot(m[n4 == 1, 1:2] == (nn[n4 == 1] + 3)/4,   # quart. = hinge
61153 ripley 108
           m[n4 == 1, 3:4] == (3*nn[n4 == 1] + 1)/4,
27442 ripley 109
           m[,"lH"] == ( (nn+3) \%/\% 2) / 2,
110
           m[,"uH"] == ((3*nn+2)\%/\% 2) / 2)
111
 cm <- noquote(format(m))
112
 cm[m[,2] == m[,1], 2] <- " = "
113
 cm[m[,4] == m[,3], 4] <- " = "
114
 cm
115
}
116
 
117
}
118
\keyword{dplot}