NNGP.pred: Posterior predictive sampling for NNGP

View source: R/v03_NNGP_density.R

NNGP.predR Documentation

Posterior predictive sampling for NNGP

Description

Generates a posterior predictive draw at a new location x_0 using the Nearest-Neighbor Gaussian Process (NNGP) approximation. This function automatically handles coordinate sorting, neighbor selection, and distance calculations internally following Algorithm 2 of Finley et al. (2019).

Usage

NNGP.pred(x0, coords_sorted, rho, sigma2, w.x0.all, k)

Arguments

x0

Numeric matrix (1 \times 2) or vector of length 2 representing the coordinates of the new prediction location.

coords_sorted

Numeric matrix (M \times 2) of the observed training locations. Output from computeNeighbors.

rho

Positive numeric scalar. Length-scale parameter \rho.

sigma2

Positive numeric scalar. Marginal variance parameter \sigma^2.

w.x0.all

Numeric vector of length M. The realizations of the spatial process (random effects) at all observed locations, ordered to correspond with coords_sorted.

k

Integer. The number of nearest neighbors to consider for the prediction approximation.

Details

The function merges the prediction point x_0 into the training coordinates and identifies the k nearest neighbors from the set of points that precede it in the x-axis ordering.

The predictive distribution is Gaussian:

w(x_0) \mid \mathbf{w}_{N_0} \sim \mathcal{N}(m, v)

where m is the **Kriging mean** and v is the **Kriging variance** defined as:

  • m = c^\top C_{N_0}^{-1} \mathbf{w}_{N_0}

  • v = C(x_0, x_0) - c^\top C_{N_0}^{-1} c

Here:

  • N_0 is the set of indices of the k nearest neighbors of x_0 among the observed locations.

  • \mathbf{w}_{N_0} is the vector of realizations of the spatial process at those neighbor locations (passed as w.s.x0).

  • c is the k \times 1 covariance vector between x_0 and its neighbors in N_0.

  • C_{N_0} is the k \times k covariance matrix among the neighbors in N_0.

The exponential covariance function is used: C(d) = \sigma^2 \exp(-d / \rho). A small nugget (10^{-6}) is added to the diagonal of C_{N_0} for numerical stability.

The internal subsetting of w.x0.all ensures that \mathbf{w}_{N_0} correctly represents the process realizations at the identified neighbor locations.

Value

A numeric scalar representing a posterior predictive draw at location x_0.

Author(s)

Fabian Ketwaroo

References

Finley, A. O., Datta, A., Cook, B. D., Morton, D. C., Andersen, H. E., & Banerjee, S. (2019). Efficient algorithms for Bayesian nearest neighbor Gaussian processes. *Journal of Computational and Graphical Statistics*, 28(2), 401–414. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1080/10618600.2018.1537924")}

Examples


# Setup training data
M <- 50
coords <- matrix(runif(M * 2), ncol = 2)
w_all <- rnorm(M) # Mock spatial realizations

# coords_sorted and w.x0.all must be aligned to the same x-sorted order
# that computeNeighbors() would produce, since NNGP.pred() selects
# neighbors by position in that order.
ord <- order(coords[, 1])
coords_sorted <- coords[ord, ]
w_sorted <- w_all[ord]

# Predict at a new location using k = 5 neighbors
new_loc <- c(0.5, 0.5)
pred_draw <- NNGP.pred(x0 = new_loc, 
                       coords_sorted = coords_sorted, 
                       rho = 0.2, 
                       sigma2 = 1.0, 
                       w.x0.all = w_sorted,
                       k = 5)


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