knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

fable.prophet

R build status Codecov test coverage CRAN_Status_Badge lifecycle

This package provides a tidy R interface to the prophet forecasting procedure using fable. This package makes use of the prophet package for R.

Installation

The can install the stable version from CRAN:

install.packages("fable.prophet")

You can install the development version from Github with:

# 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 package.

library(tsibble)
library(dplyr)
cafe <- tsibbledata::aus_retail %>% 
  filter(Industry == "Cafes, restaurants and catering services")
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.

library(fable.prophet)
fit <- cafe %>% 
  model(
    prophet = prophet(Turnover ~ season("year", 4, type = "multiplicative"))
  )
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:

components(fit)
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.

fc <- fit %>% 
  forecast(h = 24)
fc
cafe %>% 
  ggplot(aes(x = Month, y = Turnover, colour = State)) + 
  geom_line() + 
  autolayer(fc)


mitchelloharawild/fable.prophet documentation built on Oct. 17, 2023, 11:22 a.m.