The R Project SVN R

Rev

Rev 61153 | Rev 65208 | 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
2
% Part of the R package, http://www.R-project.org
59039 ripley 3
% Copyright 1995-2007 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
28484 ripley 57
  Chambers \emph{et al.} (1983, p. 62), given in McGill \emph{et al.}
28474 ripley 58
  (1978, p. 16).  They are based on asymptotic normality of the median
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{
65
  Tukey, J. W. (1977) \emph{Exploratory Data Analysis.} Section 2C.
66
 
67
  McGill, R., Tukey, J. W. and Larsen, W. A. (1978) Variations of box
68
  plots. \emph{The American Statistician} \bold{32}, 12--16.
28479 maechler 69
 
27442 ripley 70
  Velleman, P. F. and Hoaglin, D. C. (1981) \emph{Applications, Basics
71
    and Computing of Exploratory Data Analysis.}  Duxbury Press.
28479 maechler 72
 
27442 ripley 73
  Emerson, J. D and Strenio, J. (1983). Boxplots and batch comparison.
74
  Chapter 3 of \emph{Understanding Robust and Exploratory Data
75
    Analysis}, eds. D. C. Hoaglin, F. Mosteller and J. W. Tukey.  Wiley.
76
 
28484 ripley 77
  Chambers, J. M., Cleveland, W. S., Kleiner, B. and Tukey, P. A. (1983)
47262 ripley 78
  \emph{Graphical Methods for Data Analysis.}  Wadsworth & Brooks/Cole.
27442 ripley 79
}
80
\seealso{
30461 ripley 81
  \code{\link{fivenum}}, \code{\link{boxplot}}, \code{\link{bxp}}.
27442 ripley 82
}
83
\examples{
41502 ripley 84
require(stats)
27442 ripley 85
x <- c(1:100, 1000)
27713 ripley 86
(b1 <- boxplot.stats(x))
61153 ripley 87
(b2 <- boxplot.stats(x, do.conf = FALSE, do.out = FALSE))
88
stopifnot(b1 $ stats == b2 $ stats) # do.out = FALSE is still robust
89
boxplot.stats(x, coef = 3, do.conf = FALSE)
27442 ripley 90
## no outlier treatment:
27713 ripley 91
boxplot.stats(x, coef = 0)
27442 ripley 92
 
27713 ripley 93
boxplot.stats(c(x, NA)) # slight change : n is 101
94
(r <- boxplot.stats(c(x, -1:1/0)))
27442 ripley 95
stopifnot(r$out == c(1000, -Inf, Inf))
96
 
97
%% extended example (for the NG of Rdoc):
98
\dontshow{
99
 ## Difference between quartiles and hinges :
100
 nn <- 1:17 ;  n4 <- nn \%\% 4
101
 hin <- sapply(sapply(nn, seq), function(x) boxplot.stats(x)$stats[c(2,4)])
102
 q13 <- sapply(sapply(nn, seq), quantile, probs = c(1,3)/4, names = FALSE)
103
 m <- t(rbind(q13,hin))[, c(1,3,2,4)]
104
 dimnames(m) <- list(paste(nn), c("q1","lH", "q3","uH"))
61168 ripley 105
 stopifnot(m[n4 == 1, 1:2] == (nn[n4 == 1] + 3)/4,   # quart. = hinge
61153 ripley 106
           m[n4 == 1, 3:4] == (3*nn[n4 == 1] + 1)/4,
27442 ripley 107
           m[,"lH"] == ( (nn+3) \%/\% 2) / 2,
108
           m[,"uH"] == ((3*nn+2)\%/\% 2) / 2)
109
 cm <- noquote(format(m))
110
 cm[m[,2] == m[,1], 2] <- " = "
111
 cm[m[,4] == m[,3], 4] <- " = "
112
 cm
113
}
114
 
115
}
116
\keyword{dplot}