kde | R Documentation |
This function behaves similarly to the density
function of the
stats package, but uses only R code. It is a demonstration
function intended to show how kernel density estimates are
computed, at least conceptually. Unlike density
, the kernel
may be supplied as an R function in a standard form. Example kernel
functions are provided. For computational efficiency, the
density
function of the stats package is far superior.
kde(x, bw = bw.nrd0, kernel = kernelGaussian, n = 512,
from = min(x) - cut * sd, to = max(x) + cut * sd,
adjust = 1, cut = 3, ...)
x |
Univeriate sample. Must be numeric. |
bw |
Either an explicit numeric bandwidth to be used for the kernel, or a function used to calculate it. |
kernel |
The kernel function to be used. Must have the same argument
sequence as |
n |
Then number of points covering the range at which to evaluate the KDE. More gives a smoother display of the result; fewer gives a quicker and more memory efficient computation. |
from |
Lower boundary for the computed KDE. |
to |
Upper boundary for the computed KDE. |
adjust |
Adjustment factor to be used for the bandwidth. |
cut |
Number of bandwidths by which to extend the range of the data for the range of the KDE |
... |
Additional arguments, if needed, to be supplied to the kernel function. |
This is a demonstration function intended to show, via R code, the way in which a kernel density estimate is computed.
For samples which are not too large the computation is reasonably
efficient, but for serious computations the standard function
density
, or some alternative, should be
used.
An object of class “density”, with essentially the same
structure as objects generated by the density
of the stats package. plot
and allied methods should apply.
Demonstration code only!
Bill Venables
kernelBiweight
and aliases; density
.
if(require("graphics")) {
with(MASS::geyser, {
hist(waiting, freq=FALSE, main="", border="grey", las=1)
lines(stats::density(waiting), col="skyblue", lwd=8)
lines(kde(waiting))
lines(kde(waiting, kernel = kernelUniform), col="red")
rug(jitter(waiting), col="blue")
legend("topleft", c("density histogram",
"KDE gaussian (denstiy)", "KDE gaussian (kde)",
"KDE rectangular (kde)"), lty = "solid", lwd=c(1,8,1,1),
col=c("grey", "skyblue", "black", "red"), bty="n")
})
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.