n.bins {car}R Documentation

Number of Bins for Histogram

Description

Several rules for calculating the number of bins to use for a histogram.

Usage

n.bins(x, rule=c("freedman.diaconis", "sturges", "scott", "simple"))

Arguments

x numeric vector, variable for histogram
rule see below.

Details

"freedman.diaconis":
(n^1/3 * range)/(2 * IQR).
"sturges":
ceiling(log2n + 1).
"scott":
ceiling[n^1/3 * range/(3.5 * s)].
"simple":
floor[10 * log10(n)] for n > 100, or floor[2/sqrt(n)] for n <= 100.

where n is the number of observations, range is the range of x, IQR is the inter-quartile range of x, and s is the standard deviation of x.

Value

the number of bins.

Author(s)

John Fox jfox@mcmaster.ca

References

Freedman, D. and Diaconis, P. (1981) On the histogram as a density estimator. Zeitschrift fur Wahrscheinlichkeitstheorie und verwandte Gebiete 57, 453–476.

Scott, D. W. (1979) On optimal and data based-histograms. Biometrika 66, 605–610.

Venables, W. N. and Ripley, B. D. (1999) Modern Applied Statistics with S-PLUS, Third Edition, Springer.

See Also

hist

Examples

attach(UN)
n.bins(gdp)
## [1] 19
n.bins(gdp, "sturges")
## [1] 9
n.bins(gdp, "scott")
## [1] 8
n.bins(gdp, "simple")
## [1] 22
hist(gdp)
hist(gdp, nclass=n.bins(gdp))

[Package car version 1.1-0 Index]