kde_2d: A Two-dimensional Kernel Density Estimate

Description Usage Arguments Value Examples

Description

A pure R implementation of an approximate two-dimensional kde computation, where the approximation depends on the x- and y-resolution being fine, i.e. the number of both x- and y-points should be reasonably large, at least 256. The coding follows the same idea as used in kde2d, but scales much better for large data sets.

Usage

 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
kde_2d(
  x,
  y = NULL,
  bw = list(x = bw.nrd0, y = bw.nrd0),
  kernel = c("gaussian", "biweight", "cosine", "epanechnikov", "logistic", "optCosine",
    "rectangular", "squaredCosine", "triangular", "tricube", "triweight", "uniform"),
  n = 256,
  x_limits = c(rx[1] - cut * bw["x"], rx[2] + cut * bw["x"]),
  y_limits = c(ry[1] - cut * bw["y"], ry[2] + cut * bw["y"]),
  cut = 1,
  na.rm = FALSE,
  adjust = 53/45,
  ...
)

## S3 method for class 'kde_2d'
print(x, ...)

## S3 method for class 'kde_2d'
plot(
  x,
  ...,
  las = 1,
  xlab = bquote(italic(.(x$data_name[["x"]]))),
  ylab = bquote(italic(.(x$data_name[["y"]]))),
  col = hcl.colors(50, "YlOrRd", rev = TRUE)
)

Arguments

x, y

Numeric vectors of the same length specified in any way acceptable to xy.coords. In methods, x will be an object of class "kde_2d"

bw

bandwidths. May be a numeric vector of length 1 or 2, or a function, or list of two bandwidth computation functions. Short entities will be repeated to length 1. The first relates to the x-coordinate and the second to the y.

kernel

As for kde_1d though 1 or 2 values may be specified relating to x- and y-coordinates respectively. Short entities will be repeated to length 2

n

positive integer vector of length 1 or 2 specifying the resolution required in the x- and y-coordinates respectively. Short values will be repeated to length 2.

x_limits, y_limits

Numeric vectors specifying the limits required for the result

cut

The number of bandwidths beyond the x- and y-range limits for the resuls.

na.rm

Should missing values be silently removed?

adjust

A factor to adjust both bandwidths to regulate smoothness

...

currently ignored, except in method functions

las, col, xlab, ylab

base graphics parameters

Value

A list of results of class "kde_2d". The result may be used directly in image or contour.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
krc <- with(Boston, {
  criminality <- log(crim)
  spaciousness <- sqrt(rm)
  kde_2d(criminality, spaciousness, n = 128, kernel = "biweight")
})
plot(krc, xlab = expression(italic(Criminality)), ylab = expression(italic(Spaciousness)))
contour(krc, add = TRUE, lwd = 0.5)
points(sqrt(rm) ~ log(crim), Boston, pch = ".", cex = 2, col = "dark green")

with(krc, persp(x, 10*y, 3*z, border="transparent", col = "powder blue",
                theta = 30, phi = 15, r = 20, scale = FALSE, shade = TRUE, 
                xlab = "Criminality", ylab = "Spaciousness", zlab = "density"))

BillVenables/MASSExtra documentation built on March 8, 2021, 7:18 p.m.