View source: R/temporal_core.R
| temporal_rw2 | R Documentation |
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).
temporal_rw2(time_var, group_var = NULL, cyclic = FALSE, shared = NULL)
time_var |
A formula ( |
group_var |
Optional formula ( |
cyclic |
Logical. If |
shared |
Whether the temporal effect is shared across processes in a
multi-process model. |
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).
A tulpa_temporal object
# Create temporal RW2 specification
temporal_rw2("year")
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.