kNearestNeighbors function

Build Spatial Weight Matrix from k Nearest Neighbors

Build Spatial Weight Matrix from k Nearest Neighbors

Build a spatial weight matrix W using the k nearest neighbors of (x, y) coordinates

kNearestNeighbors(x, y, k = 6)

Arguments

  • x: x coordinate
  • y: y coordinate
  • k: number of nearest neighbors

Details

Determine the k nearest neighbors for a set of n points represented by (x, y) coordinates and build a spatial weight matrix W (n xx n). W will be a sparse matrix representation and row-standardised.

This method is a convenience method for quickly creating a spatial weights matrix based on planar coordinates. More ways to create W are available in knearneigh of package spdep.

Returns

The method returns a sparse spatial weight matrix W with dimension (n xx n) and k non-zero entries per row which represent the k nearest neighbors.

See Also

nb2listw and knearneigh

for computation of neighbors lists, spatial weights and standardisation.

Author(s)

Stefan Wilhelm wilhelm@financial.com

Examples

require(Matrix) # build spatial weight matrix W from random (x,y) coordinates W <- kNearestNeighbors(x=rnorm(100), y=rnorm(100), k=6) image(W, main="spatial weight matrix W")