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
A formula (
~ time) or single character string naming the time variable in the data.- group_var
Optional formula (
~ g) or character string naming a grouping variable. When supplied, a separate random walk is fit per group;NULL(default) fits a single walk shared across all observations.- cyclic
Logical. If
TRUE, the random walk wraps around so the last time point is a neighbour of the first (cyclic boundary, e.g. month of year). DefaultFALSE.Whether the temporal effect is shared across processes in a multi-process model.
NULL(default) shares the effect;FALSEfits process-specific effects and emits a warning about unshared confounding.
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")
# \donttest{
# Smooth temporal trend
set.seed(126)
df <- data.frame(
year = rep(1:20, each = 4),
x = rnorm(80)
)
trend <- sin(seq(0, 2, length.out = 20))
df$count <- rpois(80, exp(1 + 0.3 * df$x + trend[df$year]))
fit <- tulpa(
count ~ x,
data = df,
family = "poisson",
temporal = temporal_rw2("year"),
mode = "auto"
)
summary(fit)
# }