Use a second-oder random walk with seasonal effects as a model for a main effect, or use multiple second-order random walks, each with their own seasonal effects, as a model for an interaction. Typically used with temrs that involve time.
Usage
RW2_Seas(
n_seas,
s_seas,
s = 1,
sd = 1,
sd_slope = 1,
sd_seas = 1,
along = NULL,
con = c("none", "by")
)Arguments
- n_seas
Number of seasons
- s_seas
Scale for innovations in seasonal effects. Can be
0. When greater than 0, seasonal effects vary from year to year.- s
Scale for prior for innovations in random walk. Default is
1.- sd
Standard deviation of initial value. Default is
1. Can be0.- sd_slope
Standard deviation for initial slope of random walk. Default is
1.- sd_seas
Standard deviation for initial values of seasonal effects. Default is
1.- along
Name of the variable to be used as the 'along' variable. Only used with interactions.
- con
Constraints on parameters. Current choices are
"none"and"by". Default is"none". See below for details.
Value
Object of class
"bage_prior_rw2randomseasvary",
"bage_prior_rw2randomseasfix",
"bage_prior_rw2zeroseasvary", or
"bage_prior_rw2zeroseasfix".
Details
If RW2_Seas() is used with an interaction,
a separate series is constructed within each
combination of the 'by' variables.
Argument s controls the size of innovations in the random walk.
Smaller values for s tend to produce smoother series.
Argument n_seas controls the number of seasons.
When using quarterly data, for instance,
n_seas should be 4.
Setting s_seas to 0 produces
seasonal effects that are the same
each year. Setting s_seas to a value
greater than 0 produces seasonal effects
that evolve over time.
Mathematical details
When RW2_Seas() is used with a main effect,
$$\beta_j = \alpha_j + \lambda_j, \quad j = 1, \cdots, J$$ $$\alpha_1 \sim \text{N}(0, \mathtt{sd}^2)$$ $$\alpha_2 \sim \text{N}(0, \mathtt{sd\_slope}^2)$$ $$\alpha_j \sim \text{N}(2 \alpha_{j-1} - \alpha_{j-2}, \tau^2), \quad j = 3, \cdots, J$$ $$\lambda_j \sim \text{N}(0, \mathtt{sd\_seas}^2), \quad j = 1, \cdots, \mathtt{n\_seas} - 1$$ $$\lambda_j = -\sum_{s=1}^{\mathtt{n\_seas} - 1} \lambda_{j - s}, \quad j = \mathtt{n\_seas}, 2 \mathtt{n\_seas}, \cdots$$ $$\lambda_j \sim \text{N}(\lambda_{j-\mathtt{n\_seas}}, \omega^2), \quad \text{otherwise},$$
and when it is used with an interaction,
$$\beta_{u,v} = \alpha_{u,v} + \lambda_{u,v}, \quad v = 1, \cdots, V$$ $$\alpha_{u,1} \sim \text{N}(0, \mathtt{sd}^2)$$ $$\alpha_{u,2} \sim \text{N}(0, \mathtt{sd\_slope}^2)$$ $$\alpha_{u,v} \sim \text{N}(2 \alpha_{u,v-1} - \alpha_{u,v-2}, \tau^2), \quad v = 3, \cdots, V$$ $$\lambda_{u,v} \sim \text{N}(0, \mathtt{sd\_seas}^2), \quad v = 1, \cdots, \mathtt{n\_seas} - 1$$ $$\lambda_{u,v} = -\sum_{s=1}^{\mathtt{n\_seas} - 1} \lambda_{u,v - s}, \quad v = \mathtt{n\_seas}, 2 \mathtt{n\_seas}, \cdots$$ $$\lambda_{u,v} \sim \text{N}(\lambda_{u,v-\mathtt{n\_seas}}, \omega^2), \quad \text{otherwise},$$
where
\(\pmb{\beta}\) is the main effect or interaction;
\(\alpha_j\) or \(\alpha_{u,v}\) is an element of the random walk;
\(\lambda_j\) or \(\lambda_{u,v}\) is an element of the seasonal effect;
\(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 \(\omega\) has a half-normal prior
$$\omega \sim \text{N}^+(0, \mathtt{s\_seas}^2)$$.
If s_seas is set to 0, then \(\omega\) is 0,
and the seasonal effects are fixed over time.
Parameter \(\tau\) has a half-normal prior $$\tau \sim \text{N}^+(0, \mathtt{s}^2)$$.
Constraints
The specification of
constraints is likely to change in future versions of bage.
With some combinations of terms and priors, the values of
the intercept, main effects, and interactions are
are only weakly identified.
For instance, it may be possible to increase the value of the
intercept and reduce the value of the remaining terms in
the model with no effect on predicted rates and only a tiny
effect on prior probabilities. This weak identifiability is
typically harmless. However, in some applications, such as
when trying to obtain interpretable values
for main effects and interactions, it can be helpful to increase
identifiability through the use of constraints, specified through the
con argument.
Current options for con are:
"none"No constraints. The default."by"Only used in interaction terms that include 'along' and 'by' dimensions. Within each value of the 'along' dimension, terms across each 'by' dimension are constrained to sum to 0.
See also
RW2()Second-order random walk without seasonal effectRW_Seas()Random walk with seasonal effectpriors Overview of priors implemented in bage
set_prior()Specify prior for intercept, main effect, or interactionMathematical Details vignette
Examples
## seasonal effects fixed
RW2_Seas(n_seas = 4, s_seas = 0)
#> RW2_Seas(n_seas=4,s_seas=0)
#> n_seas: 4
#> s_seas: 0
#> s: 1
#> sd: 1
#> sd_slope: 1
#> sd_seas: 1
#> along: NULL
#> con: none
## seasonal effects evolve
RW2_Seas(n_seas = 4, s_seas = 1)
#> RW2_Seas(n_seas=4,s_seas=1)
#> n_seas: 4
#> s_seas: 1
#> s: 1
#> sd: 1
#> sd_slope: 1
#> sd_seas: 1
#> along: NULL
#> con: none
## first term in random walk fixed at 0
RW2_Seas(n_seas = 4, s_seas = 1, sd = 0)
#> RW2_Seas(n_seas=4,s_seas=1,sd=0)
#> n_seas: 4
#> s_seas: 1
#> s: 1
#> sd: NULL
#> sd_slope: 1
#> sd_seas: 1
#> along: NULL
#> con: none