knearneigh {spdep} | R Documentation

## K nearest neighbours for spatial weights

### Description

The function returns a matrix with the indices of regions belonging to the set of the k nearest neighbours of each other. If lonlat = TRUE, Great Circle distances are used.

### Usage

    knearneigh(x, k=1, lonlat = FALSE)

### Arguments

`x` | matrix of region point coordinates
`k` | number of nearest neighbours to be returned
`lonlat` | TRUE if point coordinates are longitude-latitude decimal degrees, in which case distances are measured in kilometers

### Details

The underlying C code is based on the `knn` function in the class package in the VR bundle.

### Value

A list of class `knn`

`nn` | integer matrix of region number ids
`np` | number of input points
`k` | input required k
`dimension` | number of columns of x
`x` | input coordinates

### Author(s)

Roger Bivand Roger.Bivand@nhh.no

### See Also

`knn`, `dnearneigh`, `knn2nb`

### Examples

    data(columbus)
    col.knn <- knearneigh(coords, k=4)
    library(maptools)
    plot(polys, border="grey", forcefill=FALSE)
    plot(knn2nb(col.knn), coords, add=TRUE)
    title(main="K nearest neighbours, k = 4")
    data(state)
    us48.fipsno <- read.geoda(system.file("etc/weights/us48.txt",
     package="spdep")[1])
    if (as.numeric(paste(version$major, version$minor, sep="")) < 19) {
     m50.48 <- match(us48.fipsno$"State.name", state.name)
    } else {
     m50.48 <- match(us48.fipsno$"State_name", state.name)
    }
    xy <- as.matrix(as.data.frame(state.center))[m50.48,]
    llk4.nb <- knn2nb(knearneigh(xy, k=4, lonlat=FALSE))
    gck4.nb <- knn2nb(knearneigh(xy, k=4, lonlat=TRUE))
    plot(llk4.nb, xy)
    plot(diffnb(llk4.nb, gck4.nb), xy, add=TRUE, col="red", lty=2)
    title(main="Differences between Euclidean and Great Circle k=4 neighbours")
    summary(llk4.nb, xy, lonlat=TRUE)
    summary(gck4.nb, xy, lonlat=TRUE)

[Package spdep version 0.3-25 Index]