1% File src/library/grDevices/man/nclass.Rd 2% Part of the R package, https://www.R-project.org 3% Copyright 1995-2018 R Core Team 4% Distributed under GPL 2 or later 5 6\name{nclass} 7\alias{nclass.Sturges} 8\alias{nclass.scott} 9\alias{nclass.FD} 10\encoding{UTF-8} 11\title{Compute the Number of Classes for a Histogram} 12\description{ 13 Compute the number of classes for a histogram. 14} 15\usage{ 16nclass.Sturges(x) 17nclass.scott(x) 18nclass.FD(x) 19} 20\arguments{ 21 \item{x}{a data vector.} 22} 23\value{ 24 The suggested number of classes. 25} 26\details{ 27 \code{nclass.Sturges} uses Sturges' formula, implicitly basing bin 28 sizes on the range of the data. 29 30 \code{nclass.scott} uses Scott's choice for a normal distribution based on 31 the estimate of the standard error, unless that is zero where it 32 returns \code{1}. 33 34 \code{nclass.FD} uses the Freedman-Diaconis choice based on the 35 inter-quartile range (\code{\link{IQR}(signif(x, 5))}) unless that's 36 zero where it uses increasingly more extreme symmetric quantiles up to 37 c(1,511)/512 and if that difference is still zero, reverts to using 38 Scott's choice. 39} 40\references{ 41 Venables, W. N. and Ripley, B. D. (2002) 42 \emph{Modern Applied Statistics with S-PLUS.} 43 Springer, page 112. 44 45 Freedman, D. and Diaconis, P. (1981). 46 On the histogram as a density estimator: \eqn{L_2} theory. 47 \emph{Zeitschrift \enc{für}{fuer} Wahrscheinlichkeitstheorie 48 und verwandte Gebiete}, \bold{57}, 453--476. 49 \doi{10.1007/BF01025868}. 50 51 Scott, D. W. (1979). 52 On optimal and data-based histograms. 53 \emph{Biometrika}, \bold{66}, 605--610. 54 \doi{10.2307/2335182}. 55 56 Scott, D. W. (1992) 57 \emph{Multivariate Density Estimation. Theory, Practice, and 58 Visualization}. Wiley. 59 60 Sturges, H. A. (1926). 61 The choice of a class interval. 62 \emph{Journal of the American Statistical Association}, \bold{21}, 63 65--66. 64 \doi{10.1080/01621459.1926.10502161}. 65 66} 67\seealso{ 68 \code{\link{hist}} and \code{\link[MASS]{truehist}} (package 69 \CRANpkg{MASS}); \code{\link[KernSmooth]{dpih}} (package 70 \CRANpkg{KernSmooth}) for a plugin bandwidth proposed by Wand(1995). 71} 72\examples{ 73set.seed(1) 74x <- stats::rnorm(1111) 75nclass.Sturges(x) 76 77## Compare them: 78NC <- function(x) c(Sturges = nclass.Sturges(x), 79 Scott = nclass.scott(x), FD = nclass.FD(x)) 80NC(x) 81onePt <- rep(1, 11) 82NC(onePt) # no longer gives NaN 83} 84\keyword{univar} 85