Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

rmvk

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

f(x) = sum[i](w[i] * prod[j]( Khj(x[i]-y[i,j]) ))

where w is a vector of weights such that all w[i] ≥ 0 and sum(w) = 1 (by default uniform 1/n weights are used), Khj is univariate kernel K parametrized by bandwidth h[j], where 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

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

Smoothed Bootstrap and Random Generation from Kernel Densities

v0.1.7
GPL-2
Authors
Tymoteusz Wolodzko
Initial release
2020-02-13

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.