the smoothing bandwidth to be used. The kernels are scaled such that this is the standard deviation of the smoothing kernel. (Note this differs from the reference books cited below.)
bw can also be a character string giving a rule to choose the bandwidth. See bw.nrd. The default, “nrd0”, has remained the default for historical and compatibility reasons, rather than as a general recommendation, where e.g., “SJ” would rather fit, see also Venables and Ripley (2002).
The specified (or computed) value of bw is multiplied by adjust.
adjust
the bandwidth used is actually adjust*bw. This makes it easy to specify values like ‘half the default’ bandwidth.
kernel
a character string giving the smoothing kernel to be used. This must partially match one of “gaussian”, “rectangular”, “triangular”, “epanechnikov”, “biweight”, “cosine” or “optcosine”, with default “gaussian”, and may be abbreviated to a unique prefix (single letter).
“cosine” is smoother than “optcosine”, which is the usual ‘cosine’ kernel in the literature and almost MSE-efficient. However, “cosine” is the version used by S.
n
the number of equally spaced points at which the density is to be estimated. When n > 512, it is rounded up to a power of 2 during the calculations (as fft is used) and the final result is interpolated by approx. So it almost always makes sense to specify n as a power of two.
joint.bw
character string indicating whether (and how) the smoothing bandwidth should be computed from the joint data distribution. The default value of “none” means that bandwidths will be computed independently for each data subgroup. Choosing “full” will result in a joint bandwidth computed from the full distribution. Similarly, “owm” will compute the joint bandwidth as the observation-weighted mean of the individual subgroup bandwidths. Note that the joint.bw argument is only relevant for grouped or faceted density plots.
alpha
numeric value between 0 and 1 specifying the opacity of ribbon shading If no alpha value is provided, then will default to tpar(“ribbon.alpha”) (i.e., probably 0.2 unless this has been overridden by the user in their global settings.)
Details
The algorithm used in density.default 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.
The statistical properties of a kernel are determined by \(\sigma^2_K = \int t^2 K(t) dt\) which is always \(= 1\) for our kernels (and hence the bandwidth bw is the standard deviation of the kernel) and \(R(K) = \int K^2(t) dt\). MSE-equivalent bandwidths (for different kernels) are proportional to \(\sigma_K R(K)\) which is scale invariant and for our kernels equal to \(R(K)\). This value is returned when give.Rkern = TRUE. See the examples for using exact equivalent bandwidths.
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).
Titles
This tinyplot method for density plots differs from the base plot.density function in its treatment of titles. The x-axis title displays only the variable name, omitting details about the number of observations and smoothing bandwidth. Additionally, the main title is left blank by default for a cleaner appearance.
Examples
library("tinyplot")# "density" type convenience stringtinyplot(~Sepal.Length, data = iris, type ="density")
# grouped density exampletinyplot(~Sepal.Length | Species, data = iris, type ="density")
# use `bg = "by"` (or, equivalent `fill = "by"`) to get filled densitiestinyplot(~Sepal.Length | Species, data = iris, type ="density", fill ="by")
# use `type_density()` to pass extra arguments for customizationtinyplot(~Sepal.Length | Species, data = iris,type =type_density(bw ="SJ"),main ="Bandwidth computed using method of Sheather & Jones (1991)")
# The default for grouped density plots is to compute bandwidths# independently for each subgroup. To override, specify the type of joint# bandwidth computationtinyplot(~Sepal.Length | Species, data = iris, type ="density") # "none" (default)tinyplot_add(type =type_density(joint.bw ="full"), lty =2) # full datasettinyplot_add(type =type_density(joint.bw ="owm"), lty =3) # obs-weighted meanlegend("topright", c("None", "Full", "OWM"), lty =1:3, title ="Joint BW")