nclass {grDevices} | R Documentation |
Compute the Number of Classes for a Histogram
Description
Compute the number of classes for a histogram, notably hist()
.
Usage
nclass.Sturges(x)
nclass.scott(x)
nclass.FD(x, digits = 5)
Arguments
x |
a data vector. |
digits |
number of significant digits to keep when rounding
|
Details
nclass.Sturges
uses Sturges' formula, implicitly basing bin
sizes on the range of the data.
nclass.scott
uses Scott's choice for a normal distribution based on
the estimate of the standard error, unless that is zero where it
returns 1
.
nclass.FD
uses the Freedman-Diaconis choice based on the
inter-quartile range (IQR(signif(x, digits))
) unless that's
zero where it uses increasingly more extreme symmetric quantiles up to
c(1,511)/512 and if that difference is still zero, reverts to using
Scott's choice. The default of digits = 5
was chosen after a few
experiments, but may be too low for some situations, see PR#17274.
Value
The suggested number of classes.
References
.
Freedman D, Diaconis P (1981).
“On the Histogram as a Density Estimator: L_2
theory.”
Zeitschrift für Wahrscheinlichkeitstheorie und Verwandte Gebiete, 57(4), 453–476.
doi:10.1007/BF01025868.
Scott DW (1979). “On Optimal and Data-based Histograms.” Biometrika, 66(3), 605–610. doi:10.1093/biomet/66.3.605.
Scott DW (1992). Multivariate Density Estimation. Theory, Practice and Visualization. Wiley, New York.
Sturges HA (1926). “The Choice of a Class Interval.” Journal of the American Statistical Association, 21(153), 65–66. doi:10.1080/01621459.1926.10502161.
Venables WN, Ripley BD (2002). Modern Applied Statistics with S, series Statistics and Computing. Springer, New York, NY. doi:10.1007/978-0-387-21706-2. Page 112.
See Also
hist
and truehist
(package
MASS); dpih
(package
KernSmooth) for a plugin bandwidth proposed by Wand(1995).
Examples
set.seed(1)
x <- stats::rnorm(1111)
nclass.Sturges(x)
## Compare them:
NC <- function(x) c(Sturges = nclass.Sturges(x),
Scott = nclass.scott(x), FD = nclass.FD(x))
NC(x)
onePt <- rep(1, 11)
NC(onePt) # no longer gives NaN