densityPlot {car}  R Documentation 
densityPlot
contructs and graphs nonparametric density estimates, possibly conditioned on a factor, using the standard R density
function or by default adaptiveKernel
, which computes an adaptive kernel density estimate.
depan
provides the Epanechnikov kernel and dbiwt
provides the biweight kernel.
densityPlot(x, ...) ## Default S3 method: densityPlot(x, g, method=c("adaptive", "kernel"), bw=if (method == "adaptive") bw.nrd0 else "SJ", adjust=1, kernel, xlim, ylim, normalize=FALSE, xlab=deparse(substitute(x)), ylab="Density", main="", col=carPalette(), lty=seq_along(col), lwd=2, grid=TRUE, legend=TRUE, show.bw=FALSE, rug=TRUE, ...) ## S3 method for class 'formula' densityPlot(formula, data=NULL, subset, na.action=NULL, xlab, ylab, main="", legend=TRUE, ...) adaptiveKernel(x, kernel=dnorm, bw=bw.nrd0, adjust=1.0, n=500, from, to, cut=3, na.rm=TRUE) depan(x) dbiwt(x)
x 
a numeric variable, the density of which is estimated; for

g 
an optional factor to divide the data. 
formula 
an R model formula, of the form 
data 
an optional data frame containing the data. 
subset 
an optional vector defining a subset of the data. 
na.action 
a function to handle missing values; defaults to the value of the R 
method 
either 
bw 
the geometric mean bandwidth for the adaptivekernel or bandwidth of the kernel density estimate(s). Must be a numerical value
or a function to compute the bandwidth (default 
adjust 
a multiplicative adjustment factor for the bandwidth; the default, 
kernel 
for 
xlim, ylim 
axis limits; if missing, determined from the range of xvalues at which the densities are estimated and the estimated densities. 
normalize 
if 
xlab 
label for the horizontalaxis; defaults to the name of the variable 
ylab 
label for the vertical axis; defaults to 
main 
plot title; default is empty. 
col 
vector of colors for the density estimate(s); defaults to the color 
lty 
vector of line types for the density estimate(s); defaults to the successive integers, starting at 1. 
lwd 
line width for the density estimate(s); defaults to 2. 
grid 
if 
legend 
a list of up to two named elements: 
n 
number of equally spaced points at which the adaptivekernel estimator is evaluated; the default is 
from, to, cut 
the range over which the density estimate is computed; the default, if missing, is 
na.rm 
remove missing values from 
show.bw 
if 
rug 
if 
... 
arguments to be passed down to graphics functions. 
If you use a different kernel function than the default dnorm
that has a
standard deviation different from 1 along with an automatic rule
like the default function bw.nrd0
, you can attach an attribute to the kernel
function named "scale"
that gives its standard deviation. This is true for
the two supplied kernels, depan
and dbiwt
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)
.
John Fox jfox@mcmaster.ca
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
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, method="kernel", data=Prestige) densityPlot(~ income, show.bw=TRUE, data=Prestige) densityPlot(~ income, from=0, normalize=TRUE, show.bw=TRUE, data=Prestige) densityPlot(income ~ type, data=Prestige) densityPlot(~ income, show.bw=TRUE, method="kernel", data=Prestige) densityPlot(~ income, show.bw=TRUE, data=Prestige) densityPlot(~ income, from=0, normalize=TRUE, show.bw=TRUE, data=Prestige) densityPlot(income ~ type, kernel=depan, data=Prestige) densityPlot(income ~ type, kernel=depan, legend=list(location="top"), data=Prestige) plot(adaptiveKernel(UN$infantMortality, from=0, adjust=0.75), col="magenta") lines(density(na.omit(UN$infantMortality), from=0, adjust=0.75), col="blue") rug(UN$infantMortality, col="cyan") legend("topright", col=c("magenta", "blue"), lty=1, legend=c("adaptive kernel", "kernel"), inset=0.02) plot(adaptiveKernel(UN$infantMortality, from=0, adjust=0.75), col="magenta") lines(density(na.omit(UN$infantMortality), from=0, adjust=0.75), col="blue") rug(UN$infantMortality, col="cyan") legend("topright", col=c("magenta", "blue"), lty=1, legend=c("adaptive kernel", "kernel"), inset=0.02)