US Dairy Consumption

Time series analysis, Forecasting (sweep package)

Published

January 28, 2019

Notable topics: Time series analysis, Forecasting (sweep package)

Recorded on: 2019-01-28

Timestamps by: Alex Cookson

View code

Screencast

Timestamps

Identifying the need for a gather step

str_to_titlestr_replace_all

Changing snake case to title case using str_to_title and str_replace_all functions

Identifying need for separating categories into major and minor categories (e.g., "Cheese Other" can be divided into "Cheese" and "Other")

separate

Using separate function to split categories into major and minor categories (good explanation of "extra" argument, which merges additional separations into one field)

coalesce

Using coalesce function to deal with NAs resulting from above step

Dealing with graph of minor category that is linked to multiple major categories ("Other" linked to "Cheese" and "Frozen")

fct_lump

Introducing fct_lump function as an approach to work with many categories

facet_wrap

Introducing facetting (facet_wrap function) as second alternative to working with many categories

Dealing with "Other" category having two parts to it by using ifelse function in the cleaning step (e.g., go from "Other" to "Other Cheese")

sweep

Looking at page for the sweep package

tk_ts
sweep

Using tk_ts function to coerce a tibble to a timeseries

Turning year column (numeric) into a date by adding number of years to Jan 1, 0001

Nesting time series object into each combination of category and product

Applying ETS (Error, Trend, Seasonal) model to each time series

sw_glance
sweep

Using sw_glance function (sweep package's version of glance function) to pull out model parameters from model field created in above step

sw_augment

Using sw_augment function to append fitted values and residuals from the model to the original data

Visualising actual and fitted values on the same graph to get a look at the ETS model

Arima

Using Arima function (note the capital A) as alternative to ETS (not sure what difference is between arima and Arima)

sw_sweepforecast
sweep

Forecasting into the future using an ETS model using various functions: unnest, sw_sweep, forecast

geom_ribbon

Using geom_ribbon function to add confidence bounds to forecast

Forecasting using auto-ARIMA (instead of ETS)

crossing

Applying two forecasting methods at the same time (auto-ARIMA and ETS) using the crossing function

invoke

Quick test of how invoke function works (used to call a function easily, e.g., when it is a character string instead of called directly)

scale_linetype_discrete

Removing only one part of legend (line type of solid or dashed) using scale_linetype_discrete function

gather

Using gather function to clean up new dataset

fct_recode

Using fct_recode to fix a typo in a categorical variable

Copy-pasting previous forecasting code to cheese and reviewing any changes needed

shiny

Discussing alternative approach: creating interactive visualisation using shiny package to do direct comparisons