rmvk: Random generation from product kernel density

View source: R/multivar-kd.R

rmvkR Documentation

Random generation from product kernel density

Description

Random generation from product kernel density

Usage

rmvk(
  n,
  y,
  bw = sqrt(diag(bw.silv(y))),
  kernel = c("gaussian", "epanechnikov", "rectangular", "triangular", "biweight",
    "cosine", "optcosine"),
  weights = NULL,
  adjust = 1,
  shrinked = FALSE
)

Arguments

n

number of observations. If length(n) > 1, the length is taken to be the number required.

y

numeric matrix or data.frame.

bw

numeric vector of length equal to ncol(y); the smoothing bandwidth to be used. The kernels are scaled such that this is the standard deviation of the smoothing kernel (see density for details). If provided as a single value, the same bandwidth is used for each variable.

kernel

a character string giving the smoothing kernel to be used. This must partially match one of "gaussian", "rectangular", "triangular", "epanechnikov", "biweight", "cosine" or "optcosine", with default "gaussian", and may be abbreviated.

weights

numeric vector of length equal to nrow(y); must be non-negative.

adjust

scalar; the bandwidth used is actually adjust*bw. This makes it easy to specify values like 'half the default' bandwidth.

shrinked

if TRUE random generation algorithm preserves mean and variances of the individual variables (see ruvk). Shrinking is applied to each of the variables individually.

Details

Product kernel density is defined in terms of independent univariate kernels

\hat{f_H}(\mathbf{x}) = \sum_{i=1}^n w_i \prod_{j=1}^m K_{h_j}( x_i - y_{ij})

where w is a vector of weights such that all w_i \ge 0 and \sum_i w_i = 1 (by default uniform 1/n weights are used), K_{h_j} is univariate kernel K parametrized by bandwidth h_j, where \boldsymbol{y} is a matrix of data points used for estimating the kernel density.

For functions estimating kernel densities please check KernSmooth, ks, or other packages reviewed by Deng and Wickham (2011).

For random generation the algorithm described in kernelboot is used. When using shrinked = TRUE, random noise is drawn from independent, shrinked univariate kernels.

References

Deng, H. and Wickham, H. (2011). Density estimation in R. http://vita.had.co.nz/papers/density-estimation.pdf

See Also

kernelboot

Examples


dat <- mtcars[, c("mpg", "disp")]

partmp <- par(mfrow = c(1, 2), mar = c(3, 3, 3, 3))

plot(rmvk(5000, dat, shrinked = FALSE), col = "#458B004D", pch = 16,
     xlim = c(0, 45), ylim = c(-200, 800),
     main = "Product kernel", axes = FALSE)
points(dat, pch = 2, lwd = 2, col = "red")
axis(1); axis(2)

plot(rmvk(5000, dat, shrinked = TRUE), col = "#458B004D", pch = 16,
     xlim = c(0, 45), ylim = c(-200, 800),
     main = "Product kernel (shrinked)", axes = FALSE)
points(dat, pch = 2, lwd = 2, col = "red")
axis(1); axis(2)

par(partmp)

cov(dat)
cov(rmvk(5000, dat, shrinked = FALSE))
cov(rmvk(5000, dat, shrinked = TRUE))


kernelboot documentation built on April 14, 2023, 5:14 p.m.