The R Project SVN R

Rev

Rev 68948 | Rev 85953 | Go to most recent revision | 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
74265 hornik 3
% Copyright 1995-2018 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{
31
  List with named components as follows:
32
  \item{stats}{a vector of length 5, containing the extreme of the
42961 ripley 33
    lower whisker, the lower \sQuote{hinge}, the median, the upper
34
    \sQuote{hinge} and the extreme of the upper whisker.}
27625 ripley 35
  \item{n}{the number of non-\code{NA} observations in the sample.}
42961 ripley 36
  \item{conf}{the lower and upper extremes of the \sQuote{notch}
28474 ripley 37
    (\code{if(do.conf)}). See the details.}
27442 ripley 38
  \item{out}{the values of any data points which lie beyond the
39
    extremes of the whiskers (\code{if(do.out)}).}
40
 
41
  Note that \code{$stats} and \code{$conf} are sorted in \emph{in}creasing
42
  order, unlike S, and that \code{$n} and \code{$out} include any
43
  \code{+- Inf} values.
44
}
45
\details{
42961 ripley 46
  The two \sQuote{hinges} are versions of the first and third quartile,
30461 ripley 47
  i.e., close to \code{\link{quantile}(x, c(1,3)/4)}.  The hinges equal
27442 ripley 48
  the quartiles for odd \eqn{n} (where \code{n <- length(x)}) and
45404 ripley 49
  differ for even \eqn{n}.  Whereas the quartiles only equal observations
27442 ripley 50
  for \code{n \%\% 4 == 1} (\eqn{n\equiv 1 \bmod 4}{n = 1 mod 4}),
51
  the hinges do so \emph{additionally} for \code{n \%\% 4 == 2}
52
  (\eqn{n\equiv 2 \bmod 4}{n = 2 mod 4}), and are in the middle of
53
  two observations otherwise.
28474 ripley 54
 
28479 maechler 55
  The notches (if requested) extend to \code{+/-1.58 IQR/sqrt(n)}.
45404 ripley 56
  This seems to be based on the same calculations as the formula with 1.57 in
65208 hornik 57
  Chambers \emph{et al} (1983, p.\sspace{}62), given in McGill \emph{et al}
58
  (1978, p.\sspace{}16).  They are based on asymptotic normality of the median
28474 ripley 59
  and roughly equal sample sizes for the two medians being compared, and
60
  are said to be rather insensitive to the underlying distributions of
61
  the samples.  The idea appears to be to give roughly a 95\% confidence
62
  interval for the difference in two medians.
27442 ripley 63
}
64
\references{
74265 hornik 65
  Tukey, J. W. (1977).
66
  \emph{Exploratory Data Analysis}.
67
  Section 2C.
27442 ripley 68
 
74265 hornik 69
  McGill, R., Tukey, J. W. and Larsen, W. A. (1978).
70
  Variations of box plots.
71
  \emph{The American Statistician}, \bold{32}, 12--16.
72
  \doi{10.2307/2683468}.
28479 maechler 73
 
74265 hornik 74
  Velleman, P. F. and Hoaglin, D. C. (1981).
75
  \emph{Applications, Basics and Computing of Exploratory Data Analysis}.
76
  Duxbury Press.
28479 maechler 77
 
74265 hornik 78
  Emerson, J. D and Strenio, J. (1983).
79
  Boxplots and batch comparison.
27442 ripley 80
  Chapter 3 of \emph{Understanding Robust and Exploratory Data
81
    Analysis}, eds. D. C. Hoaglin, F. Mosteller and J. W. Tukey.  Wiley.
82
 
74265 hornik 83
  Chambers, J. M., Cleveland, W. S., Kleiner, B. and Tukey, P. A. (1983).
84
  \emph{Graphical Methods for Data Analysis}.
85
  Wadsworth & Brooks/Cole.
27442 ripley 86
}
87
\seealso{
74265 hornik 88
  \code{\link{fivenum}},
89
  \code{\link{boxplot}},
90
  \code{\link{bxp}}.
27442 ripley 91
}
92
\examples{
41502 ripley 93
require(stats)
27442 ripley 94
x <- c(1:100, 1000)
27713 ripley 95
(b1 <- boxplot.stats(x))
61153 ripley 96
(b2 <- boxplot.stats(x, do.conf = FALSE, do.out = FALSE))
97
stopifnot(b1 $ stats == b2 $ stats) # do.out = FALSE is still robust
98
boxplot.stats(x, coef = 3, do.conf = FALSE)
27442 ripley 99
## no outlier treatment:
27713 ripley 100
boxplot.stats(x, coef = 0)
27442 ripley 101
 
27713 ripley 102
boxplot.stats(c(x, NA)) # slight change : n is 101
103
(r <- boxplot.stats(c(x, -1:1/0)))
27442 ripley 104
stopifnot(r$out == c(1000, -Inf, Inf))
105
 
106
%% extended example (for the NG of Rdoc):
107
\dontshow{
108
 ## Difference between quartiles and hinges :
109
 nn <- 1:17 ;  n4 <- nn \%\% 4
110
 hin <- sapply(sapply(nn, seq), function(x) boxplot.stats(x)$stats[c(2,4)])
111
 q13 <- sapply(sapply(nn, seq), quantile, probs = c(1,3)/4, names = FALSE)
112
 m <- t(rbind(q13,hin))[, c(1,3,2,4)]
113
 dimnames(m) <- list(paste(nn), c("q1","lH", "q3","uH"))
61168 ripley 114
 stopifnot(m[n4 == 1, 1:2] == (nn[n4 == 1] + 3)/4,   # quart. = hinge
61153 ripley 115
           m[n4 == 1, 3:4] == (3*nn[n4 == 1] + 1)/4,
27442 ripley 116
           m[,"lH"] == ( (nn+3) \%/\% 2) / 2,
117
           m[,"uH"] == ((3*nn+2)\%/\% 2) / 2)
118
 cm <- noquote(format(m))
119
 cm[m[,2] == m[,1], 2] <- " = "
120
 cm[m[,4] == m[,3], 4] <- " = "
121
 cm
122
}
123
 
124
}
125
\keyword{dplot}