Skip to contents

Use a second-oder random walk with seasonal effects to model a main effect, or use multiple second-order random walks, each with their own seasonal effects, 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.

Usage

RW2_Seas(
  n_seas,
  s = 1,
  sd_slope = 1,
  s_seas = 0,
  sd_seas = 1,
  along = NULL,
  zero_sum = FALSE
)

Arguments

n_seas

Number of seasons

s

Scale for prior for innovations in the random walk. Default is 1.

sd_slope

Standard deviation for initial slope or random walk. Default is 1.

s_seas

Scale for innovations in seasonal effects. Default is 0.

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.

zero_sum

If TRUE, values must sum to 0 within each combination of the 'by' variables. Default is FALSE.

Value

Object of class "bage_prior_rw2seasvary" or "bage_prior_rw2seasfix".

Details

If RW2_Seas() 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 is fixed. However, setting s_seas to a value greater than zero produces seasonal effects that evolve over time.

Mathematical details

When RW2_Seas() is used with a main effect,

$$\beta_j = \alpha_j + \lambda_j$$ $$\alpha_j \sim \text{N}(2 \alpha_{j-1} - \alpha_{j-2}, \tau^2)$$ $$\lambda_j \sim \text{N}(\lambda_{j-n}, \omega^2),$$

and when it is used with an interaction,

$$\beta_{u,v} = \alpha_{u,v} + \lambda_{u,v}$$ $$\alpha_{u,v} \sim \text{N}(2 \alpha_{u,v-1} - \alpha_{u,v-2}, \tau^2),$$ $$\lambda_{u,v} \sim \text{N}(\lambda_{u,v-n}, \omega^2)$$

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;

  • \(u\) denotes position within the 'by' variable(s) of the interaction; and

  • \(n\) is n_seas.

Parameter \(\omega\) has a half-normal prior $$\omega \sim \text{N}^+(0, \text{s\_seas}^2),$$ where s_seas is provided by the user. 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, \text{s}^2),$$ where s is provided by the user.

See also

  • RW2() Second-order random walk, without seasonal effect

  • RW_Seas() Random walk, with seasonal effect

  • priors Overview of priors implemented in bage

  • set_prior() Specify prior for intercept, main effect, or interaction

Examples

RW2_Seas(n_seas = 4)               ## seasonal effects fixed
#>   RW2_Seas(n_seas=4) 
#>     n_seas: 4
#>          s: 1
#>   sd_slope: 1
#>     s_seas: 0
#>    sd_seas: 1
#>      along: NULL
#>   zero_sum: FALSE
RW2_Seas(n_seas = 4, s_seas = 0.5) ## seasonal effects evolve
#>   RW2_Seas(n_seas=4,s_seas=0.5) 
#>     n_seas: 4
#>          s: 1
#>   sd_slope: 1
#>     s_seas: 0.5
#>    sd_seas: 1
#>      along: NULL
#>   zero_sum: FALSE