Skip to content

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

julia
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
julia
IID(group)

Formula constructor for IID random effects. Use only inside @formula.

source
GaussianMarkovRandomFields.RandomWalk Function
julia
RandomWalk(order, index)

Formula constructor for RandomWalk random effects (order=1 MVP). Use only inside @formula.

source
GaussianMarkovRandomFields.AR1 Function
julia
AR1(index)

Formula constructor for AR1 random effects. Use only inside @formula.

source
GaussianMarkovRandomFields.Besag Type
julia
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) of W.

  • Calling the functor directly is unsupported outside formula parsing.

source
GaussianMarkovRandomFields.build_formula_components Function
julia
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.

source

See Also