View source: R/kNearestNeighbors.R
kNearestNeighbors | R Documentation |
Build a spatial weight matrix W using the k nearest neighbors of (x, y) coordinates
kNearestNeighbors(x, y, k = 6)
x |
x coordinate |
y |
y coordinate |
k |
number of nearest neighbors |
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
.
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.
Stefan Wilhelm <wilhelm@financial.com>
nb2listw
and knearneigh
for computation of neighbors lists, spatial weights and standardisation.
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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.