Use a second-oder random walk to model a main effect, or use multiple second-order random walks to model an interaction. A second-order random walk is effectively a random walk with drift where the drift term varies. It is typically used with main effects or interactions that involve time, where there are sustained trends upward or downward.
Arguments
- s
Scale for the prior for the innovations. Default is
1
.- sd_slope
Standard deviation for initial slope. Default is
1
.- along
Name of the variable to be used as the 'along' variable. Only used with interactions.
- zero_sum
If
TRUE
, values must sum to 0 within each combination of the 'by' variables. Default isFALSE
.
Details
If RW2()
is used with an interaction,
separate series are used for
the 'along' variable within
each combination of the
'by' variables.
Argument s
controls the size of innovations in the random walk.
Smaller values for s
tend to give smoother series.
Argument n_seas
controls the number of seasons
.
When using quarterly data, for instance,
n_seas
should be 4
, and when using
monthly data, n_seas
should be 12
.
By default, the magnitude of seasonal effects
can change over time. However, setting s_seas
to 0
produces seasonal effects that are fixed,
eg where "January" effect is the same every year,
the "Feburary" effect is the same every year, and so on.
Mathematical details
When RW()
is used with a main effect,
$$\beta_j = 2 \beta_{j-1} - \beta_{j-2} + \epsilon_j$$ $$\epsilon_j \sim \text{N}(0, \tau^2),$$
and when it is used with an interaction,
$$\beta_{u,v} = 2\beta_{u,v-1} - \beta_{u,v-2} + \epsilon_{u,v}$$ $$\epsilon_{u,v} \sim \text{N}(0, \tau^2),$$
where
\(\pmb{\beta}\) is the main effect or interaction;
\(j\) denotes position within the main effect;
\(v\) denotes position within the 'along' variable of the interaction; and
\(u\) denotes position within the 'by' variable(s) of the interaction.
Parameter \(\tau\)
has a half-normal prior
$$\tau \sim \text{N}^+(0, \text{s}^2),$$
where s
is provided by the user.