fasster: Forecasting with Additive Switching of Seasonality, Trend and Exogenous Regressors

repository·main·Indexed 41 days ago

https://github.com/tidyverts/fasster

An R implementation of the FASSTER model using a state space framework with state switching to capture complex, multiple seasonal patterns in high-frequency time series. Designed for the fable framework, it supports standard R formula conventions, the %S% switch operator for conditional components, and integration with tsibble objects for forecasting and decomposition via components().

Tokens
784
Snippets
5
Records
5
Agent score
50%

What's inside fasster

  1. Use the %S% switch operator for complex patterns

    main

    The %S% operator (the switch operator) allows you to model complex patterns, such as multiple seasonalities, by modeling components for different groups separately and switching between them. This is useful when the seasonal or trend behavior changes based on a condition (e.g., WorkDay vs. Weekend).

    Example of using %S% to switch between seasonal/trend components based on a WorkDay variable:

    # Assuming elec_tr is a tsibble with WorkDay and Temperature columns
    elec_fit <- elec_tr |>
      model(
        fasster = fasster(log(Demand) ~ 
          WorkDay %S% (fourier(48, 16) + trend(1)) + Temperature + I(Temperature^2)
        )
      )
    elec_fit <- elec_tr |
      model(
        fasster = fasster(log(Demand) ~ 
          WorkDay %S% (fourier(48, 16) + trend(1)) + Temperature + I(Temperature^2)
        )
      )
  2. How fasster model specification works

    main

    fasster uses standard R formula conventions to specify model structures. It is designed to work within the fable framework, allowing you to call FASSTER() inside a model() call on a tsibble object.

    To include common state space components, use these convenience functions within your formula:

    • trend(n): Includes an $n$-th order polynomial trend.
    • season(s): Includes a seasonal factor with frequency $s$.
    • fourier(s, q): Includes seasonal Fourier terms of frequency $s$ with $q$ harmonics.
    • arma(ar, ma): Includes an ARMA term where ar and ma are vectors of coefficients.
    • Exogenous regressors: Simply refer to the column name in the formula.

    Example of a basic model with trend and monthly seasonality:

    fit <- as_tsibble(USAccDeaths) |> 
      model(fasster = FASSTER(value ~ trend(1) + fourier(12)))
    fit <- as_tsibble(USAccDeaths) |> 
      model(fasster = FASSTER(value ~ trend(1) + fourier(12)))
  3. Install fasster

    main

    You can install the stable version of fasster from CRAN, or the development version from GitHub.

    Stable version (CRAN):

    install.packages("fasster")

    Development version (GitHub):

    # install.packages("devtools")
    devtools::install_github("tidyverts/fasster")
    install.packages("fasster")
  4. Forecast with FASSTER models

    main

    Because fasster conforms to the fable object structure, you can use the standard forecast() function to generate predictions. You can also use autoplot() to visualize the results.

    Basic forecasting:

    fit |> 
      forecast(h=24) |> 
      autoplot(as_tsibble(USAccDeaths))

    Forecasting with exogenous regressors: If your model specification includes exogenous regressors (like Temperature or WorkDay), you must provide a tsibble containing the future values for those variables using the new_data argument in forecast().

    # elec_ts is a tsibble containing future values for WorkDay and Temperature
    elec_fit |> 
      forecast(new_data = elec_ts) |> 
      autoplot(elec_tr)
    fit |> 
      forecast(h=24) |> 
      autoplot(as_tsibble(USAccDeaths))
  5. Decompose a FASSTER model using components()

    main

    You can extract the underlying states (such as trends and seasonalities) from a fitted FASSTER model using the components() function. This provides a breakdown of how the model is aggregating its components over time.

    fit |> components()
    fit |> 
      components()