Generate random sample from density() or wKDE

Description

This function draws random samples given data and a density estimate (or just providing the correct bandwidth bw).

Usage

1
2
rdensity(n = 100, data = NULL, fhat = NULL, bw = fhat$bw, weights = NULL, 
    kernel = "Gaussian")

Arguments

n

number of samples

fhat

an object of class 'density' for bandwidth selection (if bw is not explicitly provided as argument)

weights

vector of weights. Same length as data. Default weights=NULL - in this case equal weights for each point

data

underlying sample of fhat

kernel

kernel choice for fhat. Default: kernel='Gaussian'. See density for other options.

bw

choice of bandwidth. Default: bw=fhat$bw. Again see density for other options.

Examples

1
2
3
4
5
6
set.seed(1923)
xx <- c(rnorm(100, mean = 2), runif(100))
aa <- density(xx)
plot(aa)
xx_sample <- rdensity(n = 1000, fhat = aa, data = xx)
lines(density(xx_sample), col = 2)

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