Weighted kernel density estimator (wKDE)

Share:

Description

wKDE gives a (weighted) kernel density estimate (KDE) for univariate data.

If weights are not provided, all samples count equally. It evaluates on new data point by interpolation (using approx).

mv_KDE uses the locfit.raw function in the locfit package to estimate KDEs for multivariate data. Note: Use this only for small dimensions, very slow otherwise.

Usage

1
2
3
wKDE(x, eval.points = x, weights = NULL, kernel = "gaussian", bw = "nrd0")

mv_wKDE(x, eval.points = x, weights = NULL, kernel = "gaussian")

Arguments

x

data vector

eval.points

points where the density should be evaluated. Default: eval.points = x.

weights

vector of weights. Same length as x. Default: weights=NULL - equal weight for each sample.

kernel

type of kernel. Default: kernel='Gaussian'. See density and locfit.raw for additional options.

bw

bandwidth. Either a character string indicating the method to use or a real number. Default: bw="nrd0". Again see density for other options.

Value

A vector of length length(eval.points) (or nrow(eval.points)) with the probabilities of each point given the nonparametric fit on x.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
### Univariate example ###
xx <- sort(c(rnorm(100, mean = 1), runif(100)))
plot(xx, wKDE(xx), type = "l")
yy <- sort(runif(50, -1, 4) - 1)
lines(yy, wKDE(xx, yy), col = 2)
### Multivariate example ###
XX <- matrix(rnorm(100), ncol = 2)
YY <- matrix(runif(40), ncol = 2)
dens.object <- mv_wKDE(XX)

plot(dens.object)
points(mv_wKDE(XX, YY), col = 2, ylab = "")

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.