densityPlot
contructs and graphs nonparametric density estimates, possibly conditioned on a factor.
By default it uses the standard R density
function or optionally adaptiveKernel
.
densityPlot(x, ...)# S3 method for default
densityPlot(x, g, method=c("kernel", "adaptive"),
bw=if (method == "adaptive") bw.nrd0 else "SJ", adjust=1,
kernel, xlim, ylim, normalize=FALSE,
xlab=deparse(substitute(x)), ylab="Density",
col=palette(), lty=seq_along(col), lwd=2, grid=TRUE,
legend.location="topright", legend.title=deparse(substitute(g)), show.bw=FALSE,
rug=TRUE, ...)
# S3 method for formula
densityPlot(formula, data = NULL, subset,
na.action = NULL, xlab, ylab, ...)
adaptiveKernel(x, kernel=dnorm, bw=bw.nrd0, adjust=1.0, n=500,
from, to, cut=3, na.rm=TRUE)
a numeric variable, the density of which is estimated.
an optional factor to divide the data.
an R model formula, of the form ~ variable
to estimate the unconditional
density of variable
, or variable ~ factor
to estimate the density of variable
within each level of factor
.
an optional data frame containing the data.
an optional vector defining a subset of the data.
a function to handle missing values; defaults to the value of the R na.action
option,
initially set to na.omit
.
either "adaptive"
(the default) for an adaptive-kernel estimate or "kernel"
for a fixed-bandwidth kernel estimate.
the geometric mean bandwidth for the adaptive-kernel or bandwidth of the kernel density estimate(s). Must be a numerical value
or a function to compute the bandwidth (default bw.nrd0
) for the adaptive
kernel estimate; for the kernel estimate, may either the quoted name of a rule to
compute the bandwidth, or a numeric value. If plotting by groups, bw
may be a vector of values, one for each group. See density
and bw.SJ
for details of the kernel estimator.
a multiplicative adjustment factor for the bandwidth; the default, 1
, indicates no adjustment;
if plotting by groups, adjust
may be a vector of adjustment factors, one for each group. The default bandwidth-selection rule tends to give a value that's too large if
the distribution is asymmetric or has multiple modes; try setting adjust
< 1, particularly for the adaptive-kernel estimator.
for densityPlot
this is the name of the kernel function for the kernel estimator (the default is "gaussian"
, see density
);
or a kernel function for the adaptive-kernel estimator (the default is dnorm
, producing the Gaussian kernel).
For adaptivekernel
this is a kernel function, defaulting to dnorm
, which is the Gaussian kernel (standard-normal density).
axis limits; if missing, determined from the range of x-values at which the densities are estimated and the estimated densities.
if TRUE
(the default is FALSE
), the estimated densities are rescaled to integrate approximately to 1; particularly useful if the
density is estimated over a restricted domain, as when from
or to
are specified.
label for the horizontal-axis; defaults to the name of the variable x
.
label for the vertical axis; defaults to "Density"
.
vector of colors for the density estimate(s); defaults to the color palette
.
vector of line types for the density estimate(s); defaults to the successive integers, starting at 1.
line width for the density estimate(s); defaults to 2.
if TRUE
(the default), grid lines are drawn on the plot.
location for the legend when densities are plotted for several groups; defaults to
"upperright"
; see legend
.
label for the legend, which is drawn if densities are plotted by groups; the default is the name of the factor g.
number of equally spaced points at which the adaptive-kernel estimator is evaluated; the default is 500
.
the range over which the density estimate is computed; the default, if missing, is min(x) - cut*bw, max(x) + cut*bw
.
remove missing values from x
in computing the adaptive-kernel estimate? The default is TRUE
.
if TRUE
, show the bandwidth(s) in the horizontal-axis label or (for multiple groups)
the legend; the default is FALSE
.
if TRUE
(the default), draw a rug plot (one-dimentional scatterplot) at the bottom of the density estimate.
arguments to be passed down.
densityPlot
invisibly returns the "density"
object computed (or list of "density"
objects) and draws a graph.
adaptiveKernel
returns an object of class "density"
(see density)
.
W. N. Venables and B. D. Ripley (2002) Modern Applied Statistics with S. New York: Springer.
B.W. Silverman (1986) Density Estimation for Statistics and Data Analysis. London: Chapman and Hall.
densityPlot(~ income, show.bw=TRUE, data=Prestige)
densityPlot(~ income, method="adaptive", show.bw=TRUE, data=Prestige)
densityPlot(~ income, method="adaptive", from=0, normalize=TRUE, show.bw=TRUE, data=Prestige)
densityPlot(income ~ type, method="adaptive", data=Prestige)
plot(adaptiveKernel(UN$infant.mortality, from=0, adjust=0.75), col="magenta")
lines(density(na.omit(UN$infant.mortality), from=0, adjust=0.75), col="blue")
rug(UN$infant.mortality, col="cyan")
legend("topright", col=c("magenta", "blue"), lty=1,
legend=c("adaptive kernel", "kernel"), inset=0.02)
Run the code above in your browser using DataLab