Density: Densities

Description Usage Arguments Details See Also Examples

View source: R/density.R

Description

The S3 class Density tries to ensure some of the properties of densities and is a subclass IntegrableFunction (see: 'Details' for exact requirements).

Usage

1
Density(fun, support = NULL, subdivisions = 1000L, ...)

Arguments

fun

a R function taking a single numeric argument and returning a numeric vector of the same length. See 'Details' for further requirements.

support

numerical vector of length 2; the lower- and upper bound of the compact support in the first and second entry respectively. In particular non-finite values are prohibited. IntegrableFunction will try to find bounds on the support itself if NULL is passed.

subdivisions

positive numeric scalar; the subdivisions parameter for the function integrate_primitive.

...

additional parameters to keep fixed during the evaluation of fun.

Details

A density function is a real valued, non-negative, integrable function, such that its integral over the real numbers equals one. Density functions as R functions are required to

  1. be vectorised in its argument, taking a single numeric argument, returning a numerical vector of the same length only,

  2. return zero for inputs outside their compact support,

  3. return non-negative values for inputs inside their support,

  4. can be integrated over their support using integrate_primitive and the given number of subdivisions (the relative error converges),

  5. yield an integral of nearly 1 (absolute error < 1%).

See the 'Details' section of IntegrableFunction for comments on the restrictiveness of compact supports.

The S3 class Density exists to ensure some of the most basic properties of density functions. The class is build on IntegrableFunctions and inherits its structure.

The constructor Density tries to construct a valid Density object based on the passed arguments. Returned objects are guaranteed to pass the validator validate_Density.

Attention: This does not guarantee the conditions in the first 'Details' paragraph: see validate_Density.

See Also

validate_Density for the corresponding validator, IntegrableFunction for more information about the superclass.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
dens_norm <- Density(dnorm, c(-15, 15))
dens_unif <- Density(dunif)
x <- seq(from = -4, to = 4, length.out = 1000)
plot(x, dens_norm$fun(x),
     xlim=c(-5,5), ylim=c(0,1),
     main="Densities", xlab="", ylab="",
     col="black", type="l")
lines(x,dens_unif$fun(x), col="red")
legend("topright",
       legend=c("dens_norm", "dens_unif"),
       col=c("black","red"), lty=1, cex=0.8)

hericks/KDE documentation built on Aug. 22, 2020, 12:04 a.m.