Top Banner
FORECASTING USING R Welcome to Forecasting Using R Rob Hyndman Author, forecast
22

Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Jul 28, 2018

Download

Documents

vuongdiep
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

FORECASTING USING R

Welcome to Forecasting Using R

Rob Hyndman Author, forecast

Page 2: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Forecasting Using R

What you will learn● Exploring and visualizing time series

● Simple benchmark methods for forecasting

● Exponential smoothing and ARIMA models

● Advanced forecasting methods

● Measuring forecast accuracy

● Choosing the best method

Page 3: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Forecasting Using R

Course textbook

● Free and online at OTexts.org/fpp2/

● Data sets in associated R package fpp2

● R code for all examples

Hyndman, R. J. & Athanasopoulos, G. (2017)

Forecasting: principles and practice, 2nd edition

Page 4: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Forecasting Using R

Time series data● Series of data observed over time

● Eg.: Daily IBM stock prices, monthly rainfall in London,…

Forecasting is estimating how the sequence of observations will continue into the future.

Page 5: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Forecasting Using R

Forecasts of monthly Australian expenditure on eating out

● What forecasting methods are available that take account of trend, seasonality and other features of the data?

● How to measure the accuracy of your forecasts?

● How to choose a good forecasting model?

Page 6: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

FORECASTING USING R

Let’s practice!

Page 7: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

FORECASTING USING R

Trends, seasonality, and cyclicity

Page 8: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Forecasting Using R

Time series pa!ernsPa!ern Description

Trend A pa!ern exists involving a long-term increase OR decrease in the data

Seasonal A periodic pa!ern exists due to the calendar (e.g. the quarter, month, or day of the week)

CyclicA pa!ern exists where the data exhibits rises and falls that are

not of fixed period (duration usually of at least 2 years)

Page 9: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Forecasting Using R

Examples of time series pa!erns

Page 10: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Forecasting Using R

Examples of time series pa!erns

Page 11: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Forecasting Using R

Examples of time series pa!erns

Page 12: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Forecasting Using R

Examples of time series pa!erns

Page 13: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Forecasting Using R

Seasonal or cyclic?Differences between seasonal and cyclic pa"erns:

● Seasonal pa!ern constant length vs. cyclic pa!ern variable length

● Average length of cycle longer than length of seasonal pa!ern

● Magnitude of cycle more variable than magnitude of seasonal pa!ern

The timing of peaks and troughs is predictable with seasonal data, but unpredictable in the long term with cyclic data.

Page 14: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

FORECASTING USING R

Let’s practice!

Page 15: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

FORECASTING USING R

White noise

Page 16: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Forecasting Using R

White noise> set.seed(3) # Reproducibility > wn <- ts(rnorm(36)) # White noise > autoplot(wn) # Plot!

"White noise" is just a time series of iid data

Page 17: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Forecasting Using R

White noise ACF> ggAcf(wn) + ggtitle("Sample ACF for white noise")

Expectation: each autocorrelation is close to zero

95% of all autocorrelations for white noise should lie within the blue lines

If not: series is probably not white noise

Page 18: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Forecasting Using R

Example: Pigs slaughtered

> autoplot(pigs/1000) + > xlab("Year") + > ylab("thousands") + > ggtitle("Monthly number of pigs slaughtered in Victoria")

Page 19: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Forecasting Using R

Example: Pigs slaughtered

> ggAcf(pigs) + > ggtitle("ACF of monthly pigs slaughtered in Victoria")

significant autocorrelation at lags 1, 2, and 3

Not a white noise series: there is info in the data that can be used to forecast future values

Page 20: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Forecasting Using R

Ljung-Box test

> Box.test(pigs, lag = 24, fitdf = 0, type = "Lj")

Box-Ljung test

data: pigs X-squared = 634.15, df = 24, p-value < 2.2e-16

The Ljung-Box test considers the first h autocorrelation values together. A significant test (small p-value) indicates the data are probably not white noise.

Page 21: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

Forecasting Using R

White noise summary● White noise is a time series that is purely random

● We can test for white noise by looking at an ACF plot or by doing a Ljung-Box test

Page 22: Welcome to Forecasting Using R - Amazon S3 · Forecasting Using R Time series data Series of data observed over time Eg.: Daily IBM stock prices, monthly rainfall in London,… Forecasting

FORECASTING USING R

Let’s practice!