Skip to contents

Supply the dependency matrix `D(theta)` (a function of the hyperparameters INLA fits, on the internal/unconstrained scale) and the constant vector `h`. The field then has conditional precision `Q(V) = D(theta)^T diag(1/V) D(theta)`, and `V = h` recovers the Gaussian model. This turns *any* Gaussian model whose precision factors as `Q = D^T diag(1/h) D` into a latent non-Gaussian one, without hand-writing an rgeneric.

Usage

ngvb_custom(
  D,
  h = 1,
  ntheta,
  theta.initial = rep(0, ntheta),
  logprior = function(theta) sum(stats::dnorm(theta, 0, 3, log = TRUE)),
  rankdef = 0L,
  graph = NULL,
  lognc = NULL
)

Arguments

D

`function(theta)` returning the sparse dependency matrix `D(theta)`, of dimension `m x n` (`m` driving-noise terms, `n` latent nodes).

h

constant vector of length `m` (`E[V] = h`; `1` for discrete models). A scalar is recycled.

ntheta

number of hyperparameters.

theta.initial

initial hyperparameters (internal scale).

logprior

`function(theta)` log prior on the internal scale (including Jacobians). Default: independent `N(0, 3)`.

rankdef

rank deficiency of `D^T D` (0 = proper; positive for intrinsic models such as `m < n` difference operators).

graph

optional sparsity pattern of `D^T D` (the union over `theta`); computed from `D(theta.initial)` if omitted.

lognc

optional analytic normalizing constant `function(theta, Vinv)` returning `0.5 log|Q| - 0.5 n log(2 pi)`. Supply it when the generic determinant is numerically unstable (e.g. `D` near-singular, as in SAR/CAR).

Value

An operator descriptor for use in [ngvb()] / [ng.check()] via `components`, or to build a Gaussian fit via [ngvb_rgeneric()].

Details

Typical workflow: build `op`, fit the Gaussian LGM with `f(idx, model = ngvb_rgeneric(op))`, then call `ng.check(fit, components = ...)` and `ngvb(fit, components = ...)`.

Note

`D`, `logprior` and `lognc` are evaluated in a separate R process where only base packages are attached. Data they reference (matrices, vectors) is captured automatically, but any *non-base function* must be namespace-qualified (e.g. `Matrix::Diagonal`, `stats::dnorm`) or written with base R. For instance use `exp(t)/(1 + exp(t))` rather than `plogis(t)`.

See also

[ngvb()], [ng.check()], [ngvb_rgeneric()]

Examples

# First differences by hand (an RW1), showing the custom interface:
n  <- 8
D0 <- Matrix::bandSparse(n - 1, n, c(0, 1), list(rep(-1, n - 1), rep(1, n - 1)))
op <- ngvb_custom(D = function(theta) sqrt(exp(theta[1])) * D0,
                  h = 1, ntheta = 1, rankdef = 1)
dim(op$Dfunc(0))
#> [1] 7 8