Learn R Programming

simukde (version 1.3.0)

simulate_kde: Simulation with Kernel Density Estimation

Description

Generates random values from a univariate and multivariate continuous distribution by using kernel density estimation based on a sample. The function uses the Accept-Reject method.

Usage

simulate_kde(
  x,
  n = 100,
  distr = "norm",
  const.only = FALSE,
  seed = NULL,
  parallel = FALSE,
  ...
)

Arguments

x

a numeric vector, matrix or data frame; data.

n

integer; the number of random values will be generated.

distr

character; instrumental or candidate distribution name. See details.

const.only

logical; if TRUE, the constant of the Accept-Reject method will be returned.

seed

a single value, interpreted as an integer, or NULL (default).

parallel

logical; if TRUE parallel generator will be worked. FALSE is default.

...

other parameters for functions kde.

Value

list of given data, simulated values, kernel density estimation and the constant of the Accept-Reject method when const.only is FALSE (default).

Details

Such function uses the function kde as kernel density estimator.

The Accept-Reject method is used to simulate random variables. Following code named distributions can be used as a value of the argument distr and an instrumental or candidate distribution of the simulation method. For univariate distributions:

norm

normal distribution (default), \((-\infty,+\infty)\)

cauchy

Cauchy distribution, \((-\infty,+\infty)\)

lnorm

log-normal distribution, \((0,+\infty)\)

exp

exponential distribution, \((0,+\infty)\)

gamma

gamma distribution, \((0,+\infty)\)

weibull

Weibull distribution, \((0,+\infty)\)

unif

uniform distribution, \((a,b)\)

And you can choose the best fitting instrumental distribution to simulate random variables more effectively by using find_best_fit. See examples.

For multivariate distributions, "norm" (multivariate normal distribution) is used.

References

  • Tarn Duong (2018). ks: Kernel Smoothing. R package version 1.11.2. https://CRAN.R-project.org/package=ks

  • Christian P. Robert and George Casella (2010) Introducing Monte Carlo Methods with R. Springer. Pages 51-57.

See Also

find_best_fit, kde

Examples

Run this code
# NOT RUN {
## 1-dimensional data
data(faithful)
hist(faithful$eruptions)
res <- simukde::simulate_kde(x = faithful$eruptions, n = 100, parallel = FALSE)
hist(res$random.values)

## Simulation with the best fitting instrumental distribution
data(faithful)
par(mfrow = c(1, 3))
hist(faithful$eruptions)
fit <- simukde::find_best_fit(x = faithful$eruptions, positive = TRUE)
res <- simukde::simulate_kde(
  x = faithful$eruptions, n = 100,
  distr = fit$distribution, parallel = FALSE
)
hist(res$random.values)
par(mfrow = c(1, 1))
# }
# NOT RUN {
## 2-dimensional data
data(faithful)
res <- simukde::simulate_kde(x = faithful, n = 100)
plot(res$kde, display = "filled.contour")
points(x = res$random.values, cex = 0.25, pch = 16, col = "green")
points(x = faithful, cex = 0.25, pch = 16, col = "black")
# }

Run the code above in your browser using DataLab