| fit_spde | R Documentation |
Fits a GLM with a Matern spatial field via the SPDE approach. Uses CHOLMOD sparse solver with optional nested Laplace for hyperparameter integration.
fit_spde(
y,
X,
spatial,
family = "binomial",
n_trials = NULL,
range = NULL,
sigma = NULL,
nested_laplace = is.null(range) || is.null(sigma),
phi = 1,
offset = NULL,
re_idx = NULL,
n_re_groups = 0L,
sigma_re = 1,
mode = c("laplace", "nuts"),
hyperprior = c("proper", "flat"),
control = list()
)
y |
Integer response vector. |
X |
Design matrix. |
spatial |
A |
family |
Distribution family: |
n_trials |
Integer vector of trial sizes (binomial only). |
range |
Spatial range parameter. If NULL, uses nested Laplace to integrate over range and sigma. |
sigma |
Marginal standard deviation. If NULL, uses nested Laplace. |
nested_laplace |
Logical. If TRUE (default when range/sigma are NULL), use nested Laplace approximation over hyperparameters. |
phi |
Dispersion passed to the family, held fixed. One convention at
every door: for Defaulted, it conditions at 1 and says so with a warning, since for a
family that reads a dispersion that is a modelling choice rather than a
neutral value. |
offset |
Optional fixed additive term on the linear predictor
( |
re_idx, n_re_groups, sigma_re |
Optional single iid random-intercept
|
mode |
Inference method (the method is an argument, not a parallel
verb): |
hyperprior |
|
control |
A named list of numerical / tuning knobs (statistical arguments stay in the signature above). Recognized entries:
|
A list with:
mode: mode of the latent field (beta + mesh node effects)
log_marginal: log marginal likelihood
converged: convergence flag
spatial: the spatial specification (for prediction)
pareto_k, pareto_k_is_ess: outer Pareto-\hat{k} and its
importance-sampling ESS (NA when diagnose_k = FALSE)
pareto_k_proposal_source, pareto_k_first_pass: which proposal
family the reported k-hat came from (several candidates are scored and
the best kept), and the k-hat of the first pass – the proposal exactly
as placed, before refinement. A large gap says the placement is poor
even where the verdict is fine.
nested: nested Laplace results (if used)
Lindgren, Rue & Lindstrom (2011). An explicit link between Gaussian fields and Gaussian Markov random fields: the stochastic partial differential equation approach. JRSS-B 73(4):423-498. Rue, Martino & Chopin (2009). Approximate Bayesian inference for latent Gaussian models by using integrated nested Laplace approximations. JRSS-B 71(2):319-392.
if (requireNamespace("fmesher", quietly = TRUE)) {
set.seed(1)
n <- 200L
coords <- cbind(runif(n), runif(n))
mesh <- fmesher::fm_mesh_2d(loc = coords, max.edge = c(0.15, 0.4), cutoff = 0.05)
fem <- fmesher::fm_fem(mesh)
A <- as(fmesher::fm_basis(mesh, loc = coords), "CsparseMatrix")
spec <- spatial_spde_custom(C = fem$c0, G = fem$g1, A = A, nu = 1,
prior_range = c(0.3, 0.5), prior_sigma = c(0.6, 0.05))
w <- as.numeric(rnorm(spec$n_mesh, 0, 0.6)); w <- w - mean(w)
x <- rnorm(n)
y <- rpois(n, exp(2.0 + 0.5 * x + as.numeric(spec$A %*% w)))
fit <- fit_spde(y = y, X = cbind(1, x), spatial = spec, family = "poisson")
fit$nested$range_mean
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.