Last chance! 50% off unlimited learning
Sale ends in
The generic function quantile
produces sample quantiles
corresponding to the given probabilities.
The smallest observation corresponds to a probability of 0 and the
largest to a probability of 1.
quantile(x, …)# S3 method for default
quantile(x, probs = seq(0, 1, 0.25), na.rm = FALSE,
names = TRUE, type = 7, …)
numeric vector whose sample quantiles are wanted, or an
object of a class for which a method has been defined (see also
‘details’). NA
and NaN
values are not
allowed in numeric vectors unless na.rm
is TRUE
.
numeric vector of probabilities with values in
logical; if true, any NA
and NaN
's
are removed from x
before the quantiles are computed.
logical; if true, the result has a names
attribute. Set to FALSE
for speedup with many probs
.
an integer between 1 and 9 selecting one of the nine quantile algorithms detailed below to be used.
further arguments passed to or from other methods.
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.
All sample quantiles are defined as weighted averages of
consecutive order statistics. Sample quantiles of type
Discontinuous sample quantile types 1, 2, and 3
For types 1, 2 and 3,
Inverse of empirical distribution function.
Similar to type 1 but with averaging at discontinuities.
SAS definition: nearest even order statistic.
Continuous sample quantile types 4 through 9
For types 4 through 9,
x
.
x
is normally distributed.
Further details are provided in Hyndman and Fan (1996) who recommended type 8. The default method is type 7, as used by S and by R < 2.0.0.
A vector of length length(probs)
is returned;
if names = TRUE
, it has a names
attribute.
NA
and NaN
values in probs
are
propagated to the result.
The default method works with classed objects sufficiently like
numeric vectors that sort
and (not needed by types 1 and 3)
addition of elements and multiplication by a number work correctly.
Note that as this is in a namespace, the copy of sort
in
base will be used, not some S4 generic of that name. Also note
that that is no check on the ‘correctly’, and so
e.g.quantile
can be applied to complex vectors which (apart
from ties) will be ordered on their real parts.
There is a method for the date-time classes (see
"POSIXt"
). Types 1 and 3 can be used for class
"Date"
and for ordered factors.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Hyndman, R. J. and Fan, Y. (1996) Sample quantiles in statistical packages, American Statistician 50, 361--365. 10.2307/2684934.
ecdf
for empirical distributions of which
quantile
is an inverse;
boxplot.stats
and fivenum
for computing
other versions of quartiles, etc.
# NOT RUN {
quantile(x <- rnorm(1001)) # Extremes & Quartiles by default
quantile(x, probs = c(0.1, 0.5, 1, 2, 5, 10, 50, NA)/100)
### Compare different types
quantAll <- function(x, prob, ...)
t(vapply(1:9, function(typ) quantile(x, prob=prob, type = typ, ...), quantile(x, prob, type=1)))
p <- c(0.1, 0.5, 1, 2, 5, 10, 50)/100
signif(quantAll(x, p), 4)
## for complex numbers:
z <- complex(re=x, im = -10*x)
signif(quantAll(z, p), 4)
# }
Run the code above in your browser using DataLab