histo: Histogram with Added Options

Description Usage Arguments Details Value Examples

View source: R/histo.R

Description

Similar to base R function hist, but with two added features: (1) Can overlay a fitted probability density/mass function (PDF/PMF) for any univariate distribution supported in R (see Distributions); and (2) Can generate more of a barplot type histogram, where each possible value gets its own bin centered over its value (useful for discrete variables with not too many possible values).

Usage

1
2
histo(x, dis = "none", dis.shift = NULL, integer.breaks = NULL,
  points.list = NULL, axis.list = NULL, ...)

Arguments

x

Numeric vector of values.

dis

Character vector indicating which distribution should be used to add fitted PDF/PMF to the histogram. Possible values are "none", "beta", "binom" (must specify size), "cauchy", "chisq", "exp", "f", "gamma", "geom", "hyper" (must specify total number of balls in urn, N, and number of balls drawn each time, k), "lnorm", "nbinom" (must specify size), "norm", "pois", "t", "unif", and "weibull".

dis.shift

Numeric value for shifting the fitted PDF/PMF along the x-axis of the histogram.

integer.breaks

If TRUE, integers covering the range of x are used for breaks, so there is one bin for each integer. Useful for discrete distributions that don't take on too many unique values.

points.list

Optional list of inputs to pass to points function, which is used to add the fitted PDF/PMF.

axis.list

Optional list of inputs to pass to axis function.

...

May include arguments to pass to hist and/or parameter values needed for certain distributions (size if dis = "binom" or dis = "nbinom", N and k if dis = "hyper").

Details

When x takes on whole numbers, you typically want to set dis.shift = -0.5 if right = TRUE (hist's default) and dis.shift = 0.5 if right = FALSE. The function will do this internally by default.

To illustrate, suppose a particular bin represents (7, 10]. Its midpoint will be at x = 8.5 on the graph. But if input values are whole numbers, this bin really only includes values of 8, 9, and 10, which have a mean of 9. So you really want f(9) to appear at x = 8.5. This requires shifting the curve to the left 0.5 units, i.e. setting dis.shift = -0.5.

When x takes on whole numbers with not too many unique values, you may want the histogram to show one bin for each integer. You can do this by setting integer.breaks = TRUE. By default, the function sets integer.breaks = TRUE if x contains whole numbers with 10 or fewer unique values.

Value

Histogram with fitted PDF/PMF if requested.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Generate 10,000 Poisson(2) values. Compare default histograms from hist vs. 
# histo.
set.seed(123)
x <- rpois(n = 10000, lambda = 2)
par(mfrow = c(1, 2))
hist(x)
histo(x)

# Generate 10,000 lognormal(0, 0.35) values. Create histograms with curves
# showing fitted log-normal and normal PDFs.
set.seed(123)
x <- rlnorm(n = 10000, meanlog = 0, sdlog = 0.35)
par(mfrow = c(1, 2))
histo(x, "lnorm", main = "Log-normal curve")
histo(x, "norm", main = "Normal curve")

# Generate 10,000 Binomial(8, 0.25) values. Create histogram, specifying 
# size = 5, with blue line/points showing fitted PMF.
set.seed(123)
x <- rbinom(n = 10000, size = 5, prob = 0.25)
par(mfrow = c(1, 1))
histo(x, "binom", size = 5, points.list = list(type = "b", col = "blue"))

dvmisc documentation built on May 2, 2019, 5:51 p.m.