Skip to contents

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).

Usage

temporal_rw2(
  time_var,
  group_var = NULL,
  cyclic = FALSE,
  shared = TRUE,
  parameterization = c("centered", "noncentered")
)

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. Each group's walk sums to zero over time, so it carries the group's trend and not its level; add a group intercept such as (1 | site) for the levels.

cyclic

Logical; if TRUE, the random walk wraps around (first and last time points are neighbors). Useful for seasonal data.

shared

Logical; if TRUE (default), temporal effect enters both numerator and denominator linear predictors identically.

parameterization

Coordinate the walk is sampled in. "centered" (default) samples the effects themselves; "noncentered" samples z ~ N(0, I) and reaches the effects through sigma * A^-1 z, where A stacks the walk's difference operator on 1'/sqrt(n), so the variance enters the linear predictor through the transform rather than through a quadratic form. The two describe the same model and differ in the geometry the sampler sees: non-centred helps where the variance is shrunk toward zero and leaves a funnel the centred coordinate cannot descend, and can be worse where the data pin the field. Measured on a 500-observation fixture with no temporal signal, an RW1's bulk ESS for tau_temporal goes 409 to 805, while an RW2 picks up 23 divergences of 2000 for no gain, which is why this is an argument rather than a default. With group_var the prior still augments one global constant, so the transform carries the group contrasts as free directions beside the scaled ones and the group levels stay unshrunk, as they are in the centred coordinate.

Value

A ratiod_temporal object

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 = 400, chains = 1, seed = 1)
)
# }