logdensity: A local polynomial log-density estimator

Description Usage Arguments Value References See Also Examples

View source: R/logdensity.R

Description

logdensity.fit is intended to be called from within logdensity, after performing basic argument verification. Use caution when calling logdensity.fit directly.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
logdensity(
  data,
  x,
  h,
  g,
  dg,
  m = "epanechnikov",
  minx = -Inf,
  maxx = Inf,
  S = 1,
  logf = TRUE,
  mc.cores = 1L,
  ...
)

logdensity.fit(data, x, h, g, dg, m, minx, maxx, logf, exact, ...)

Arguments

data

numeric vector of observations

x

points at which to estimate (if shorter than h, recycled to length of h). logdensity.fit only accepts scalar x.

h

bandwidth (if shorter than x, recycled to the length of x). logdensity.fit only accepts scalar x.

g

function of u, zl, and zr that must be equal to an S-length vector of 0's at zl = max((minx-x)/h,-1) and zr = min((maxx-x),1), where S is the order of the local polynomial approximation to the log-density. Function must be vectorized so that g(u, zl, zr) returns a matrix that is length(u) by S.

dg

function that evaluates derivative of g with respect to u. Must be vectorized and return a matrix that is length(u) by S.

m

kernel function used to compute the density. Can be a function, symbol, or character string that matches the name of a function or one of the kernels allowed in evalkernel. If m == "epanechnikov" and the polynomial order is 1, an exact solution is computed. Otherwise, the estimate of the log-density involves numerical integration.

minx

lower bound of support of x

maxx

upper bound of support of x

S

degree of polynomial expansion of log-density to be used with default g. If user supplies g and dg, this argument is ignored without warning.

logf

logical indicating whether the log-density should be compute, in addition to its derivative(s).

mc.cores

integer number of cores to use with mcmapply. If equal to 1 (default), mapply will be used to loop over x, instead.

...

Further arguments supplied to g and dg

exact

logical indicating whether an exact solution should be used (if available) or numerical integration. Exact solution currently only available with epanechnikov kernel and local linear approximation.

Value

logdensity returns an object of class logdensity which inherits from matrix. The S+1 by length(x) matrix of estimated log-densities (NA unless logf is TRUE) and derivatives has the following additional attributes:

logdensity.fit returns a numeric vector containing the log-density (if logf == TRUE) and its derivatives.

References

Pinkse, J. and Schurter, K. (2020) "Estimates of derivatives of (log) densities and related objects."

See Also

mapply, mcmapply, integrate, bellpoly

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
25
26
27
dat <- rchisq(n = 100, df = 2)
x <- seq(from = 0, to = 2, length.out = 20)

## fixed bandwidth
ld <- logdensity(data = dat, x = x, h = 0.5, minx = 0, S = 1)
print(ld)
plot(ld)

## variable bandwidth
h <- pmax(1-x, 0.5)
ld <- logdensity(data = dat, x = x, h = h, minx = 0, S = 2)
ld
plot(ld)

## Faa di Bruno's formula for the density and its derivatives
deriv <- 0L  # integer between 0 and S (=2 for most recent estimation)
exp(ld[1, ]) * colSums(bellpoly(ld[-1, ], n = deriv))

### verify formula for deriv = 0, 1, and 2 
exp(ld[1, ]) * colSums(bellpoly(ld[-1, ], n = 0L))  # density (0th derivative)
exp(ld[1, ])  # equivalent

exp(ld[1, ]) * colSums(bellpoly(ld[-1, ], n = 1L))  # 1st derivative of density
exp(ld[1, ]) * ld[2,]  # equivalent

exp(ld[1, ]) * colSums(bellpoly(ld[-1, ], n = 2L))  # 2nd derivative of density
exp(ld[1, ]) * (ld[2,]^2 + ld[3, ])  # equivalent

kschurter/logdensity documentation built on Feb. 22, 2022, 1:07 p.m.