Learn R Programming

spatialprobit (version 1.0.4)

kNearestNeighbors: Build Spatial Weight Matrix from k Nearest Neighbors

Description

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

Usage

kNearestNeighbors(x, y, k = 6)

Value

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

Arguments

x

x coordinate

y

y coordinate

k

number of nearest neighbors

Author

Stefan Wilhelm <wilhelm@financial.com>

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 \(\times\) 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.

See Also

nb2listw and knearneigh for computation of neighbors lists, spatial weights and standardisation.

Examples

Run this code
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")

Run the code above in your browser using DataLab