View source: R/v03_NNGP_density.R
| NNGP.pred | R Documentation |
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).
NNGP.pred(x0, coords_sorted, rho, sigma2, w.x0.all, k)
x0 |
Numeric matrix ( |
coords_sorted |
Numeric matrix ( |
rho |
Positive numeric scalar. Length-scale parameter |
sigma2 |
Positive numeric scalar. Marginal variance parameter |
w.x0.all |
Numeric vector of length |
k |
Integer. The number of nearest neighbors to consider for the prediction approximation. |
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.
A numeric scalar representing a posterior predictive draw at location x_0.
Fabian Ketwaroo
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")}
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.