Define a custom latent non-Gaussian model from a precision "square root".
Source:R/custom.R
ngvb_custom.RdSupply 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.
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)`.