Formula Interface
The formula interface lets you compose models succinctly using StatsModels.jl
syntax. It maps terms on the right-hand side to latent components and assembles a sparse design matrix that connects observations to the combined latent field. To use the formula syntax, load StatsModels
first to activate the corresponding package extension.
Quick Example
using GaussianMarkovRandomFields, StatsModels, Distributions, SparseArrays
# Suppose W is a spatial adjacency for regions; y are counts with an offset
besag = Besag(W) # structured spatial effect
f = @formula(y ~ 1 + x + IID(region) + besag(region))
comp = build_formula_components(f, data; family = Poisson)
lik = comp.obs_model(data.y; offset = data.logE)
prior = comp.combined_model(; τ_besag=1.0, τ_iid=1.0)
post = gaussian_approximation(prior, lik)
Terms and Builders
GaussianMarkovRandomFields.IID Function
IID(group)
Formula constructor for IID random effects. Use only inside @formula
.
GaussianMarkovRandomFields.RandomWalk Function
RandomWalk(order, index)
Formula constructor for RandomWalk random effects (order=1 MVP). Use only inside @formula
.
GaussianMarkovRandomFields.AR1 Function
AR1(index)
Formula constructor for AR1 random effects. Use only inside @formula
.
GaussianMarkovRandomFields.Besag Type
Besag(W; id_to_node = nothing, normalize_var = true, singleton_policy = :gaussian)
Formula functor for Besag (intrinsic CAR) random effects.
Usage:
Create a functor instance:
besag = Besag(W)
With string/categorical region IDs:
besag = Besag(W; id_to_node = Dict("WesternIsles" => 11, ...))
Use in a formula:
@formula(y ~ 0 + besag(region))
Notes
id_to_node
maps arbitrary region identifiers to integer node indices (1-based) ofW
.Calling the functor directly is unsupported outside formula parsing.
GaussianMarkovRandomFields.build_formula_components Function
build_formula_components(formula, data; kwargs...)
Placeholder function for the formula interface. A concrete method is provided by the GaussianMarkovRandomFieldsFormula
extension when StatsModels
is loaded.
See Also
- The BYM + fixed effects Poisson tutorial shows this in practice: Advanced GMRF modelling for disease mapping