| Title: | Finite Element Approximations for Adlaplace |
|---|---|
| Description: | Finite-element approximations for hierarchical models with the adlaplace Laplace approximation framework. Provides tensor-product B-spline FEM ingredients for Matern Gaussian random fields (mass, stiffness, and higher-order Grams), including hierarchical locally refined B-spline bases, design and Gram matrix construction on regular grids, optional terra SpatRaster / SpatVector input, AD tape builders for random_fem_ssq_* / random_fem_det_*, and a near-monotone mGP smoother (mgp) with a predictive SD (psd) interface. |
| Authors: | Patrick Brown [aut, cre, cph], Ziang Zhang [aut] |
| Maintainer: | Patrick Brown <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 0.7.3 |
| Built: | 2026-10-16 21:42:57 UTC |
| Source: | https://github.com/eborgnine/adlaplace |
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.
fem_bspline(coords, knots, degree = 2L, ...) ## S4 method for signature 'list' fem_bspline(coords, knots, degree = 2L, ...) ## S4 method for signature 'data.frame' fem_bspline(coords, knots, degree = 2L, ...)fem_bspline(coords, knots, degree = 2L, ...) ## S4 method for signature 'list' fem_bspline(coords, knots, degree = 2L, ...) ## S4 method for signature 'data.frame' fem_bspline(coords, knots, degree = 2L, ...)
coords |
Evaluation locations. For a |
knots |
Knot-line positions on each axis: |
degree |
B-spline degree; must be |
... |
Passed to methods (unused for |
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.
A list with:
dgCMatrix design, n by n_x * n_y
sparse Grams
sparse Gram or NULL if degree < 3
metadata (n_basis = c(n_x, n_y))
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 <- fem_bspline(sites_eval, sites_list, degree = 2L)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 <- fem_bspline(sites_eval, sites_list, degree = 2L)
One-dimensional linear B-spline FEM (mass and stiffness)
fem_bspline_1d(knots, degree = 1L)fem_bspline_1d(knots, degree = 1L)
knots |
Strictly increasing knot positions, including the domain ends. |
degree |
B-spline degree. The Matérn-1/2 term uses degree 1. |
List with open knots, degree, sparse Grams C and G, and
n basis functions.
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).
fem_chol_pattern(Q_structure)fem_chol_pattern(Q_structure)
Q_structure |
Symmetric sparse matrix with the structural nonzeros of
|
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.
Exercises the same chol_update_csc path used by
random_fem_det_*. Useful for diagnosing non-finite joint densities.
fem_logdet_debug(precision, range, sd, log_scale = NULL)fem_logdet_debug(precision, range, sd, log_scale = NULL)
precision |
List from |
range, sd
|
Practical range and field SD (natural or log scale). |
log_scale |
Logical length-1 or length-2: whether |
List with logdet, coef, D, first_bad,
n_bad, n, alpha.
Assemble FEM Matern precision Q2 or Q3 from Grams
fem_precision(kappa, tau, C, G, G2 = NULL, G3 = NULL, alpha = 2L)fem_precision(kappa, tau, C, G, G2 = NULL, G3 = NULL, alpha = 2L)
kappa, tau
|
Positive SPDE parameters. |
C, G, G2, G3
|
Sparse Grams from |
alpha |
|
Sparse dgCMatrix precision.
random_fem_ssq_* / random_fem_det_* precision payload for adlaplaceBuild random_fem_ssq_* / random_fem_det_* precision payload for adlaplace
fem_precision_payload(fem, alpha = 2L)fem_precision_payload(fem, alpha = 2L)
fem |
Result of |
alpha |
|
List for density_data@precision: Grams, chol pattern, and Q CSC
coefficients aligned for on-tape assembly.
Structural nonzero pattern for Q2 or Q3 from Grams
fem_Q_structure(C, G, G2, G3 = NULL)fem_Q_structure(C, G, G2, G3 = NULL)
C, G, G2, G3
|
Sparse Grams from |
Symmetric sparse matrix with the union of Gram nonzero patterns.
Expands hb_knots() breakpoint lines to open knot vectors and builds the
THB embedding (active sets and sparse map S) for a B-spline degree.
hb_basis(knots, degree = 2L) hb_summary(hb)hb_basis(knots, degree = 2L) hb_summary(hb)
knots |
An |
degree |
B-spline degree (must be |
hb |
An |
An "hb_basis" object used by fem_bspline() and matern().
hb_summary(): Per-level active basis counts and total hierarchical dof.
Builds nested knot-line locations from a coarse raster box and optional
inner refinement extents. Knot lines do not depend on B-spline degree;
pass the result to hb_basis() or matern() to form the THB space.
hb_knots(outer, inner = NULL, fact = 2L)hb_knots(outer, inner = NULL, fact = 2L)
outer |
Level-0 box: |
inner |
|
fact |
Integer |
Each element of inner is one refinement level: a single extent, or a list
of extents for sibling boxes at that level. list(ext) and list(list(ext))
are the same one-level, one-box specification. list(ext1, ext2) is two
levels; siblings are list(list(ext1, ext2)). Extents are snapped to the
nested cell grid at that level (spacing outer$resolution / prod(fact[1:i]))
and clipped to the parent level.
An "hb_knots" list, one element per level, each with knots and
rasters (xmin, xmax, ymin, ymax, resolution).
density_data method for hinv_trace_index.
One 0-based index into H_inv@x per stored upper-triangle entry of
Q, in that CSC order. -1L marks a pair the symbolic inverse
does not store. Returns NULL when shard is not a
random_fem_ssq_* density.
## S4 method for signature 'density_data' hinv_trace_index(shard, H_inv)## S4 method for signature 'density_data' hinv_trace_index(shard, H_inv)
shard |
A |
H_inv |
Symbolic upper triangle of the joint inverse Hessian. |
An integer vector, or NULL when shard is not a
random_fem_ssq_* density.
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.
matern_est(fit, eval_grid, n = 0L, cores = 1L)matern_est(fit, eval_grid, n = 0L, cores = 1L)
fit |
An |
eval_grid |
A terra |
n |
Number of posterior simulations of the field. Default |
cores |
Number of PSOCK workers used to build the design matrix.
Each worker evaluates a contiguous block of cells, including
|
A SpatRaster with n + 2 layers: mean, sd, and sim1...simn
when n > 0.
Evaluates the B-spline design A at the geometry column in data and
returns A Q^{-1} A'. Q is the SPDE precision at the practical range
sqrt(8 * nu) / kappa and marginal SD sd, with nu = shape.
matern_fem_covariance(term, data, range, sd)matern_fem_covariance(term, data, range, sd)
term |
A |
data |
Data frame containing the geometry column named in |
range |
Practical Matérn range, in the same units as the coordinates. |
sd |
Marginal standard deviation of the Matérn field. |
A dense n by n covariance matrix, with n = nrow(data).
Formula term for a 2D Matern GRF via tensor-product B-spline FEM, wired to
adlaplaceFem 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_density().
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 design() from data.
matern( x, knots, shape = 1L, init = c(1, 1), lower = c(1e-09, 1e-09), upper = c(Inf, Inf), parscale = c(1, 1), log = TRUE ) ## S4 method for signature 'matern' extra_density(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)matern( x, knots, shape = 1L, init = c(1, 1), lower = c(1e-09, 1e-09), upper = c(Inf, Inf), parscale = c(1, 1), log = TRUE ) ## S4 method for signature 'matern' extra_density(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)
x |
Name of the geometry / coordinate column in |
knots |
Knot lines as |
shape |
Matern smoothness nu ( |
init |
Initial values for |
lower, upper
|
Bounds for |
parscale |
Optimization scales for |
log |
Whether each theta is optimized on the log scale (recycled to
|
term |
A |
data |
Model data frame (passed by adlaplace generics). |
In 2D, B-spline degree and SPDE order alpha equal shape + 1.
A matern term object.
extra_density(matern): Companion parameters density for the FEM
log-determinant shard.
design(matern): Design matrix of B-spline basis evaluations at
coordinates in data[[term@name]].
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 term@log is TRUE).
beta_info(matern): Beta info (none).
mgp termOne-column design given by the normalized Box-Cox transform of
. Default sd = Inf is a flat prior (no precision shard);
a finite boundary_sd uses random_diagonal with known SD.
## S4 method for signature 'mgp_boundary' design(term, data) ## S4 method for signature 'mgp_boundary' precision(term, data) ## S4 method for signature 'mgp_boundary' random_info(term, data)## S4 method for signature 'mgp_boundary' design(term, data) ## S4 method for signature 'mgp_boundary' precision(term, data) ## S4 method for signature 'mgp_boundary' random_info(term, data)
term |
An |
data |
Model data frame. |
design(mgp_boundary): Normalized Box-Cox design column.
precision(mgp_boundary): Diagonal precision, or NULL when sd = Inf.
random_info(mgp_boundary): One random coefficient.
sdPrior SD for the boundary coefficient (Inf = flat).
aCurvature (same as the parent mgp).
shiftInternal shift .
Cubic B-spline FEM for the near-monotone Gaussian process
with
. The null space is spanned by
where is the normalized Box-Cox transform of
; the FEM weights drop the first two bases so
, and the slope column is returned separately as
an mgp_boundary term (like adlaplace::iwp() returning rpoly).
mgp( x, a = 2, knots, ref_value = "left", boundary_sd = Inf, n_quad = 10, init = 0.02, lower = 1e-09, upper = Inf, parscale = 1, log = TRUE ) ## S4 method for signature 'mgp' design(term, data) ## S4 method for signature 'mgp' precision(term, data) ## S4 method for signature 'mgp' theta_info(term) ## S4 method for signature 'mgp' beta_info(term, data) ## S4 method for signature 'mgp' random_info(term, data) ## S4 method for signature 'mgp' extra_density(term) ## S4 method for signature 'mgp' psd(term, h, x = NULL, sd = 1, ...)mgp( x, a = 2, knots, ref_value = "left", boundary_sd = Inf, n_quad = 10, init = 0.02, lower = 1e-09, upper = Inf, parscale = 1, log = TRUE ) ## S4 method for signature 'mgp' design(term, data) ## S4 method for signature 'mgp' precision(term, data) ## S4 method for signature 'mgp' theta_info(term) ## S4 method for signature 'mgp' beta_info(term, data) ## S4 method for signature 'mgp' random_info(term, data) ## S4 method for signature 'mgp' extra_density(term) ## S4 method for signature 'mgp' psd(term, h, x = NULL, sd = 1, ...)
x |
Location(s) on the original scale (default |
a |
Curvature; |
knots |
Vector of knot locations on the original scale (required),
same convention as |
ref_value |
Reference location: |
boundary_sd |
Prior SD for the boundary column ( |
n_quad |
Gauss-Legendre nodes per knot span. |
init, lower, upper, parscale, log
|
SD hyperparameter controls. |
term |
An |
data |
Model data frame. |
h |
Step size(s). |
sd |
SD hyperparameter scale (default 1). |
... |
Unused; for S4 generic compatibility. |
Knot specification matches adlaplace::iwp(): pass original-scale knot
positions (including the domain ends). Knots must be strictly positive. The
reference is snapped to the nearest knot. Bases on each side of ref_value
are built from the knots on that side.
The prior density uses adlaplace's random_mult kernel with a fixed
precision Q (one SD theta). Use adlaplace::psd() /
adlaplace::psd_prior() for a PC prior on the predictive SD.
FEM construction follows Ziang Zhang's BayesGP near-monotone FEM, with
Gauss-Legendre quadrature and splines::splineDesign() (no fda
dependency at runtime).
Named list with the mgp term and its
mgp_boundary companion.
design(mgp): Sparse B-spline design at data[[x]].
precision(mgp): Fixed random_mult precision payload.
theta_info(mgp): One SD theta.
beta_info(mgp): No fixed effects.
random_info(mgp): One row per FEM weight.
extra_density(mgp): No companion log-det density (random_mult includes it).
psd(mgp): Predictive SD via Green's-function quadrature.
aCurvature parameter (Inf gives IWP2: , ).
shiftInternal shift used in centred coordinates.
n_quadGauss-Legendre nodes per knot span.
femCached list from the internal FEM assembler.
two_sidedLogical; both sides of ref_value are active.
adlaplace::psd(), adlaplace::psd_prior(), mgp_boundary
knots <- seq(8, 100, by = 4) mgp_term <- mgp(x = "pm10", a = 2, knots = knots, ref_value = 30) mgp_termknots <- seq(8, 100, by = 4) mgp_term <- mgp(x = "pm10", a = 2, knots = knots, ref_value = 30) mgp_term
Method for adlaplace::replace_vars(). Rewrites the time column, the
exposure column (mult), and the grouping variable (by).
## S4 method for signature 'rsmatern' replace_vars(x, map) ## S4 method for signature 'mgp' replace_vars(x, map) ## S4 method for signature 'mgp_boundary' replace_vars(x, map)## S4 method for signature 'rsmatern' replace_vars(x, map) ## S4 method for signature 'mgp' replace_vars(x, map) ## S4 method for signature 'mgp_boundary' replace_vars(x, map)
x |
An |
map |
Named character vector: names are current column names, values
are replacement column names. Names absent from |
The term with column references and the label updated.
Stationary one-dimensional Matérn field with smoothness 1/2, the continuous
analogue of AR(1), multiplied by (mult - ref_mult). Groups in by share
one (range, sd) and have independent fields. The precision is the linear
B-spline FEM
Q = tau^2 (kappa^2 C + G) with practical range rho = 2/kappa and
tau = 1 / (sd * sqrt(2 * kappa)).
rsmatern( x, mult, by, knots, ref_mult = 0, levels = NULL, init = c(1, 0.05), lower = NULL, upper = NULL, log = TRUE, parscale = NULL ) ## S4 method for signature 'rsmatern' design(term, data) ## S4 method for signature 'rsmatern' precision(term, data) ## S4 method for signature 'rsmatern' random_info(term, data) ## S4 method for signature 'rsmatern' theta_info(term) ## S4 method for signature 'rsmatern' beta_info(term, data) ## S4 method for signature 'rsmatern' extra_density(term)rsmatern( x, mult, by, knots, ref_mult = 0, levels = NULL, init = c(1, 0.05), lower = NULL, upper = NULL, log = TRUE, parscale = NULL ) ## S4 method for signature 'rsmatern' design(term, data) ## S4 method for signature 'rsmatern' precision(term, data) ## S4 method for signature 'rsmatern' random_info(term, data) ## S4 method for signature 'rsmatern' theta_info(term) ## S4 method for signature 'rsmatern' beta_info(term, data) ## S4 method for signature 'rsmatern' extra_density(term)
x |
Time column. |
mult |
Exposure column. The basis row is multiplied by
|
by |
Grouping column. Levels share |
knots |
Increasing knot positions on the time axis. Linear B-splines. |
ref_mult |
Reference exposure (default 0). |
levels |
Optional group levels, in the order used for the blocks. |
init |
Length-2 starting values |
lower, upper
|
Length-2 bounds. The default lower range is the knot spacing. |
log |
Log-transform the two hyperparameters (default |
parscale |
Optional length-2 step scale. |
term |
An |
data |
Model data frame (passed by adlaplace generics). |
An rsmatern term.
design(rsmatern): Design: basis at x, times (mult - ref_mult),
written into the block for by.
precision(rsmatern): Block-diagonal FEM precision, one block per group.
random_info(rsmatern): One metadata row per basis weight and group.
theta_info(rsmatern): Hyperparameters (range, sd).
beta_info(rsmatern): No fixed effects.
extra_density(rsmatern): Log-determinant density random_fem_det_1.
multExposure column multiplied into the basis.
ref_multReference subtracted from mult.
byGrouping variable; one independent field per level.
femCached 1D Grams from fem_bspline_1d().