This function can be used to generate nearest neighbour information for a set of 2D data points.

nearest.neighbours(x, y)

Arguments

x

vector containing \(x\) ccordinates of points.

y

vector containing \(x\) ccordinates of points.

Details

The C++ implementation of this function is used inside the locpoly and interp functions.

Value

A list with two components

index

A matrix with one row per data point. Each row contains the indices of the nearest neigbours to the point associated with this row, currently the point itself is also listed in the first row, so this matrix is of dimension \(n\) times \(n\) (will change to \(n\) times \(n-1\) later).

dist

A matrix containing the distances according to the neigbours listed in component index.

Author

Albrecht Gebhardt <albrecht.gebhardt@aau.at>, Roger Bivand <roger.bivand@nhh.no>

See also

Examples

data(franke)
## use only a small subset
fd <- franke$ds1[1:5,]
nearest.neighbours(fd$x,fd$y)
#> $index
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    1    2    3    4    5
#> [2,]    2    3    1    4    5
#> [3,]    3    4    2    5    1
#> [4,]    4    3    5    2    1
#> [5,]    5    4    3    2    1
#> 
#> $dist
#>      [,1]      [,2]      [,3]      [,4]      [,5]
#> [1,]    0 0.2887147 0.5257926 0.7305571 0.9418279
#> [2,]    0 0.2374946 0.2887147 0.4420102 0.6531478
#> [3,]    0 0.2084088 0.2374946 0.4174614 0.5257926
#> [4,]    0 0.2084088 0.2115727 0.4420102 0.7305571
#> [5,]    0 0.2115727 0.4174614 0.6531478 0.9418279
#>