Description Usage Arguments Value Examples
View source: R/kernel_density.R
A pure R implementation of a one-dimensional KDE with a gaussian kernel. Mainly of programming interest.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | kde(
y,
bw = bw.nrd0(y),
N = 512,
lower = ry[1] - 3 * bw,
upper = ry[2] + 3 * bw
)
## S3 method for class 'kde'
plot(
x,
...,
col = "steel blue",
xlab = bquote(x == italic(.(x$data_name))),
ylab = expression(kde(italic(x)))
)
## S3 method for class 'kde'
print(x, ...)
## S3 method for class 'kde'
as.data.frame(x, row.names, optional, ..., responseName = "density")
|
y |
Numeric vector of observed values |
bw |
Numeric value: bandwidth, standard deviation of the kernel density |
N |
integer: number of equally spaced points for the kde |
lower |
Numeric value: lower range limit for the kde |
upper |
Numeric value: upper range limit for the kde |
x |
an obeject of class "kde" |
... |
Extra arguments passed on to methods. |
xlab, ylab, col |
base graphics parameters |
row.names, optional |
As for the as.data.frame generic |
responseName |
character string: name for the response variable |
a list object with results of the kde estimation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | set.seed(1234)
x_value <- rchisq(10000, 2)
kx <- kde(x_value, lower = 0)
kx
if("package:ggplot2" %in% search()) {
ggplot(as.data.frame(kx)) +
aes(x = x_value, y = density) +
geom_line(colour = "steel blue") +
geom_function(fun = function(x) dchisq(x, 2),
colour = "firebrick3") +
theme_bw() +
xlab(expression(x == italic(x_value))) +
ylab(expression(kde(italic(x))))
} else {
oldPar <- par(mar = c(3, 3, 1, 1) + 0.1, las = 1, mgp = c(1.5, 0.5, 0),
cex.axis = 0.7, cex.lab = 0.8, tck = -0.015)
plot(kx, ylim = c(0,0.5))
curve(dchisq(x, 2), add = TRUE, col = 2, n = 1000)
rug(x_value, col = "dark green")
box()
par(oldPar)
}
rm(oldPar)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.