The density() function can be used to compute kernel density estimates. It supports the following kernels:
["gaussian", "epanechnikov", "rectangular", "triangular", "biweight", "cosine", "optcosine"]
The format of the density() function is as follows:
density(x, bw = "nrd0", adjust = 1,
kernel = c("gaussian", "epanechnikov", "rectangular",
"triangular", "biweight",
"cosine", "optcosine"),
weights = NULL, window = kernel, width,
give.Rkern = FALSE, subdensity = FALSE,
n = 512, from, to, cut = 3, na.rm = FALSE, ...)
If you want to use a built-in bandwidth calculation method, you can try one of the followings: "nrd," "nrd0", "ucv," and "bcv." Parameter n is used to specify the number of equally spaced points (bins) at which the density is to be estimated.
Here is an example to show how to use this function to compute the KDE.
xvals <- runif(100, min = 0, max = 1)
fitd <- density(xvals, bw='ucv', kernel="gaussian", n=100, from = 0, to=1)
fitd$y values will give the KDE at fitd$x points. fitd$bw will give the bandwidth used by the density function.