Various subsidiary kernel function, conversion of bandwidths and evaluating certain kernel integrals.
Various subsidiary kernel function, conversion of bandwidths and evaluating certain kernel integrals.
Functions for checking the inputs to the kernel functions, evaluating integrals ∫ulK∗(u)du for l=0,1,2 and conversion between the two bandwidth definitions.
x: location to evaluate KDE (single scalar or vector)
lambda: bandwidth for kernel (as half-width of kernel) or NULL
bw: bandwidth for kernel (as standard deviations of kernel) or NULL
kerncentres: kernel centres (typically sample data vector or scalar)
allownull: logical, where TRUE permits NULL values
kernel: kernel name (default = "gaussian")
truncpoint: upper endpoint as standardised location x/lambda
Returns
klambda and kbw return the lambda and bw bandwidths respectively.
The checking functions check.kinputs, check.klambda and check.kernel
will stop on errors and return no value.
ka0, ka1 and ka2
return the partial moment integrals specified above.
Details
Various boundary correction methods require integral of (partial moments of) kernel within the range of support, over the range [−1,p] where p
is the truncpoint determined by the standardised distance of location x
where KDE is being evaluated to the lower bound of zero, i.e. truncpoint = x/lambda. The exception is the normal kernel which has unbounded support so the [−5∗λ,p] where lambda is the standard deviation bandwidth. There is a function for each partial moment of degree (0, 1, 2):
ka0 - ∫−1pK∗(z)dz
ka1 - ∫−1puK∗(z)dz
ka2 - ∫−1pu2K∗(z)dz
Notice that when evaluated at the upper endpoint on the support p=1
(or p=∞ for normal) these are the zeroth, first and second moments. In the normal distribution case the lower bound on the region of integration is ∞ but implemented here as −5∗λ. These integrals are all specified in closed form, there is no need for numerical integration (except normal which uses the pnorm function).
See kpu for list of kernels and discussion of bandwidth definitions (and their default values):
bw - in terms of number of standard deviations of the kernel, consistent with the defined values in the density function in the R base libraries
lambda - in terms of half-width of kernel
The klambda function converts the bw to the lambda
equivalent, and kbw applies converse. These conversions are
kernel specific as they depend on the kernel standard deviations. If both bw and
lambda are provided then the latter is used by default. If neither are provided
(bw=NULL and lambda=NULL) then default is lambda=1.
check.kinputs checks all the kernel function inputs, check.klambda checks the pair of inputted bandwidths and check.kernel checks the kernel names.
Examples
xx = seq(-2,2,0.01)plot(xx, kdgaussian(xx), type ="l", col ="black",ylim = c(0,1.2))lines(xx, kduniform(xx), col ="grey")lines(xx, kdtriangular(xx), col ="blue")lines(xx, kdepanechnikov(xx), col ="darkgreen")lines(xx, kdbiweight(xx), col ="red")lines(xx, kdtriweight(xx), col ="purple")lines(xx, kdtricube(xx), col ="orange")lines(xx, kdparzen(xx), col ="salmon")lines(xx, kdcosine(xx), col ="cyan")lines(xx, kdoptcosine(xx), col ="goldenrod")legend("topright", c("Gaussian","uniform","triangular","Epanechnikov","biweight","triweight","tricube","Parzen","cosine","optcosine"), lty =1,col = c("black","grey","blue","darkgreen","red","purple","salmon","orange","cyan","goldenrod"))