rw2: Random Walk Model of Order 2 (RW2)

View source: R/models.R

rw2R Documentation

Random Walk Model of Order 2 (RW2)

Description

Constructs a second-order random walk model for spatial or temporal processes. The RW2 model assumes that second-order differences \Delta^2 W_i = W_i - 2W_{i-1} + W_{i-2} are independent and identically distributed Gaussian variables.

Usage

rw2(mesh = NULL, cyclic = FALSE, constr = TRUE)

Arguments

mesh

numerical vector or inla.mesh.1d object, locations to build the mesh. For numerical vectors, assumes equally spaced locations. Must have at least 3 locations.

cyclic

logical, whether the mesh is circular. If TRUE, the first and last locations are treated as neighbors with second-order differences computed across the boundary.

constr

logical, whether to enforce the sum-to-zero constraint \sum_{i=1}^n h_i W_i = 0 and the linear trend constraint \sum_{i=1}^n (\sum_{j=1}^i h_j - h_1) \cdot W_i = 0. If FALSE, fixes the first and second elements W_1 = 0 and W_2 = 0.

Details

The RW2 model is defined by the precision matrix K that penalizes second-order differences, making it smoother than RW1. The model enforces constraints to ensure identifiability:

**Non-cyclic (default)**: The first two rows of K enforce constraints: the first row implements \sum_{i=1}^n h_i W_i = 0 (sum-to-zero), and the second row implements \sum_{i=1}^n h_i \cdot i \cdot W_i = 0 (removes linear trend). The remaining rows penalize second-order differences.

**Cyclic**: Treats the domain as circular, connecting the first and last locations as neighbors. The constraint \sum_{i=1}^n h_i W_i = 0 is enforced. The precision matrix is expanded to size (n+1) \times (n+1) to handle the constraint and circular structure properly.

Value

An 'ngme_operator' object containing the precision matrix and related components for the RW2 model.

Examples

# Non-cyclic RW2 with constraints (default)
rw2_default <- rw2(1:6)
print(rw2_default$K)

# Cyclic RW2 (connects first and last locations)
rw2_cyclic <- rw2(1:6, cyclic = TRUE)
print(rw2_cyclic$K)

# Using with unequally spaced locations
locations <- c(0, 1, 3, 6, 10, 15)
rw2_unequal <- rw2(locations)
print(rw2_unequal$K)

ngme2 documentation built on May 20, 2026, 9:10 a.m.