kdeDensity: Computes an evaluable kernel density estimate on a dataset

View source: R/helpers.R

kdeDensityR Documentation

Computes an evaluable kernel density estimate on a dataset

Description

The distance is measured in the metric d(x,y)= sqrt((x-y)^t M (x-y)) given by the 'Metric' tensor M. The kernel density is estimated as

Usage

kdeDensity(X, ..., bandwidth, Metric, kernel, K, Var, sdFactor)

## S3 method for class 'matrix'
kdeDensity(
  X,
  ...,
  bandwidth = NULL,
  Metric = if (is.null(bandwidth)) invertVariance(Var * sdFactor^2) else
    diag(1/bandwidth^2, nrow = ncol(X)),
  K = 1/sqrt(2 * pi)^(NCOL(X) - 1) * sqrt(det(Metric)),
  kernel = dnorm,
  Var = var(X),
  sdFactor = (0.2)^(1/NCOL(X))
)

## S3 method for class 'data.frame'
kdeDensity(
  X,
  ...,
  bandwidth = NULL,
  Metric = if (is.null(bandwidth)) invertVariance(Var * sdFactor^2) else
    diag(1/bandwidth^2, nrow = ncol(X)),
  K = 1/sqrt(2 * pi)^(NCOL(X) - 1) * sqrt(det(Metric)),
  kernel = dnorm,
  Var = var(X),
  sdFactor = (0.2)^(1/NCOL(X))
)

## S3 method for class 'list'
kdeDensity(
  X,
  ...,
  bandwidth = NULL,
  Metric = if (is.null(bandwidth)) 1/(Var * sdFactor^2) else 1/bandwidth^2,
  K = sqrt(Metric),
  kernel = dnorm,
  Var = var(X),
  sdFactor = 0.2,
  minus = "-"
)

## S3 method for class 'numeric'
kdeDensity(
  X,
  ...,
  support = Support(a = -Inf, b = Inf, lim = range(X)),
  bandwidth = NULL,
  Metric = if (is.null(bandwidth)) 1/(Var * sdFactor^2) else 1/bandwidth^2,
  K = sqrt(Metric),
  kernel = dnorm,
  Var = var(X),
  sdFactor = 0.2,
  minus = "-"
)

Arguments

X

the sample to compute the kernel density estimated from

...

futher generic parameter

bandwidth

the bandwidth to be used, if null the metric tensor is computed in a different way.

Metric

the metric tensor used to scale the distances before passed to the kernel.

kernel

a kernel function evaluated for a scaled distance. In case of univariate data the kernel is also evaluated with just the scaled difference (i.e. signed distances) and can thus be asymetric.

K

the factor to scale the kernel density to get an integral of 1. The factor can be ignored in base of BData objects.

Var

the variance (matrix) of the dataset to base a Metric calculation on

sdFactor

the factor to multiply the directional standard deviation width to the the directional bandwidth.

minus

the function to be used to compute the distance of elements

support

the support on which to compute the kernel density estimate. Finite supports lead to a mirroring at the support limits. I future also an independent estimate of the density at discrete points is planned.

Details

f(x) = 1/N sum_i=1^n scale*kernel(d(x,X_i))

The computation is done explicity in all calls to the resulting function.

Value

a function that can be evaluated with a dataset similar to X

Methods (by class)

  • kdeDensity(matrix): method for multivariate datasets provided as matrix

  • kdeDensity(data.frame): method for multivariate datasets provided as data.frame

  • kdeDensity(list): method for datasets provided as list. The minus operator must be defined on the elements.

  • kdeDensity(numeric): method for datasets provided as a numeric vector.

Examples

X = rnorm(10)
x=seq(-4,4,by=0.1)
f = kdeDensity(X,sdFactor=0.5)
plot(x,f(x),type="b")

# data generation and kde function construction
xx = cbind(rnorm(100, sd=2.5), rnorm(100, sd=0.5))
kdxy = kdeDensity(xx)
# check that the integral is approx 1:
x0 = seq(from=-2.5, to=2.5, length.out=25)
xys = expand.grid(x0*2.5, x0*0.5)
zs =  kdxy(xys)
(dx0 = x0[2]-x0[1])
(A = sum(zs)*dx0^2*2.5*0.5) # bad approx but within expectations given low precision
# compare with kde from stats::density for one marginal:
dim(zs) = c(25,25)
dx = density(xx[,1], kernel="gaussian")
dx |> plot()
lines(x0*2.5, apply(zs,1,sum)*dx0*0.5/A, col=2)
rug(xx[,1])
xx = rnorm(1000, sd=2.5) # simulate some data
# generate and plot the kde function
kdx = kdeDensity(xx)
plot(kdx, from=-5, to=5)
# ensure that the integral is approx 1
xs = seq(from=-5, to=5, by=0.01)
ys = kdx(xs)
sum(ys)*0.01             #  approx 1.0
# graphically compare with kde from stats::density
dx = density(xx, kernel="gaussian")
with(dx, lines(x, y, col=2))
plot(dx)
curve(kdx, from=-4, to=4, col=2, add=TRUE)

DistributionalDDA documentation built on Oct. 20, 2026, 5:06 p.m.