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.
rw2(mesh, cyclic = FALSE, ...)
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.
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.
additional arguments (currently unused)
An `ngme_operator` object containing the precision matrix and related components for the RW2 model.
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.
# Non-cyclic RW2 with constraints (default)
rw2_default <- rw2(1:6)
print(rw2_default$K)
#> 6 x 6 sparse Matrix of class "dgCMatrix"
#>
#> [1,] 0.5 1 1 1 1 0.5
#> [2,] 0.5 2 3 4 5 3.0
#> [3,] 1.0 -2 1 . . .
#> [4,] . 1 -2 1 . .
#> [5,] . . 1 -2 1 .
#> [6,] . . . 1 -2 1.0
# Cyclic RW2 (connects first and last locations)
rw2_cyclic <- rw2(1:6, cyclic = TRUE)
print(rw2_cyclic$K)
#> 7 x 7 sparse Matrix of class "dgCMatrix"
#>
#> [1,] . 0.5 1 1 1 1 0.5
#> [2,] 0.5 1.0 -2 1 . . .
#> [3,] 1.0 . 1 -2 1 . .
#> [4,] 1.0 . . 1 -2 1 .
#> [5,] 1.0 . . . 1 -2 1.0
#> [6,] 1.0 1.0 . . . 1 -2.0
#> [7,] 0.5 -2.0 1 . . . 1.0
# Using with unequally spaced locations
locations <- c(0, 1, 3, 6, 10, 15)
rw2_unequal <- rw2(locations)
print(rw2_unequal$K)
#> 6 x 6 sparse Matrix of class "dgCMatrix"
#>
#> [1,] 0.5 1.5 2.5 3.5 4.5 2.5
#> [2,] 0.5 3.0 7.5 14.0 22.5 15.0
#> [3,] 1.0 -2.0 1.0 . . .
#> [4,] . 1.0 -2.0 1.0 . .
#> [5,] . . 1.0 -2.0 1.0 .
#> [6,] . . . 1.0 -2.0 1.0