Two-Dimensional Kernel Density Estimation

DESCRIPTION:

Two-dimensional kernel density estimation with an axis-aligned bivariate normal kernel, evaluated on a square grid.

USAGE:

kde2d(x, y, h, n = 25, lims = c(range(x), range(y)))

REQUIRED ARGUMENTS:

x
x coordinate of data
y
y coordinate of data

OPTIONAL ARGUMENTS:

h
vector of bandwidths for x and y directions. Defaults to normal reference bandwidth.
n
Number of grid points in each direction.
lims
The limits of the rectangle covered by the grid as c(xl, xu, yl, yu).

VALUE:

A list of three components.
x, y
The x and y coordinates of the grid points, vectors of length n .
z
An n x n matrix of the evaluated density.

EXAMPLES:

geyser2 <- data.frame(as.data.frame(geyser)[-1,],
                      pduration = geyser$duration[-299])
attach(geyser2)
xyplot(waiting ~ pduration, xlim = c(0.5, 6), ylim = c(40, 110))
f1 <- kde2d(pduration, waiting, n = 50, lims = c(0.5 , 6, 40, 110))
levelplot(z ~ x*y, con2tr(f1),
   xlab = "previous duration", ylab = "waiting",
   at = seq(0, 0.07, 0.001), colorkey = F,
   col.regions = rev(trellis.par.get("regions")$col))
f2 <- kde2d(pduration, waiting, n = 50, lims = c(0.5, 6, 40, 110),
   h = c(width.SJ(duration), width.SJ(waiting)) )
levelplot(z ~ x*y, con2tr(f2),
   xlab = "previous duration", ylab = "waiting",
   at = seq(0, 0.07, 0.001), colorkey = F,
   col.regions = rev(trellis.par.get("regions")$col))
wireframe(z ~ x*y, con2tr(f2),
   aspect = c(1, 0.5), screen = list(z=20, x=-60), zoom = 1.2)