computeNeighbors: Compute neighbor sets for a Nearest-Neighbor Gaussian Process...

View source: R/v03_NNGP_neighbors.R

computeNeighborsR Documentation

Compute neighbor sets for a Nearest-Neighbor Gaussian Process (NNGP)

Description

Computes ordered neighbor sets based on Vecchia's approximation for a set of spatial coordinates. Locations are first ordered by their x-coordinate, and neighbors are selected from previously ordered locations.

Usage

computeNeighbors(coords, k, proj)

Arguments

coords

Numeric matrix (M \times 2) of spatial coordinates, where each row corresponds to a location.

k

Positive integer. Number of neighbors for each location.

proj

Coordinate reference system (CRS) used to compute distances. Can be an EPSG code (e.g., 4326) or a CRS object compatible with sf.

Details

Neighbor selection follows a Vecchia-type ordering. For each location i, neighbors are chosen from the set of previously ordered locations 1, \dots, i-1.

Distances are computed using sf::st_distance and depend on the CRS.

The first location has no neighbors. For the first k+1 locations, all previous locations are used as neighbors. For subsequent locations, the k nearest neighbors among previously ordered points are selected.

Value

A list with components:

coords_sorted

Matrix of coordinates sorted by x-coordinate

edist_sorted

Matrix (M \times M) of pairwise distances between sorted coordinates

neighbors

Binary matrix (M \times M) indicating neighbor relationships

neighbor_idx

Matrix (M \times k) of neighbor indices for each location

neighbors_dist

Matrix (M \times k) of distances to neighbors

Author(s)

Fabian Ketwaroo

See Also

st_distance

Examples

coords <- matrix(runif(40), ncol = 2)
res <- computeNeighbors(coords, k = 5, proj = 4326)


BayesNSGP documentation built on Sept. 10, 2026, 5:08 p.m.