An open API service indexing awesome lists of open source software.

https://github.com/business-science/sweep

Extending broom for time series forecasting
https://github.com/business-science/sweep

broom forecast forecasting-models prediction r-package tidy tidyverse time time-series timeseries

Last synced: about 1 year ago
JSON representation

Extending broom for time series forecasting

Awesome Lists containing this project

README

          

---
output: github_document
---

```{r, echo = FALSE}
knitr::opts_chunk$set(
message = F,
warning = F,
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```

```{r, echo=FALSE}
library(tidyquant)
library(sweep)
```

# sweep

> Extending `broom` to time series forecasting


[![R-CMD-check](https://github.com/business-science/sweep/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/business-science/sweep/actions/workflows/R-CMD-check.yaml)
[![codecov](https://codecov.io/gh/business-science/sweep/branch/master/graph/badge.svg)](https://app.codecov.io/gh/business-science/sweep)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/sweep)](https://cran.r-project.org/package=sweep)
![](http://cranlogs.r-pkg.org/badges/sweep?color=brightgreen)
![](http://cranlogs.r-pkg.org/badges/grand-total/sweep?color=brightgreen)

The `sweep` package extends the `broom` tools (tidy, glance, and augment) for performing forecasts and time series analysis in the "tidyverse". The package is geared towards "tidying" the forecast workflow used with Rob Hyndman's `forecast` package.

## Benefits

* __Designed for modeling and scaling forecasts using the the `tidyverse` tools in [_R for Data Science_](https://r4ds.hadley.nz/)__
* __Extends `broom` for model analysis (ARIMA, ETS, BATS, etc)__
* __Tidies the `forecast` objects for easy plotting and "tidy" data manipulation__
* __Integrates `timetk` to enable dates and datetimes (irregular time series) in the tidied forecast output__

## Tools

The package contains the following elements:

1. __model tidiers__: `sw_tidy`, `sw_glance`, `sw_augment`, `sw_tidy_decomp` functions extend `tidy`, `glance`, and `augment` from the `broom` package specifically for models (`ets()`, `Arima()`, `bats()`, etc) used for forecasting.

2. __forecast tidier__: `sw_sweep` converts a `forecast` object to a tibble that can be easily manipulated in the "tidyverse".

## Making forecasts in the tidyverse

`sweep` enables converting a `forecast` object to `tibble`. The result is ability to use `dplyr`, `tidyr`, and `ggplot` natively to manipulate, analyze and visualize forecasts.

```{r echo=FALSE, out.width='100%'}
knitr::include_graphics("man/figures/forecast.png")
```

## Forecasting multiple time series groups at scale

Often forecasts are required on grouped data to analyse trends in sub-categories. The good news is scaling from one time series to many is easy with the various `sw_` functions in combination with `dplyr` and `purrr`.

```{r echo=FALSE, out.width='100%'}
knitr::include_graphics("man/figures/time_series_groups.png")
```

## Forecasting multiple models for accuracy

A common goal in forecasting is to compare different forecast models against each other. `sweep` helps in this area as well.

```{r echo=FALSE, out.width='100%'}
knitr::include_graphics("man/figures/multiple_models.png")
```

## broom extensions for forecasting

If you are familiar with `broom`, you know how useful it is for retrieving "tidy" format model components. `sweep` extends this benefit to the `forecast` package workflow with the following functions:

* `sw_tidy`: Returns model coefficients (single column)
* `sw_glance`: Returns accuracy statistics (single row)
* `sw_augment`: Returns residuals
* `sw_tidy_decomp`: Returns seasonal decompositions
* `sw_sweep`: Returns tidy forecast outputs.

The compatibility chart is listed below.

```{r, echo = F}
tibble::tribble(
~Object, ~`sw_tidy()`, ~`sw_glance()`, ~`sw_augment()`, ~`sw_tidy_decomp()`, ~`sw_sweep()`,
"ar", "", "", "", "", "",
"arima", "X", "X", "X", "", "",
"Arima", "X", "X", "X", "", "",
"ets", "X", "X", "X", "X", "",
"baggedETS", "", "", "", "", "",
"bats", "X", "X", "X", "X", "",
"tbats", "X", "X", "X", "X", "",
"nnetar", "X", "X", "X", "", "",
"stl", "", "", "", "X", "",
"HoltWinters", "X", "X", "X", "X", "",
"StructTS", "X", "X", "X", "X", "",
"tslm", "X", "X", "X", "", "",
"decompose", "", "", "", "X", "",
"adf.test", "X", "X", "", "", "",
"Box.test", "X", "X", "", "", "",
"kpss.test", "X", "X", "", "", "",
"forecast", "", "", "", "", "X"
) %>%
knitr::kable(caption = "Function Compatibility",
align = c("l", "c", "c", "c", "c", "c"))
```

## Installation

Here's how to get started.

Development version with latest features:

``` {r, eval = FALSE}
# install.packages("remotes")
remotes::install_github("business-science/sweep")
```

## Further Information

The `sweep` package includes several vignettes to help users get up to speed quickly:

* SW00 - Introduction to `sweep`
* SW01 - Forecasting Time Series Groups in the tidyverse
* SW02 - Forecasting Using Multiple Models