-
Notifications
You must be signed in to change notification settings - Fork 8
/
README.Rmd
103 lines (82 loc) · 3.59 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# fable.prophet
[![R build status](https://github.com/mitchelloharawild/fable.prophet/workflows/R-CMD-check/badge.svg)](https://github.com/mitchelloharawild/fable.prophet)
[![Codecov test coverage](https://codecov.io/gh/mitchelloharawild/fable.prophet/branch/master/graph/badge.svg)](https://codecov.io/gh/mitchelloharawild/fable.prophet?branch=master)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/fable.prophet)](https://cran.r-project.org/package=fable.prophet)
[![lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
This package provides a tidy R interface to the prophet forecasting procedure using [fable](https://github.com/tidyverts/fable). This package makes use of the [prophet package](https://cran.r-project.org/package=prophet) for R.
## Installation
The can install the **stable** version from [CRAN](https://cran.r-project.org/package=fable.prophet):
```{r cran-installation, eval = FALSE}
install.packages("fable.prophet")
```
You can install the **development** version from [Github](https://github.com/mitchelloharawild/fable.prophet) with:
```{r gh-installation, eval = FALSE}
# install.packages("remotes")
remotes::install_github("mitchelloharawild/fable.prophet")
```
## Example
Suppose we wanted to model Australia's monthly turnover for cafes, restaurants and catering services. The data is available from the Australian Bureau of Statistics catalogue 8501.0, and in the [tsibbledata](https://github.com/tidyverts/tsibbledata) package.
```{r data, message=FALSE}
library(tsibble)
library(dplyr)
cafe <- tsibbledata::aus_retail %>%
filter(Industry == "Cafes, restaurants and catering services")
```
```{r plot, echo = FALSE}
library(ggplot2)
cafe %>%
ggplot(aes(x = Month, y = Turnover, colour = State)) +
geom_line() +
ylab("Turnover (millions $AUD)")
```
Each series generally exhibits an increasing trend with an annual seasonal pattern that varies proportionally to the level of the series. At a monthly level, any holiday effects can be modelled using a seasonal term. A piecewise linear trend is included by default, and so it is not included in the model specification below.
```{r model}
library(fable.prophet)
fit <- cafe %>%
model(
prophet = prophet(Turnover ~ season("year", 4, type = "multiplicative"))
)
```
```{r mable}
fit
```
The above output confirms that this Prophet model has been fitted to each of the time series. Components from this model can be extracted:
```{r components}
components(fit)
```
```{r components-plot, echo = FALSE, message = FALSE}
library(lubridate)
components(fit) %>%
ggplot(aes(x = Month, y = trend, colour = State)) +
geom_line()
components(fit) %>%
ggplot(aes(x = month(Month), y = year,
colour = State, group = interaction(year(Month), State))) +
geom_line() +
scale_x_continuous(breaks = 1:12, labels = month.abb) +
xlab("Month")
```
Note that the annual seasonal pattern does not change very quickly, although it does differ slightly between years. A very differently seasonal pattern can be seen for the Northern Territory. We can also produce forecasts for each of these series over the next two years.
```{r forecast}
fc <- fit %>%
forecast(h = 24)
```
```{r fable, echo = FALSE}
fc
cafe %>%
ggplot(aes(x = Month, y = Turnover, colour = State)) +
geom_line() +
autolayer(fc)
```