Specify a second-order random walk temporal random effect.
RW2 penalizes deviations from linearity, resulting in smoother trends
than RW1: phi[t] - 2*phi[t-1] + phi[t-2] ~ N(0, sigma^2).
Arguments
- time_var
Name of the time variable in data. Can be a formula (e.g.,
~ year) or a character string (e.g.,"year").- group_var
Optional name of grouping variable for panel data. If provided, separate random walks are estimated for each group.
- cyclic
Logical; if TRUE, the random walk wraps around (first and last time points are neighbors). Useful for seasonal data.
Logical; if TRUE (default), temporal effect enters both numerator and denominator linear predictors identically.
Details
RW2 produces smoother curves than RW1 because it penalizes the second derivative (curvature) rather than the first derivative (slope). It requires at least 3 time points.
The precision matrix is rank T-2 (two constraints needed).
Examples
# Create temporal RW2 specification
temporal_rw2("year")
#> tulpaRatio temporal specification
#> ============================
#>
#> Type: RW2 (Second-order Random Walk)
#> Time variable: year
#> Shared: Yes (enters both processes)
# \donttest{
# Smooth temporal trend
set.seed(126)
df <- data.frame(
year = rep(2008:2027, each = 2),
x = rnorm(40),
count = rpois(40, lambda = 25),
effort = rgamma(40, shape = 4, rate = 1)
)
fit <- tratio(
count | effort ~ x,
data = df,
family = ratiod_poisson_gamma(),
temporal = temporal_rw2("year"),
mode = "hmc",
control = list(iter = 200, warmup = 100, chains = 1)
)
#> Inference: Exact (Tier 1)
#> Backend: hmc
#> Fitting ratio model...
#> Family: poisson_gamma
#> Observations: 40
#> Iterations: 200 (warmup: 100)
#> Temporal: rw2 (20 time points)
#> Running NUTS sampler...
#> Parameters: 26
#> Iterations: 200 (warmup: 100)
#> Chains: 1 (cores: 1)
#> Temporal: rw2 (20 time points)
# }