Introduction to forecasting - Rob J Hyndman

116
Forecasting using 1. Introduction to forecasting OTexts.com/fpp/1/ OTexts.com/fpp/2/3 Forecasting using R 1 Rob J Hyndman

Transcript of Introduction to forecasting - Rob J Hyndman

Page 1: Introduction to forecasting - Rob J Hyndman

Forecasting using

1. Introduction to forecasting

OTexts.com/fpp/1/OTexts.com/fpp/2/3

Forecasting using R 1

Rob J Hyndman

Page 2: Introduction to forecasting - Rob J Hyndman

Outline

1 Introduction

2 Some case studies

3 Time series data

4 Some simple forecasting methods

5 Conclusion

Forecasting using R Introduction 2

Page 3: Introduction to forecasting - Rob J Hyndman

The online environment

Forecasting using R Introduction 3

Page 4: Introduction to forecasting - Rob J Hyndman

Brief bio

Forecasting using R Introduction 4

Director of Monash University’sBusiness & EconomicForecasting Unit

Editor-in-Chief, InternationalJournal of Forecasting

How my forecasting methodology is used:

Pharmaceutical Benefits Scheme

Cancer incidence and mortality

Electricity demand

Ageing population

Fertilizer sales

Page 5: Introduction to forecasting - Rob J Hyndman

Brief bio

Forecasting using R Introduction 4

Director of Monash University’sBusiness & EconomicForecasting Unit

Editor-in-Chief, InternationalJournal of Forecasting

How my forecasting methodology is used:

Pharmaceutical Benefits Scheme

Cancer incidence and mortality

Electricity demand

Ageing population

Fertilizer sales

Page 6: Introduction to forecasting - Rob J Hyndman

Brief bio

Forecasting using R Introduction 4

Director of Monash University’sBusiness & EconomicForecasting Unit

Editor-in-Chief, InternationalJournal of Forecasting

How my forecasting methodology is used:

Pharmaceutical Benefits Scheme

Cancer incidence and mortality

Electricity demand

Ageing population

Fertilizer sales

Page 7: Introduction to forecasting - Rob J Hyndman

Brief bio

Forecasting using R Introduction 4

Director of Monash University’sBusiness & EconomicForecasting Unit

Editor-in-Chief, InternationalJournal of Forecasting

How my forecasting methodology is used:

Pharmaceutical Benefits Scheme

Cancer incidence and mortality

Electricity demand

Ageing population

Fertilizer sales

Page 8: Introduction to forecasting - Rob J Hyndman

Brief bio

Forecasting using R Introduction 4

Director of Monash University’sBusiness & EconomicForecasting Unit

Editor-in-Chief, InternationalJournal of Forecasting

How my forecasting methodology is used:

Pharmaceutical Benefits Scheme

Cancer incidence and mortality

Electricity demand

Ageing population

Fertilizer sales

Page 9: Introduction to forecasting - Rob J Hyndman

Brief bio

Forecasting using R Introduction 4

Director of Monash University’sBusiness & EconomicForecasting Unit

Editor-in-Chief, InternationalJournal of Forecasting

How my forecasting methodology is used:

Pharmaceutical Benefits Scheme

Cancer incidence and mortality

Electricity demand

Ageing population

Fertilizer sales

Page 10: Introduction to forecasting - Rob J Hyndman

Brief bio

Forecasting using R Introduction 4

Director of Monash University’sBusiness & EconomicForecasting Unit

Editor-in-Chief, InternationalJournal of Forecasting

How my forecasting methodology is used:

Pharmaceutical Benefits Scheme

Cancer incidence and mortality

Electricity demand

Ageing population

Fertilizer sales

Page 11: Introduction to forecasting - Rob J Hyndman

Brief bio

Forecasting using R Introduction 4

Director of Monash University’sBusiness & EconomicForecasting Unit

Editor-in-Chief, InternationalJournal of Forecasting

How my forecasting methodology is used:

Pharmaceutical Benefits Scheme

Cancer incidence and mortality

Electricity demand

Ageing population

Fertilizer sales

Page 12: Introduction to forecasting - Rob J Hyndman

Brief bio

Forecasting using R Introduction 4

Director of Monash University’sBusiness & EconomicForecasting Unit

Editor-in-Chief, InternationalJournal of Forecasting

How my forecasting methodology is used:

Pharmaceutical Benefits Scheme

Cancer incidence and mortality

Electricity demand

Ageing population

Fertilizer salesrobjhyndman.com

Page 13: Introduction to forecasting - Rob J Hyndman

Introductions

Please introduce yourself briefly using the chat box(be sure to send your message to everyone).

What is your name?

Where in the world are you?

What time is it there?

Why are you taking this course?

Example

Rob. Melbourne, Australia. 9am. To help me use Rfor forecasting sales for my company.

Forecasting using R Introduction 5

Page 14: Introduction to forecasting - Rob J Hyndman

Introductions

Please introduce yourself briefly using the chat box(be sure to send your message to everyone).

What is your name?

Where in the world are you?

What time is it there?

Why are you taking this course?

Example

Rob. Melbourne, Australia. 9am. To help me use Rfor forecasting sales for my company.

Forecasting using R Introduction 5

Page 15: Introduction to forecasting - Rob J Hyndman

Poll: How experienced are you in forecasting?

1 Guru: I wrote the book, done it for decades,now I do the conference circuit.

2 Expert: It has been my full time job for morethan a decade.

3 Skilled: I have been doing it for years.

4 Comfortable: I understand it and have done it.

5 Learner: I am still learning.

6 Beginner: I have heard of it and would like tolearn more.

7 Unknown: What is forecasting? Is that what theweather people do?

Forecasting using R Introduction 6

Page 16: Introduction to forecasting - Rob J Hyndman

Key reference

Hyndman, R. J. & Athanasopoulos, G.

(2013) Forecasting: principles and

practice.

otexts.com/fpp/

Free and online

Data sets in associated R package

R code for examples

Forecasting using R Introduction 7

Page 17: Introduction to forecasting - Rob J Hyndman

Key reference

Hyndman, R. J. & Athanasopoulos, G.

(2013) Forecasting: principles and

practice.

otexts.com/fpp/

Free and online

Data sets in associated R package

R code for examples

Forecasting using R Introduction 7

Page 18: Introduction to forecasting - Rob J Hyndman

Key reference

Hyndman, R. J. & Athanasopoulos, G.

(2013) Forecasting: principles and

practice.

otexts.com/fpp/

Free and online

Data sets in associated R package

R code for examples

Forecasting using R Introduction 7

Page 19: Introduction to forecasting - Rob J Hyndman

Key reference

Hyndman, R. J. & Athanasopoulos, G.

(2013) Forecasting: principles and

practice.

otexts.com/fpp/

Free and online

Data sets in associated R package

R code for examples

Forecasting using R Introduction 7

Page 20: Introduction to forecasting - Rob J Hyndman

Key reference

Hyndman, R. J. & Athanasopoulos, G.

(2013) Forecasting: principles and

practice.

otexts.com/fpp/

Free and online

Data sets in associated R package

R code for examples

Forecasting using R Introduction 7

Page 21: Introduction to forecasting - Rob J Hyndman

Book recommendation

Forecasting using R Introduction 8

If you need more supporton statistics or R, then Irecommend:

Introductory Statisticswith Rby Peter Dalgaard

Page 22: Introduction to forecasting - Rob J Hyndman

Poll: How proficient are you in using R?

1 Guru: The R core team come to me for advice.2 Expert: I have written several packages on

CRAN.3 Skilled: I use it regularly and it is an important

part of my job.4 Comfortable: I use it often and am comfortable

with the tool.5 User: I use it sometimes, but I am often

searching around for the right function.6 Learner: I have used it a few times.7 Beginner: I’ve managed to download and install

it.8 Unknown: Why are you speaking like a pirate?

Forecasting using R Introduction 9

Page 23: Introduction to forecasting - Rob J Hyndman

Which version of R are you using?

Version: (try getRversion() if you don’t know)

1 R 3.0.0 or higher

2 R 2.15.x

3 R 2.14.x

4 Something older.

Edition

1 Standard R (CRAN)

2 Standard R with RStudio

3 Revolution R: Community, EnterpriseWorkstation or Server

4 Something else?Forecasting using R Introduction 10

Page 24: Introduction to forecasting - Rob J Hyndman

Install required packages

install.packages("fpp", dependencies=TRUE)

Forecasting using R Introduction 11

Page 25: Introduction to forecasting - Rob J Hyndman

Getting help with R# Search for termshelp.search("forecasting")

# Detailed helphelp(forecast)

# Worked examplesexample("forecast.ar")

# Similar namesapropos("forecast")

#Help on packagehelp(package="fpp")

Forecasting using R Introduction 12

Page 26: Introduction to forecasting - Rob J Hyndman

Approximate outline

Week Topic Chapter

1 The forecaster’s toolbox 1,22 Seasonality and trends 63 Exponential smoothing 74 Stationarity, transformations

and differencing2,8

5 ARIMA models 86 Time series cross-validation 26 Dynamic regression 9

Forecasting using R Introduction 13

Page 27: Introduction to forecasting - Rob J Hyndman

Assumptions

This is not an introduction to R. I assume youare broadly comfortable with R code and the Renvironment.

This is not a statistics course. I assume you arefamiliar with concepts such as the mean,standard deviation, quantiles, regression,normal distribution, etc.

This is not a theory course. I am not going toderive anything. I will teach you forecastingtools, when to use them and how to use themmost effectively.

Forecasting using R Introduction 14

Page 28: Introduction to forecasting - Rob J Hyndman

Assumptions

This is not an introduction to R. I assume youare broadly comfortable with R code and the Renvironment.

This is not a statistics course. I assume you arefamiliar with concepts such as the mean,standard deviation, quantiles, regression,normal distribution, etc.

This is not a theory course. I am not going toderive anything. I will teach you forecastingtools, when to use them and how to use themmost effectively.

Forecasting using R Introduction 14

Page 29: Introduction to forecasting - Rob J Hyndman

Assumptions

This is not an introduction to R. I assume youare broadly comfortable with R code and the Renvironment.

This is not a statistics course. I assume you arefamiliar with concepts such as the mean,standard deviation, quantiles, regression,normal distribution, etc.

This is not a theory course. I am not going toderive anything. I will teach you forecastingtools, when to use them and how to use themmost effectively.

Forecasting using R Introduction 14

Page 30: Introduction to forecasting - Rob J Hyndman

Outline

1 Introduction

2 Some case studies

3 Time series data

4 Some simple forecasting methods

5 Conclusion

Forecasting using R Some case studies 15

Page 31: Introduction to forecasting - Rob J Hyndman

CASE STUDY 1: Paperware company

Forecasting using R Some case studies 16

Problem: Want forecasts of each ofhundreds of items. Series can bestationary, trended or seasonal. Theycurrently have a large forecastingprogram written in-house but it doesn’tseem to produce sensible forecasts.They want me to tell them what iswrong and fix it.

Page 32: Introduction to forecasting - Rob J Hyndman

CASE STUDY 1: Paperware company

Forecasting using R Some case studies 16

Problem: Want forecasts of each ofhundreds of items. Series can bestationary, trended or seasonal. Theycurrently have a large forecastingprogram written in-house but it doesn’tseem to produce sensible forecasts.They want me to tell them what iswrong and fix it.

Additional informationProgram written in COBOL making numerical calculationslimited. It is not possible to do any optimisation.

Page 33: Introduction to forecasting - Rob J Hyndman

CASE STUDY 1: Paperware company

Forecasting using R Some case studies 16

Problem: Want forecasts of each ofhundreds of items. Series can bestationary, trended or seasonal. Theycurrently have a large forecastingprogram written in-house but it doesn’tseem to produce sensible forecasts.They want me to tell them what iswrong and fix it.

Additional informationProgram written in COBOL making numerical calculationslimited. It is not possible to do any optimisation.Their programmer has little experience in numericalcomputing.

Page 34: Introduction to forecasting - Rob J Hyndman

CASE STUDY 1: Paperware company

Forecasting using R Some case studies 16

Problem: Want forecasts of each ofhundreds of items. Series can bestationary, trended or seasonal. Theycurrently have a large forecastingprogram written in-house but it doesn’tseem to produce sensible forecasts.They want me to tell them what iswrong and fix it.

Additional informationProgram written in COBOL making numerical calculationslimited. It is not possible to do any optimisation.Their programmer has little experience in numericalcomputing.They employ no statisticians and want the program toproduce forecasts automatically.

Page 35: Introduction to forecasting - Rob J Hyndman

CASE STUDY 1: Paperware company

Methods currently used

A 12 month average

C 6 month average

E straight line regression over last 12 months

G straight line regression over last 6 months

H average slope between last year’s and thisyear’s values.(Equivalent to differencing at lag 12 andtaking mean.)

I Same as H except over 6 months.

K I couldn’t understand the explanation.

Forecasting using R Some case studies 17

Page 36: Introduction to forecasting - Rob J Hyndman

CASE STUDY 2: PBS

Forecasting using R Some case studies 18

Page 37: Introduction to forecasting - Rob J Hyndman

CASE STUDY 2: PBS

The Pharmaceutical Benefits Scheme (PBS) isthe Australian government drugs subsidy scheme.

Many drugs bought from pharmacies aresubsidised to allow more equitable access tomodern drugs.

The cost to government is determined by thenumber and types of drugs purchased.Currently nearly 1% of GDP.

The total cost is budgeted based on forecastsof drug usage.

Forecasting using R Some case studies 19

Page 38: Introduction to forecasting - Rob J Hyndman

CASE STUDY 2: PBS

The Pharmaceutical Benefits Scheme (PBS) isthe Australian government drugs subsidy scheme.

Many drugs bought from pharmacies aresubsidised to allow more equitable access tomodern drugs.

The cost to government is determined by thenumber and types of drugs purchased.Currently nearly 1% of GDP.

The total cost is budgeted based on forecastsof drug usage.

Forecasting using R Some case studies 19

Page 39: Introduction to forecasting - Rob J Hyndman

CASE STUDY 2: PBS

The Pharmaceutical Benefits Scheme (PBS) isthe Australian government drugs subsidy scheme.

Many drugs bought from pharmacies aresubsidised to allow more equitable access tomodern drugs.

The cost to government is determined by thenumber and types of drugs purchased.Currently nearly 1% of GDP.

The total cost is budgeted based on forecastsof drug usage.

Forecasting using R Some case studies 19

Page 40: Introduction to forecasting - Rob J Hyndman

CASE STUDY 2: PBS

The Pharmaceutical Benefits Scheme (PBS) isthe Australian government drugs subsidy scheme.

Many drugs bought from pharmacies aresubsidised to allow more equitable access tomodern drugs.

The cost to government is determined by thenumber and types of drugs purchased.Currently nearly 1% of GDP.

The total cost is budgeted based on forecastsof drug usage.

Forecasting using R Some case studies 19

Page 41: Introduction to forecasting - Rob J Hyndman

CASE STUDY 2: PBS

Forecasting using R Some case studies 20

Page 42: Introduction to forecasting - Rob J Hyndman

CASE STUDY 2: PBS

In 2001: $4.5 billion budget, under-forecastedby $800 million.Thousands of products. Seasonal demand.Subject to covert marketing, volatile products,uncontrollable expenditure.Although monthly data available for 10 years,data are aggregated to annual values, and onlythe first three years are used in estimating theforecasts.All forecasts being done with the FORECASTfunction in MS-Excel!

Problem: How to do the forecasting better?

Forecasting using R Some case studies 21

Page 43: Introduction to forecasting - Rob J Hyndman

CASE STUDY 2: PBS

In 2001: $4.5 billion budget, under-forecastedby $800 million.Thousands of products. Seasonal demand.Subject to covert marketing, volatile products,uncontrollable expenditure.Although monthly data available for 10 years,data are aggregated to annual values, and onlythe first three years are used in estimating theforecasts.All forecasts being done with the FORECASTfunction in MS-Excel!

Problem: How to do the forecasting better?

Forecasting using R Some case studies 21

Page 44: Introduction to forecasting - Rob J Hyndman

CASE STUDY 2: PBS

In 2001: $4.5 billion budget, under-forecastedby $800 million.Thousands of products. Seasonal demand.Subject to covert marketing, volatile products,uncontrollable expenditure.Although monthly data available for 10 years,data are aggregated to annual values, and onlythe first three years are used in estimating theforecasts.All forecasts being done with the FORECASTfunction in MS-Excel!

Problem: How to do the forecasting better?

Forecasting using R Some case studies 21

Page 45: Introduction to forecasting - Rob J Hyndman

CASE STUDY 2: PBS

In 2001: $4.5 billion budget, under-forecastedby $800 million.Thousands of products. Seasonal demand.Subject to covert marketing, volatile products,uncontrollable expenditure.Although monthly data available for 10 years,data are aggregated to annual values, and onlythe first three years are used in estimating theforecasts.All forecasts being done with the FORECASTfunction in MS-Excel!

Problem: How to do the forecasting better?

Forecasting using R Some case studies 21

Page 46: Introduction to forecasting - Rob J Hyndman

CASE STUDY 2: PBS

In 2001: $4.5 billion budget, under-forecastedby $800 million.Thousands of products. Seasonal demand.Subject to covert marketing, volatile products,uncontrollable expenditure.Although monthly data available for 10 years,data are aggregated to annual values, and onlythe first three years are used in estimating theforecasts.All forecasts being done with the FORECASTfunction in MS-Excel!

Problem: How to do the forecasting better?

Forecasting using R Some case studies 21

Page 47: Introduction to forecasting - Rob J Hyndman

CASE STUDY 2: PBS

In 2001: $4.5 billion budget, under-forecastedby $800 million.Thousands of products. Seasonal demand.Subject to covert marketing, volatile products,uncontrollable expenditure.Although monthly data available for 10 years,data are aggregated to annual values, and onlythe first three years are used in estimating theforecasts.All forecasts being done with the FORECASTfunction in MS-Excel!

Problem: How to do the forecasting better?

Forecasting using R Some case studies 21

Page 48: Introduction to forecasting - Rob J Hyndman

CASE STUDY 3: Airline

Forecasting using R Some case studies 22

Page 49: Introduction to forecasting - Rob J Hyndman

CASE STUDY 3: Airline

Forecasting using R Some case studies 23

First class passengers: Melbourne−Sydney

Year

1988 1989 1990 1991 1992 1993

0.0

1.0

2.0

Business class passengers: Melbourne−Sydney

Year

1988 1989 1990 1991 1992 1993

02

46

8

Economy class passengers: Melbourne−Sydney

Year

1988 1989 1990 1991 1992 1993

010

2030

Page 50: Introduction to forecasting - Rob J Hyndman

CASE STUDY 3: Airline

Forecasting using R Some case studies 23

First class passengers: Melbourne−Sydney

Year

1988 1989 1990 1991 1992 1993

0.0

1.0

2.0

Business class passengers: Melbourne−Sydney

Year

1988 1989 1990 1991 1992 1993

02

46

8

Economy class passengers: Melbourne−Sydney

Year

1988 1989 1990 1991 1992 1993

010

2030

Not the real data!Or is it?

Page 51: Introduction to forecasting - Rob J Hyndman

CASE STUDY 3: Airline

Problem: how to forecast passenger traffic onmajor routes.

Additional information

They can provide a large amount of data onprevious routes.

Traffic is affected by school holidays, specialevents such as the Grand Prix, advertisingcampaigns, competition behaviour, etc.

They have a highly capable team of people whoare able to do most of the computing.

Forecasting using R Some case studies 24

Page 52: Introduction to forecasting - Rob J Hyndman

CASE STUDY 3: Airline

Problem: how to forecast passenger traffic onmajor routes.

Additional information

They can provide a large amount of data onprevious routes.

Traffic is affected by school holidays, specialevents such as the Grand Prix, advertisingcampaigns, competition behaviour, etc.

They have a highly capable team of people whoare able to do most of the computing.

Forecasting using R Some case studies 24

Page 53: Introduction to forecasting - Rob J Hyndman

CASE STUDY 3: Airline

Problem: how to forecast passenger traffic onmajor routes.

Additional information

They can provide a large amount of data onprevious routes.

Traffic is affected by school holidays, specialevents such as the Grand Prix, advertisingcampaigns, competition behaviour, etc.

They have a highly capable team of people whoare able to do most of the computing.

Forecasting using R Some case studies 24

Page 54: Introduction to forecasting - Rob J Hyndman

CASE STUDY 3: Airline

Problem: how to forecast passenger traffic onmajor routes.

Additional information

They can provide a large amount of data onprevious routes.

Traffic is affected by school holidays, specialevents such as the Grand Prix, advertisingcampaigns, competition behaviour, etc.

They have a highly capable team of people whoare able to do most of the computing.

Forecasting using R Some case studies 24

Page 55: Introduction to forecasting - Rob J Hyndman

Outline

1 Introduction

2 Some case studies

3 Time series data

4 Some simple forecasting methods

5 Conclusion

Forecasting using R Time series data 25

Page 56: Introduction to forecasting - Rob J Hyndman

Time series data

Time series consist of sequences ofobservations collected over time.We will assume the time periods are equallyspaced.

Time series examples

Daily IBM stock pricesMonthly rainfallAnnual Google profitsQuarterly Australian beer production

Forecasting is estimating how the sequenceof observations will continue into the future.

Forecasting using R Time series data 26

Page 57: Introduction to forecasting - Rob J Hyndman

Time series data

Time series consist of sequences ofobservations collected over time.We will assume the time periods are equallyspaced.

Time series examples

Daily IBM stock pricesMonthly rainfallAnnual Google profitsQuarterly Australian beer production

Forecasting is estimating how the sequenceof observations will continue into the future.

Forecasting using R Time series data 26

Page 58: Introduction to forecasting - Rob J Hyndman

Time series data

Time series consist of sequences ofobservations collected over time.We will assume the time periods are equallyspaced.

Time series examples

Daily IBM stock pricesMonthly rainfallAnnual Google profitsQuarterly Australian beer production

Forecasting is estimating how the sequenceof observations will continue into the future.

Forecasting using R Time series data 26

Page 59: Introduction to forecasting - Rob J Hyndman

Time series data

Time series consist of sequences ofobservations collected over time.We will assume the time periods are equallyspaced.

Time series examples

Daily IBM stock pricesMonthly rainfallAnnual Google profitsQuarterly Australian beer production

Forecasting is estimating how the sequenceof observations will continue into the future.

Forecasting using R Time series data 26

Page 60: Introduction to forecasting - Rob J Hyndman

Time series data

Time series consist of sequences ofobservations collected over time.We will assume the time periods are equallyspaced.

Time series examples

Daily IBM stock pricesMonthly rainfallAnnual Google profitsQuarterly Australian beer production

Forecasting is estimating how the sequenceof observations will continue into the future.

Forecasting using R Time series data 26

Page 61: Introduction to forecasting - Rob J Hyndman

Time series data

Time series consist of sequences ofobservations collected over time.We will assume the time periods are equallyspaced.

Time series examples

Daily IBM stock pricesMonthly rainfallAnnual Google profitsQuarterly Australian beer production

Forecasting is estimating how the sequenceof observations will continue into the future.

Forecasting using R Time series data 26

Page 62: Introduction to forecasting - Rob J Hyndman

Time series data

Time series consist of sequences ofobservations collected over time.We will assume the time periods are equallyspaced.

Time series examples

Daily IBM stock pricesMonthly rainfallAnnual Google profitsQuarterly Australian beer production

Forecasting is estimating how the sequenceof observations will continue into the future.

Forecasting using R Time series data 26

Page 63: Introduction to forecasting - Rob J Hyndman

Time series data

Time series consist of sequences ofobservations collected over time.We will assume the time periods are equallyspaced.

Time series examples

Daily IBM stock pricesMonthly rainfallAnnual Google profitsQuarterly Australian beer production

Forecasting is estimating how the sequenceof observations will continue into the future.

Forecasting using R Time series data 26

Page 64: Introduction to forecasting - Rob J Hyndman

Australian beer production

Forecasting using R Time series data 27

Year

meg

alite

rs

1995 2000 2005 2010

400

450

500

Page 65: Introduction to forecasting - Rob J Hyndman

Looking for stories

Forecasting using R Time series data 28

Page 66: Introduction to forecasting - Rob J Hyndman

Looking for stories that make sense

Forecasting using R Time series data 29

Page 67: Introduction to forecasting - Rob J Hyndman

Think about what you’re doing

Forecasting using R Time series data 30

Page 68: Introduction to forecasting - Rob J Hyndman

Time series in RAustralian GDPausgdp <- ts(scan("gdp.dat"),frequency=4,

start=1971+2/4)Class: tsPrint and plotting methods available.

> ausgdpQtr1 Qtr2 Qtr3 Qtr4

1971 4612 46511972 4645 4615 4645 47221973 4780 4830 4887 49331974 4921 4875 4867 49051975 4938 4934 4942 49791976 5028 5079 5112 51271977 5130 5101 5072 50691978 5100 5166 5244 53121979 5349 5370 5388 53961980 5388 5403 5442 5482

Forecasting using R Time series data 31

Page 69: Introduction to forecasting - Rob J Hyndman

Time series in RAustralian GDPausgdp <- ts(scan("gdp.dat"),frequency=4,

start=1971+2/4)Class: tsPrint and plotting methods available.

> ausgdpQtr1 Qtr2 Qtr3 Qtr4

1971 4612 46511972 4645 4615 4645 47221973 4780 4830 4887 49331974 4921 4875 4867 49051975 4938 4934 4942 49791976 5028 5079 5112 51271977 5130 5101 5072 50691978 5100 5166 5244 53121979 5349 5370 5388 53961980 5388 5403 5442 5482

Forecasting using R Time series data 31

Page 70: Introduction to forecasting - Rob J Hyndman

Time series in RAustralian GDPausgdp <- ts(scan("gdp.dat"),frequency=4,

start=1971+2/4)Class: tsPrint and plotting methods available.

> ausgdpQtr1 Qtr2 Qtr3 Qtr4

1971 4612 46511972 4645 4615 4645 47221973 4780 4830 4887 49331974 4921 4875 4867 49051975 4938 4934 4942 49791976 5028 5079 5112 51271977 5130 5101 5072 50691978 5100 5166 5244 53121979 5349 5370 5388 53961980 5388 5403 5442 5482

Forecasting using R Time series data 31

Page 71: Introduction to forecasting - Rob J Hyndman

Time series in R

Forecasting using R Time series data 32

Time

ausg

dp

1975 1980 1985 1990 19954500

5000

5500

6000

6500

7000

7500 > plot(ausgdp)

Page 72: Introduction to forecasting - Rob J Hyndman

Time series in R

Residential electricity sales

> elecsalesTime Series:Start = 1989End = 2008Frequency = 1[1] 2354.34 2379.71 2318.52 2468.99 2386.09 2569.47[7] 2575.72 2762.72 2844.50 3000.70 3108.10 3357.50

[13] 3075.70 3180.60 3221.60 3176.20 3430.60 3527.48[19] 3637.89 3655.00

Forecasting using R Time series data 33

Page 73: Introduction to forecasting - Rob J Hyndman

Time series in RMain package used in this course> library(fpp)

Forecasting using R Time series data 34

Page 74: Introduction to forecasting - Rob J Hyndman

Time series in RMain package used in this course> library(fpp)This loads:

some data for use in examples and exercisesforecast package (for forecasting functions)tseries package (for a few time seriesfunctions)fma package (for lots of time series data)expsmooth package (for more time seriesdata)lmtest package (for some regressionfunctions)

Forecasting using R Time series data 34

Page 75: Introduction to forecasting - Rob J Hyndman

Time series in RMain package used in this course> library(fpp)This loads:

some data for use in examples and exercisesforecast package (for forecasting functions)tseries package (for a few time seriesfunctions)fma package (for lots of time series data)expsmooth package (for more time seriesdata)lmtest package (for some regressionfunctions)

Forecasting using R Time series data 34

Page 76: Introduction to forecasting - Rob J Hyndman

Time series in RMain package used in this course> library(fpp)This loads:

some data for use in examples and exercisesforecast package (for forecasting functions)tseries package (for a few time seriesfunctions)fma package (for lots of time series data)expsmooth package (for more time seriesdata)lmtest package (for some regressionfunctions)

Forecasting using R Time series data 34

Page 77: Introduction to forecasting - Rob J Hyndman

Time series in RMain package used in this course> library(fpp)This loads:

some data for use in examples and exercisesforecast package (for forecasting functions)tseries package (for a few time seriesfunctions)fma package (for lots of time series data)expsmooth package (for more time seriesdata)lmtest package (for some regressionfunctions)

Forecasting using R Time series data 34

Page 78: Introduction to forecasting - Rob J Hyndman

Time series in RMain package used in this course> library(fpp)This loads:

some data for use in examples and exercisesforecast package (for forecasting functions)tseries package (for a few time seriesfunctions)fma package (for lots of time series data)expsmooth package (for more time seriesdata)lmtest package (for some regressionfunctions)

Forecasting using R Time series data 34

Page 79: Introduction to forecasting - Rob J Hyndman

Time series in RMain package used in this course> library(fpp)This loads:

some data for use in examples and exercisesforecast package (for forecasting functions)tseries package (for a few time seriesfunctions)fma package (for lots of time series data)expsmooth package (for more time seriesdata)lmtest package (for some regressionfunctions)

Forecasting using R Time series data 34

Page 80: Introduction to forecasting - Rob J Hyndman

Outline

1 Introduction

2 Some case studies

3 Time series data

4 Some simple forecasting methods

5 Conclusion

Forecasting using R Some simple forecasting methods 35

Page 81: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Forecasting using R Some simple forecasting methods 36

Australian quarterly beer production

meg

alite

rs

1995 2000 2005

400

450

500

Page 82: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Forecasting using R Some simple forecasting methods 36

Australian quarterly beer production

meg

alite

rs

1995 2000 2005

400

450

500

Can you think of any forecasting methods for these data?

Page 83: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Forecasting using R Some simple forecasting methods 37

Number of pigs slaughtered in Victoria

thou

sand

s

1990 1991 1992 1993 1994 1995

8090

100

110

Page 84: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Forecasting using R Some simple forecasting methods 37

Number of pigs slaughtered in Victoria

thou

sand

s

1990 1991 1992 1993 1994 1995

8090

100

110

How would you forecast these data?

Page 85: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Forecasting using R Some simple forecasting methods 38

Dow Jones index (daily ending 15 Jul 94)

0 50 100 150 200 250

3600

3700

3800

3900

Page 86: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Forecasting using R Some simple forecasting methods 38

Dow Jones index (daily ending 15 Jul 94)

0 50 100 150 200 250

3600

3700

3800

3900

How would you forecast these data?

Page 87: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Average method

Forecast of all future values is equal to mean ofhistorical data {y1, . . . , yT}.Forecasts: yT+h|T = y = (y1 + · · ·+ yT)/T

Naïve method (for time series only)

Forecasts equal to last observed value.Forecasts: yT+h|T = yT.Consequence of efficient market hypothesis.

Seasonal naïve method

Forecasts equal to last value from same season.Forecasts: yT+h|T = yT+h−km where m =seasonal period and k = b(h− 1)/mc+1.

Forecasting using R Some simple forecasting methods 39

Page 88: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Average method

Forecast of all future values is equal to mean ofhistorical data {y1, . . . , yT}.Forecasts: yT+h|T = y = (y1 + · · ·+ yT)/T

Naïve method (for time series only)

Forecasts equal to last observed value.Forecasts: yT+h|T = yT.Consequence of efficient market hypothesis.

Seasonal naïve method

Forecasts equal to last value from same season.Forecasts: yT+h|T = yT+h−km where m =seasonal period and k = b(h− 1)/mc+1.

Forecasting using R Some simple forecasting methods 39

Page 89: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Average method

Forecast of all future values is equal to mean ofhistorical data {y1, . . . , yT}.Forecasts: yT+h|T = y = (y1 + · · ·+ yT)/T

Naïve method (for time series only)

Forecasts equal to last observed value.Forecasts: yT+h|T = yT.Consequence of efficient market hypothesis.

Seasonal naïve method

Forecasts equal to last value from same season.Forecasts: yT+h|T = yT+h−km where m =seasonal period and k = b(h− 1)/mc+1.

Forecasting using R Some simple forecasting methods 39

Page 90: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Average method

Forecast of all future values is equal to mean ofhistorical data {y1, . . . , yT}.Forecasts: yT+h|T = y = (y1 + · · ·+ yT)/T

Naïve method (for time series only)

Forecasts equal to last observed value.Forecasts: yT+h|T = yT.Consequence of efficient market hypothesis.

Seasonal naïve method

Forecasts equal to last value from same season.Forecasts: yT+h|T = yT+h−km where m =seasonal period and k = b(h− 1)/mc+1.

Forecasting using R Some simple forecasting methods 39

Page 91: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Average method

Forecast of all future values is equal to mean ofhistorical data {y1, . . . , yT}.Forecasts: yT+h|T = y = (y1 + · · ·+ yT)/T

Naïve method (for time series only)

Forecasts equal to last observed value.Forecasts: yT+h|T = yT.Consequence of efficient market hypothesis.

Seasonal naïve method

Forecasts equal to last value from same season.Forecasts: yT+h|T = yT+h−km where m =seasonal period and k = b(h− 1)/mc+1.

Forecasting using R Some simple forecasting methods 39

Page 92: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Average method

Forecast of all future values is equal to mean ofhistorical data {y1, . . . , yT}.Forecasts: yT+h|T = y = (y1 + · · ·+ yT)/T

Naïve method (for time series only)

Forecasts equal to last observed value.Forecasts: yT+h|T = yT.Consequence of efficient market hypothesis.

Seasonal naïve method

Forecasts equal to last value from same season.Forecasts: yT+h|T = yT+h−km where m =seasonal period and k = b(h− 1)/mc+1.

Forecasting using R Some simple forecasting methods 39

Page 93: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Average method

Forecast of all future values is equal to mean ofhistorical data {y1, . . . , yT}.Forecasts: yT+h|T = y = (y1 + · · ·+ yT)/T

Naïve method (for time series only)

Forecasts equal to last observed value.Forecasts: yT+h|T = yT.Consequence of efficient market hypothesis.

Seasonal naïve method

Forecasts equal to last value from same season.Forecasts: yT+h|T = yT+h−km where m =seasonal period and k = b(h− 1)/mc+1.

Forecasting using R Some simple forecasting methods 39

Page 94: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Average method

Forecast of all future values is equal to mean ofhistorical data {y1, . . . , yT}.Forecasts: yT+h|T = y = (y1 + · · ·+ yT)/T

Naïve method (for time series only)

Forecasts equal to last observed value.Forecasts: yT+h|T = yT.Consequence of efficient market hypothesis.

Seasonal naïve method

Forecasts equal to last value from same season.Forecasts: yT+h|T = yT+h−km where m =seasonal period and k = b(h− 1)/mc+1.

Forecasting using R Some simple forecasting methods 39

Page 95: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Average method

Forecast of all future values is equal to mean ofhistorical data {y1, . . . , yT}.Forecasts: yT+h|T = y = (y1 + · · ·+ yT)/T

Naïve method (for time series only)

Forecasts equal to last observed value.Forecasts: yT+h|T = yT.Consequence of efficient market hypothesis.

Seasonal naïve method

Forecasts equal to last value from same season.Forecasts: yT+h|T = yT+h−km where m =seasonal period and k = b(h− 1)/mc+1.

Forecasting using R Some simple forecasting methods 39

Page 96: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Forecasting using R Some simple forecasting methods 40

Forecasts for quarterly beer production

1995 2000 2005

400

450

500

Which method is which?

Page 97: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Forecasting using R Some simple forecasting methods 40

Forecasts for quarterly beer production

1995 2000 2005

400

450

500

Mean methodNaive methodSeasonal naive method

Which method is which?

Page 98: Introduction to forecasting - Rob J Hyndman

Drift method

Forecasts equal to last value plus averagechange.

Forecasts:

yT+h|T = yT +h

T − 1

T∑t=2

(yt − yt−1)

= yT +h

T − 1(yT − y1).

Equivalent to extrapolating a line drawnbetween first and last observations.

Forecasting using R Some simple forecasting methods 41

Page 99: Introduction to forecasting - Rob J Hyndman

Drift method

Forecasts equal to last value plus averagechange.

Forecasts:

yT+h|T = yT +h

T − 1

T∑t=2

(yt − yt−1)

= yT +h

T − 1(yT − y1).

Equivalent to extrapolating a line drawnbetween first and last observations.

Forecasting using R Some simple forecasting methods 41

Page 100: Introduction to forecasting - Rob J Hyndman

Drift method

Forecasts equal to last value plus averagechange.

Forecasts:

yT+h|T = yT +h

T − 1

T∑t=2

(yt − yt−1)

= yT +h

T − 1(yT − y1).

Equivalent to extrapolating a line drawnbetween first and last observations.

Forecasting using R Some simple forecasting methods 41

Page 101: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Forecasting using R Some simple forecasting methods 42

Dow Jones Index (daily ending 15 Jul 94)

Day

0 50 100 150 200 250 300

3600

3700

3800

3900

Page 102: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Forecasting using R Some simple forecasting methods 42

Dow Jones Index (daily ending 15 Jul 94)

Day

0 50 100 150 200 250 300

3600

3700

3800

3900

Mean methodNaive methodDrift model

Page 103: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Mean: meanf(x, h=20)

Naive: naive(x, h=20) or rwf(x, h=20)

Seasonal naive: snaive(x, h=20)

Drift: rwf(x, drift=TRUE, h=20)

Forecasting using R Some simple forecasting methods 43

Page 104: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Mean: meanf(x, h=20)

Naive: naive(x, h=20) or rwf(x, h=20)

Seasonal naive: snaive(x, h=20)

Drift: rwf(x, drift=TRUE, h=20)

Forecasting using R Some simple forecasting methods 43

Page 105: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Mean: meanf(x, h=20)

Naive: naive(x, h=20) or rwf(x, h=20)

Seasonal naive: snaive(x, h=20)

Drift: rwf(x, drift=TRUE, h=20)

Forecasting using R Some simple forecasting methods 43

Page 106: Introduction to forecasting - Rob J Hyndman

Some simple forecasting methods

Mean: meanf(x, h=20)

Naive: naive(x, h=20) or rwf(x, h=20)

Seasonal naive: snaive(x, h=20)

Drift: rwf(x, drift=TRUE, h=20)

Forecasting using R Some simple forecasting methods 43

Page 107: Introduction to forecasting - Rob J Hyndman

Outline

1 Introduction

2 Some case studies

3 Time series data

4 Some simple forecasting methods

5 Conclusion

Forecasting using R Conclusion 44

Page 108: Introduction to forecasting - Rob J Hyndman

Piazza

You should all have received an invitation tojoin the Piazza course page.

All discussion and distribution of informationoutside of classes will take place there.

Homework available there.

Session slides available there after eachsession.

Please ask questions there.

If you haven’t received an invitation, or arehaving trouble, please ask James to help([email protected]).

Forecasting using R Conclusion 45

Page 109: Introduction to forecasting - Rob J Hyndman

Piazza

You should all have received an invitation tojoin the Piazza course page.

All discussion and distribution of informationoutside of classes will take place there.

Homework available there.

Session slides available there after eachsession.

Please ask questions there.

If you haven’t received an invitation, or arehaving trouble, please ask James to help([email protected]).

Forecasting using R Conclusion 45

Page 110: Introduction to forecasting - Rob J Hyndman

Piazza

You should all have received an invitation tojoin the Piazza course page.

All discussion and distribution of informationoutside of classes will take place there.

Homework available there.

Session slides available there after eachsession.

Please ask questions there.

If you haven’t received an invitation, or arehaving trouble, please ask James to help([email protected]).

Forecasting using R Conclusion 45

Page 111: Introduction to forecasting - Rob J Hyndman

Piazza

You should all have received an invitation tojoin the Piazza course page.

All discussion and distribution of informationoutside of classes will take place there.

Homework available there.

Session slides available there after eachsession.

Please ask questions there.

If you haven’t received an invitation, or arehaving trouble, please ask James to help([email protected]).

Forecasting using R Conclusion 45

Page 112: Introduction to forecasting - Rob J Hyndman

Piazza

You should all have received an invitation tojoin the Piazza course page.

All discussion and distribution of informationoutside of classes will take place there.

Homework available there.

Session slides available there after eachsession.

Please ask questions there.

If you haven’t received an invitation, or arehaving trouble, please ask James to help([email protected]).

Forecasting using R Conclusion 45

Page 113: Introduction to forecasting - Rob J Hyndman

Piazza

You should all have received an invitation tojoin the Piazza course page.

All discussion and distribution of informationoutside of classes will take place there.

Homework available there.

Session slides available there after eachsession.

Please ask questions there.

If you haven’t received an invitation, or arehaving trouble, please ask James to help([email protected]).

Forecasting using R Conclusion 45

Page 114: Introduction to forecasting - Rob J Hyndman

Homework

There will be a homework sheet available foreach session to give you practice with theconcepts introduced.

Please try to do each sheet before the nextsession.

I will post answers on Piazza.

Forecasting using R Conclusion 46

Page 115: Introduction to forecasting - Rob J Hyndman

Homework

There will be a homework sheet available foreach session to give you practice with theconcepts introduced.

Please try to do each sheet before the nextsession.

I will post answers on Piazza.

Forecasting using R Conclusion 46

Page 116: Introduction to forecasting - Rob J Hyndman

Homework

There will be a homework sheet available foreach session to give you practice with theconcepts introduced.

Please try to do each sheet before the nextsession.

I will post answers on Piazza.

Forecasting using R Conclusion 46