density {base} | R Documentation |
Kernel Density Estimation
Description
The function density
computes kernel density estimates
with the given kernel and bandwidth.
The generic functions plot
and print
have
methods for density objects.
Usage
density(x, bw, adjust = 1, kernel="gaussian", window = kernel,
n = 512, width, from, to, cut = 3, na.rm = FALSE)
print(dobj)
plot(dobj, main = NULL, xlab = NULL, ylab = "Density", type = "l",
zero.line = TRUE, ...)
Arguments
x |
the data from which the estimate is to be computed. |
n |
the number of equally spaced points at which the density
is to be estimated. When |
kernel , window |
a character string giving the smoothing kernel to be used.
This must be one of |
bw |
the smoothing bandwith to be used. This is the standard
deviation of the smoothing kernel. It defaults to 0.9 times the
minimum of the standard deviation and the interquartile range divided by
1.34 times the sample size to the negative one-fifth power
(= Silverman's “rule of thumb”).
The specified value of |
adjust |
the bandwith used is actually |
width |
this exists for compatibility with S. |
from , to |
the left and right-most points of the grid at which the density is to be estimated. |
cut |
by default, the values of |
na.rm |
logical; if |
dobj |
a “density” object. |
main , xlab , ylab , type |
plotting parameters with useful defaults. |
... |
further plotting parameters. |
zero.line |
logical; if |
Details
The algorithm used in density
disperses the mass of the
empirical distribution function over a regular grid of at least 512
points and then
uses the fast Fourier transform to convolve this approximation
with a discretized version of the kernel and then uses linear
approximation to evaluate the density at the specified points.
Infinite values in x
are assumed to correspond to a point mass at
+/-Inf
and the density estimate is of the sub-density on
(-Inf, +Inf)
.
Value
An object with class "density"
.
The underlying structure is a list containing the following components.
x |
the |
y |
the estimated density values. |
bw |
the bandwidth used. |
N |
the sample size after elimination of missing values. |
call |
the call which produced the result. |
data.name |
the deparsed name of the |
has.na |
logical, for compatibility (always FALSE). |
References
Silverman, B. W. (1986). Density Estimation. London: Chapman and Hall.
Venables, W. N. and B. D. Ripley (1994,7,9). Modern Applied Statistics with S-PLUS. New York: Springer.
Scott, D. W. (1992). Multivariate Density Estimation. Theory, Practice and Visualization. New York: Wiley.
Sheather, S. J. and M. C. Jones (1991). “A reliable data-based bandwidth selection method for kernel density estimation. J. Roy. Statist. Soc. B, 683-690.
See Also
convolve
, hist
.
Examples
# The Old Faithful geyser data
data(faithful)
d <- density(faithful$eruptions, bw=0.15)
d
plot(d)
plot(d, type="n")
polygon(d, col="wheat")
## Missing values:
x <- xx <- faithful$eruptions
x[i.out <- sample(length(x), 10)] <- NA
doR <- density(x, bw=0.15, na.rm = TRUE)
lines(doR, col="blue")
points(xx[i.out], rep(.01,10))