The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
27442 ripley 1
\name{boxplot.stats}
2
\title{Box Plot Statistics}
3
\usage{
4
boxplot.stats(x, coef = 1.5, do.conf=TRUE, do.out=TRUE)
5
}
6
\alias{boxplot.stats}
7
\arguments{
8
  \item{x}{a numeric vector for which the boxplot will
9
    be constructed (\code{\link{NA}}s and \code{\link{NaN}}s are allowed
10
    and omitted).}
11
  \item{coef}{this determines how far the plot \dQuote{whiskers} extend out
12
    from the box.  If \code{coef} is positive, the whiskers extend to the
13
    most extreme data point which is no more than \code{coef} times
14
    the length of the box away from the box. A value of zero causes
15
    the whiskers
16
    to extend to the data extremes (and no outliers be returned).}
17
  \item{do.conf,do.out}{logicals; if \code{FALSE}, the \code{conf} or
18
    \code{out} component respectively will be empty in the result.}
19
}
20
\description{
21
  This function is typically called by \code{\link{boxplot}} to
22
  gather the statistics necessary for producing box plots,
23
  but may be invoked separately.
24
}
25
\value{
26
  List with named components as follows:
27
  \item{stats}{a vector of length 5, containing the extreme of the
28
    lower whisker, the lower \dQuote{hinge}, the median, the upper \dQuote{hinge}
29
    and the extreme of the upper whisker.}
27625 ripley 30
  \item{n}{the number of non-\code{NA} observations in the sample.}
27442 ripley 31
  \item{conf}{the lower and upper extremes of the \dQuote{notch} (\code{if(do.conf)}).}
32
  \item{out}{the values of any data points which lie beyond the
33
    extremes of the whiskers (\code{if(do.out)}).}
34
 
35
  Note that \code{$stats} and \code{$conf} are sorted in \emph{in}creasing
36
  order, unlike S, and that \code{$n} and \code{$out} include any
37
  \code{+- Inf} values.
38
}
39
\details{
40
  The two \dQuote{hinges} are versions of the first and third quartile,
27447 ripley 41
  i.e., close to \code{\link[stats]{quantile}(x, c(1,3)/4)}.  The hinges equal
27442 ripley 42
  the quartiles for odd \eqn{n} (where \code{n <- length(x)}) and
43
  differ for even \eqn{n}. Where the quartiles only equal observations
44
  for \code{n \%\% 4 == 1} (\eqn{n\equiv 1 \bmod 4}{n = 1 mod 4}),
45
  the hinges do so \emph{additionally} for \code{n \%\% 4 == 2}
46
  (\eqn{n\equiv 2 \bmod 4}{n = 2 mod 4}), and are in the middle of
47
  two observations otherwise.
48
}
49
\references{
50
  Tukey, J. W. (1977) \emph{Exploratory Data Analysis.} Section 2C.
51
 
52
  McGill, R., Tukey, J. W. and Larsen, W. A. (1978) Variations of box
53
  plots. \emph{The American Statistician} \bold{32}, 12--16.
54
 
55
  Velleman, P. F. and Hoaglin, D. C. (1981) \emph{Applications, Basics
56
    and Computing of Exploratory Data Analysis.}  Duxbury Press.
57
 
58
  Emerson, J. D and Strenio, J. (1983). Boxplots and batch comparison.
59
  Chapter 3 of \emph{Understanding Robust and Exploratory Data
60
    Analysis}, eds. D. C. Hoaglin, F. Mosteller and J. W. Tukey.  Wiley.
61
 
62
}
63
\seealso{
27447 ripley 64
  \code{\link[stats]{fivenum}}, \code{\link{boxplot}}, \code{\link{bxp}}.
27442 ripley 65
}
66
\examples{
67
x <- c(1:100, 1000)
27713 ripley 68
(b1 <- boxplot.stats(x))
69
(b2 <- boxplot.stats(x, do.conf=FALSE, do.out=FALSE))
27442 ripley 70
stopifnot(b1 $ stats == b2 $ stats) # do.out=F is still robust
27713 ripley 71
boxplot.stats(x, coef = 3, do.conf=FALSE)
27442 ripley 72
## no outlier treatment:
27713 ripley 73
boxplot.stats(x, coef = 0)
27442 ripley 74
 
27713 ripley 75
boxplot.stats(c(x, NA)) # slight change : n is 101
76
(r <- boxplot.stats(c(x, -1:1/0)))
27442 ripley 77
stopifnot(r$out == c(1000, -Inf, Inf))
78
 
79
%% extended example (for the NG of Rdoc):
80
\dontshow{
81
 ## Difference between quartiles and hinges :
82
 nn <- 1:17 ;  n4 <- nn \%\% 4
83
 hin <- sapply(sapply(nn, seq), function(x) boxplot.stats(x)$stats[c(2,4)])
84
 q13 <- sapply(sapply(nn, seq), quantile, probs = c(1,3)/4, names = FALSE)
85
 m <- t(rbind(q13,hin))[, c(1,3,2,4)]
86
 dimnames(m) <- list(paste(nn), c("q1","lH", "q3","uH"))
87
 stopifnot(m[n4==1, 1:2] == (nn[n4==1] + 3)/4,# quart. = hinge
88
           m[n4==1, 3:4] == (3*nn[n4==1]+1)/4,
89
           m[,"lH"] == ( (nn+3) \%/\% 2) / 2,
90
           m[,"uH"] == ((3*nn+2)\%/\% 2) / 2)
91
 cm <- noquote(format(m))
92
 cm[m[,2] == m[,1], 2] <- " = "
93
 cm[m[,4] == m[,3], 4] <- " = "
94
 cm
95
}
96
 
97
}
98
\keyword{dplot}