Skip to contents

Evaluates the kernel sum \(f(u, v) = (N h_x h_y)^{-1} \sum_k \phi((u - x_k) / h_x) \phi((v - y_k) / h_y)\) on a regular grid, with kernel standard deviations from the normal-reference plug-in rule \(h = 1.06 \min(s, IQR / 1.34) N^{-1/5}\) (method "nrd", equal to MASS::kde2d with its default bandwidth) or Scott's rule \(h = s N^{-1/6}\) (method "scott"). Identical to the Python arm morie.fn.ptkde.spatial_kde.

Usage

SpatialKde(data, method = "nrd", h = NULL, n = 25, lims = NULL)

Arguments

data

Two-column matrix (or data frame) of point coordinates.

method

"nrd" or "scott" ("default" means "nrd").

h

Kernel standard deviations (scalar or pair); overrides the rule.

n

Grid size per axis (scalar or pair).

lims

Grid limits c(xmin, xmax, ymin, ymax); default the data range.

Value

List: x, y (grid), z (density matrix, rows along x), bandwidth, n.

References

Silverman, B. W. (1986). Density Estimation for Statistics and Data Analysis. Chapman and Hall.

Scott, D. W. (1992). Multivariate Density Estimation. Wiley.

Venables, W. N. and Ripley, B. D. (2002). Modern Applied Statistics with S, 4th edn. Springer.

Examples

pts <- cbind(c(0.1, 0.4, 0.5, 0.9, 0.3), c(0.2, 0.9, 0.4, 0.7, 0.6))
SpatialKde(pts, n = 3)$z
#>            [,1]      [,2]      [,3]
#> [1,] 1.63941181 0.5507473 0.1301617
#> [2,] 0.84849560 1.5835738 1.2059057
#> [3,] 0.02546844 1.1059336 0.8210617