kde_1d: One-dimensional Kernel Density Estimate

View source: R/kde_2d_mass.R

kde_1dR Documentation

One-dimensional Kernel Density Estimate

Description

A pure R implementation of an approximate one-dimensional KDE, similar to density but using a different algorithm not involving fft. Two extra facilities are provided, namely (a) the kernel may be given either as a character string to select one of a number of kernel functions provided, or a user defined R function, and (b) the kde may be fitted beyond the prescribed limits for the result, and folded back to emulate the effect of having known bounds for the distribution.

Usage

kde_1d(
  x,
  bw = bw.nrd0,
  kernel = c("gaussian", "biweight", "cosine", "epanechnikov", "logistic", "optCosine",
    "rectangular", "squaredCosine", "triangular", "tricube", "triweight", "uniform"),
  n = 512,
  limits = c(rx[1] - cut * bw, rx[2] + cut * bw),
  cut = 3,
  na.rm = FALSE,
  adjust = 1,
  fold = FALSE,
  ...
)

## S3 method for class 'kde_1d'
print(x, ...)

## S3 method for class 'kde_1d'
plot(
  x,
  ...,
  col = "steel blue",
  las = 1,
  xlab = bquote(x == italic(.(x$data_name))),
  ylab = expression(kde(italic(x)))
)

Arguments

x

A numeric vector for which the kde is required or (in methods) an object of class "kde_1d"

bw

The bandwidth or the bandwidth function.

kernel

The kernel function, specified either as a character string or as an R function. Partial matching of the character string is allowed.

n

Integer, the number of equally-spaced values in the abscissa of the kde

limits

numeric vector of length 2. Prescribed x-range limits for the x-range of the result. May be infinite, but infinite values will be pruned back to an appropriate value as determined by the data.

cut

The number of bandwidths beyond the range of the input x-values to use

na.rm

Logical value: should any missing values in x be silently removed?

adjust

numeric value: a multiplier to be applied to the computed bandwidth.

fold

Logical value: should the kde be estimated beyond the prescribed limits for the result and 'folded back' to emulate the effect of having known range boundaries for the underlying distribution?

...

currently ignored, except in method functions

las, col, xlab, ylab

base graphics parameters

Value

A list of results specifying the result of the kde computation, of class "kde_1d"

Examples

set.seed(1234)
u <- runif(5000)
kdeu0 <- kde_1d(u, limits = c(-Inf, Inf))
kdeu1 <- kde_1d(u, limits = 0:1, kernel = "epan", fold = TRUE)
plot(kdeu0, col = 4)
lines(kdeu1, col = "dark green")
fun <- function(x) (0 < x & x < 1) + 0
curve(fun, add=TRUE, col = "grey", n = 1000)

MASSExtra documentation built on Feb. 16, 2023, 10:55 p.m.