Package 'adlaplaceGrf'

Title: FEM Matern Gaussian Random Fields for Adlaplace
Description: Tensor-product B-spline finite-element ingredients for Matern Gaussian random fields (mass, stiffness, and higher-order Grams) for use with the adlaplace Laplace approximation framework. Provides design and Gram matrix construction on regular grids, with optional terra SpatRaster / SpatVector input, and AD tape builders for random_fem_ssq_* / random_fem_det_*.
Authors: Patrick Brown [aut, cre, cph]
Maintainer: Patrick Brown <[email protected]>
License: MPL-2.0
Version: 0.5.2
Built: 2026-07-17 23:38:41 UTC
Source: https://github.com/eborgnine/adlaplace

Help Index


Symbolic LDL pattern for a sparse FEM precision structure

Description

Builds the permutation and unit-lower pattern required by adlaplace's chol_update for a precision with the same sparsity as Q_structure (typically the union of FEM Gram patterns).

Usage

fem_chol_pattern(Q_structure)

Arguments

Q_structure

Symmetric sparse matrix with the structural nonzeros of Q(theta) (numeric values only need to make the matrix positive definite for symbolic analysis; diagonals are inflated if needed).

Value

A list with perm (0-based), L1 (dtCMatrix / unit lower), and perm_inv (0-based), suitable for the chol slot of a ⁠random_fem_*⁠ precision payload.


Assemble FEM Matern precision Q2 or Q3 from Grams

Description

Assemble FEM Matern precision Q2 or Q3 from Grams

Usage

fem_precision(kappa, tau, C, G, G2, G3 = NULL, alpha = 2L)

Arguments

kappa, tau

Positive SPDE parameters.

C, G, G2, G3

Sparse Grams from grf_bspline().

alpha

2 or 3.

Value

Sparse dgCMatrix precision.


Build ⁠random_fem_ssq_*⁠ / ⁠random_fem_det_*⁠ precision payload for adlaplace

Description

Build ⁠random_fem_ssq_*⁠ / ⁠random_fem_det_*⁠ precision payload for adlaplace

Usage

fem_precision_payload(fem, alpha = 2L)

Arguments

fem

Result of grf_bspline() (or list with C, G, G2, optional G3).

alpha

2 or 3.

Value

List for ad_data@precision: Grams, chol pattern, and Q CSC coefficients aligned for on-tape assembly.


Structural nonzero pattern for Q2 or Q3 from Grams

Description

Structural nonzero pattern for Q2 or Q3 from Grams

Usage

fem_Q_structure(C, G, G2, G3 = NULL)

Arguments

C, G, G2, G3

Sparse Grams from grf_bspline(); G3 may be NULL for alpha = 2.

Value

Symmetric sparse matrix with the union of Gram nonzero patterns.


Tensor-product B-spline FEM ingredients for a 2D Matern GRF

Description

Builds the design matrix of tensor-product B-splines at evaluation coordinates together with sparse mass, stiffness, and higher-order Gram matrices used in the SPDE FEM precision for operator orders alpha = 2 and 3.

Usage

grf_bspline(coords, knots, degree = 2L, ...)

## S4 method for signature 'list'
grf_bspline(coords, knots, degree = 2L, ...)

## S4 method for signature 'data.frame'
grf_bspline(coords, knots, degree = 2L, ...)

Arguments

coords

Evaluation locations. For a data.frame (e.g. sites_eval from expand.grid), rows are point coordinates with columns x and y. For a plain list(x, y) of axis sequences, the full tensor grid is formed with expand.grid. With terra, SpatRaster / SpatVector are also accepted.

knots

Knot-line positions on each axis: list(x = seq(...), y = seq(...)) of unique increasing breakpoints (domain endpoints included). These are expanded to open B-spline knot vectors. With terra, a SpatRaster whose grid defines knot lines is also accepted.

degree

B-spline degree; must be ⁠>= 2⁠. Default 2 (alpha = 2). Use degree = 3 when assembling G3 for alpha = 3.

...

Passed to methods (unused for list / data.frame).

Details

Coefficient ordering is column-major (vec) over the n_x by n_y array of tensor basis indices: basis ⁠(i, j)⁠ maps to column i + (j - 1) * n_x.

Value

A list with:

A

dgCMatrix design, n by n_x * n_y

C, G, G2

sparse Grams

G3

sparse Gram or NULL if degree < 3

degree, knots, n_basis

metadata (n_basis = c(n_x, n_y))

Examples

sites_list <- list(x = seq(0, 1, by = 0.25), y = seq(0, 1, by = 0.25))
sites_eval <- do.call(expand.grid, sites_list)
fem <- grf_bspline(sites_eval, sites_list, degree = 2L)

Posterior mean, SD, and simulations of a matern field on a raster

Description

Evaluates the fitted Matern GRF on eval_grid under the Laplace approximation for the random effects (outer parameters fixed at the MLE). Returns a SpatRaster with layers mean, sd, and optionally sim1 ... simn.

Usage

matern_est(fit, eval_grid, n = 0L)

Arguments

fit

An "adlaplace_fit" from adlaplace::adlaplace() that includes exactly one matern() term.

eval_grid

A terra SpatRaster whose cell centers are evaluation sites.

n

Number of posterior simulations of the field. Default 0 (mean and sd only).

Value

A SpatRaster with n + 2 layers: mean, sd, and sim1...simn when n > 0.


Matern FEM spatial random field term

Description

Formula term for a 2D Matern GRF via tensor-product B-spline FEM, wired to adlaplaceGrf kernels random_fem_ssq_2 / random_fem_det_2 (shape nu = 1) or random_fem_ssq_3 / random_fem_det_3 (nu = 2). The quadratic form is a random shard; the log-determinant is a parameters companion via adlaplace::extra_ad_fun(). The first argument is a column name in data (like adlaplace::iid()): that column holds observation locations as a 2-column matrix, WKT text, or HEX WKB points. Knot lines are supplied at construction; the observation design matrix is built later in adlaplace::design() from data.

Usage

matern(
  x,
  knots,
  shape = 1L,
  init = c(1, 1),
  lower = c(1e-09, 1e-09),
  upper = c(Inf, Inf),
  parscale = c(1, 1)
)

## S4 method for signature 'matern'
extra_ad_fun(term)

## S4 method for signature 'matern'
design(term, data)

## S4 method for signature 'matern'
precision(term, data)

## S4 method for signature 'matern'
random_info(term, data)

## S4 method for signature 'matern'
theta_info(term)

## S4 method for signature 'matern'
beta_info(term, data)

Arguments

x

Name of the geometry / coordinate column in data.

knots

Knot lines as list(x = ..., y = ...) or a terra SpatRaster (cell-center x/y become the knot lines).

shape

Matern smoothness nu (1 or 2). Default 1L (SPDE alpha = 2, quadratic B-splines). Use 2 for nu = 2 / cubic B-splines.

init

Initial values for ⁠(range, sd)⁠ on the natural scale, where range = sqrt(8*nu)/kappa is the practical range (same units as coordinates; matches geostatsp/INLA) and sd is the marginal SD of the Matern field. Internally converted to SPDE ⁠(kappa, tau)⁠.

lower, upper

Bounds for ⁠(range, sd)⁠.

parscale

Optimization scales for ⁠(range, sd)⁠.

term

A matern term.

data

Model data frame (passed by adlaplace generics).

Details

In 2D, B-spline degree and SPDE order alpha equal shape + 1.

Value

A matern term object.

Functions

  • extra_ad_fun(matern): Companion parameters density for the FEM log-determinant shard.

  • design(matern): Design matrix of B-spline basis evaluations at coordinates in data[[term@term]].

  • precision(matern): FEM precision payload for ⁠random_fem_*⁠.

  • random_info(matern): Random-effect metadata (one row per basis weight).

  • theta_info(matern): Theta info for practical range = sqrt(8*nu)/kappa and field sd (log scale when transform_theta is TRUE).

  • beta_info(matern): Beta info (none).