simulate_kde: Simulation with Kernel Density Estimation

Description Usage Arguments Details Value References See Also Examples

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

1
2
3
4
5
6
7
8
9
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.

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), (-∞,+∞)

cauchy

Cauchy distribution, (-∞,+∞)

lnorm

log-normal distribution, (0,+∞)

exp

exponential distribution, (0,+∞)

gamma

gamma distribution, (0,+∞)

weibull

Weibull distribution, (0,+∞)

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.

Value

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

References

See Also

find_best_fit, kde

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## 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))

## 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")

simukde documentation built on May 20, 2021, 5:09 p.m.