# ruvk() produces samples from kernel densities as estimated using
# density() function from base R
hist(ruvk(1e5, mtcars$mpg), 100, freq = FALSE, xlim = c(5, 40))
lines(density(mtcars$mpg, bw = bw.nrd0(mtcars$mpg)), col = "red")
# when using 'shrinked = TRUE', the samples differ from density() estimates
# since they are shrinked to have the same variance as the underlying data
hist(ruvk(1e5, mtcars$mpg, shrinked = TRUE), 100, freq = FALSE, xlim = c(5, 40))
lines(density(mtcars$mpg, bw = bw.nrd0(mtcars$mpg)), col = "red")
# Comparison of different univariate kernels under standard parametrization
kernels <- c("gaussian", "epanechnikov", "rectangular", "triangular",
"biweight", "cosine", "optcosine")
partmp <- par(mfrow = c(2, 4), mar = c(3, 3, 3, 3))
for (k in kernels) {
hist(ruvk(1e5, 0, 1, kernel = k), 25, freq = FALSE, main = k)
lines(density(0, 1, kernel = k), col = "red")
}
par(partmp)
Run the code above in your browser using DataLab