forecasting using r - rob j. hyndman · class: “ts ” print and ... ## 1992 6362 6389 6433 6491...

64
Forecasting using R 1 Forecasting using R Rob J Hyndman 1.1 Time series graphics

Upload: others

Post on 25-Mar-2021

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Forecasting using R 1

Forecasting using R

Rob J Hyndman

11 Time series graphics

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Time series in R 2

Time series in R

Time series consist of sequences of observationscollected over timeWe will assume the time periods are equally spaced

Time series examplesDaily IBM stock pricesMonthly rainfallAnnual Google profitsQuarterly Australian beer production

Forecasting using R Time series in R 3

Time series in R

ausgdp lt- ts(scan(gdpdat)frequency=4start=1971+24)

Class ldquotsrdquoPrint and plotting methods available

ausgdp

Qtr1 Qtr2 Qtr3 Qtr4 1971 4612 4651 1972 4645 4615 4645 4722 1973 4780 4830 4887 4933 1974 4921 4875 4867 4905 1975 4938 4934 4942 4979 1976 5028 5079 5112 5127 1977 5130 5101 5072 5069 1978 5100 5166 5244 5312 1979 5349 5370 5388 5396 1980 5388 5403 5442 5482 1981 5506 5531 5560 5583 1982 5568 5524 5452 5358 1983 5303 5320 5408 5531 1984 5624 5669 5697 5736 1985 5811 5894 5952 5965 1986 5943 5924 5935 5979 1987 6035 6097 6167 6227 1988 6256 6272 6295 6345 1989 6413 6468 6497 6511 1990 6514 6512 6490 6442 1991 6390 6346 6328 6340 1992 6362 6389 6433 6491 1993 6541 6566 6602 6671 1994 6765 6847 6890 6918 1995 6962 7018 7083 7134 1996 7173 7212 7242 7276 1997 7332 7400 7478 7550 1998 7618

Forecasting using R Time series in R 4

Time series in R

5000

6000

7000

1975 1980 1985 1990 1995Time

ausg

dp

Forecasting using R Time series in R 5

autoplot(ausgdp)

Time series in R

Residential electricity sales

elecsales

Time Series Start = 1989 End = 2008 Frequency = 1 [1] 235434 237971 231852 246899 238609 256947 257572 276272 [9] 284450 300070 310810 335750 307570 318060 322160 317620 [17] 343060 352748 363789 365500

Forecasting using R Time series in R 6

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Time plots 8

Time plotsautoplot(melsyd[EconomyClass])

0

10

20

30

1988 1989 1990 1991 1992 1993Time

mel

syd[

E

cono

my

Cla

ss]

Forecasting using R Time plots 9

Time plots

autoplot(a10 ylab=$ million xlab=Yearmain=Antidiabetic drug sales)

10

20

30

1995 2000 2005Year

$ m

illio

n

Antidiabetic drug sales

Forecasting using R Time plots 10

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 1 11

Lab Session 1

Forecasting using R Lab session 1 12

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal plots 13

Seasonal plotsggseasonplot(a10 ylab=$ million

yearlabels=TRUE yearlabelsleft=TRUE) +ggtitle(Seasonal plot antidiabetic drug sales)

1991 19911992 1992199319931994 19941995 19951996 199619971997

1998199819991999

2000 2000

20012001

20022002

2003 20032004 20042005 2005

2006 2006

20072007

2008

2008

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal plot antidiabetic drug sales

Forecasting using R Seasonal plots 14

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 2: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Time series in R 2

Time series in R

Time series consist of sequences of observationscollected over timeWe will assume the time periods are equally spaced

Time series examplesDaily IBM stock pricesMonthly rainfallAnnual Google profitsQuarterly Australian beer production

Forecasting using R Time series in R 3

Time series in R

ausgdp lt- ts(scan(gdpdat)frequency=4start=1971+24)

Class ldquotsrdquoPrint and plotting methods available

ausgdp

Qtr1 Qtr2 Qtr3 Qtr4 1971 4612 4651 1972 4645 4615 4645 4722 1973 4780 4830 4887 4933 1974 4921 4875 4867 4905 1975 4938 4934 4942 4979 1976 5028 5079 5112 5127 1977 5130 5101 5072 5069 1978 5100 5166 5244 5312 1979 5349 5370 5388 5396 1980 5388 5403 5442 5482 1981 5506 5531 5560 5583 1982 5568 5524 5452 5358 1983 5303 5320 5408 5531 1984 5624 5669 5697 5736 1985 5811 5894 5952 5965 1986 5943 5924 5935 5979 1987 6035 6097 6167 6227 1988 6256 6272 6295 6345 1989 6413 6468 6497 6511 1990 6514 6512 6490 6442 1991 6390 6346 6328 6340 1992 6362 6389 6433 6491 1993 6541 6566 6602 6671 1994 6765 6847 6890 6918 1995 6962 7018 7083 7134 1996 7173 7212 7242 7276 1997 7332 7400 7478 7550 1998 7618

Forecasting using R Time series in R 4

Time series in R

5000

6000

7000

1975 1980 1985 1990 1995Time

ausg

dp

Forecasting using R Time series in R 5

autoplot(ausgdp)

Time series in R

Residential electricity sales

elecsales

Time Series Start = 1989 End = 2008 Frequency = 1 [1] 235434 237971 231852 246899 238609 256947 257572 276272 [9] 284450 300070 310810 335750 307570 318060 322160 317620 [17] 343060 352748 363789 365500

Forecasting using R Time series in R 6

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Time plots 8

Time plotsautoplot(melsyd[EconomyClass])

0

10

20

30

1988 1989 1990 1991 1992 1993Time

mel

syd[

E

cono

my

Cla

ss]

Forecasting using R Time plots 9

Time plots

autoplot(a10 ylab=$ million xlab=Yearmain=Antidiabetic drug sales)

10

20

30

1995 2000 2005Year

$ m

illio

n

Antidiabetic drug sales

Forecasting using R Time plots 10

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 1 11

Lab Session 1

Forecasting using R Lab session 1 12

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal plots 13

Seasonal plotsggseasonplot(a10 ylab=$ million

yearlabels=TRUE yearlabelsleft=TRUE) +ggtitle(Seasonal plot antidiabetic drug sales)

1991 19911992 1992199319931994 19941995 19951996 199619971997

1998199819991999

2000 2000

20012001

20022002

2003 20032004 20042005 2005

2006 2006

20072007

2008

2008

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal plot antidiabetic drug sales

Forecasting using R Seasonal plots 14

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 3: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Time series in R

Time series consist of sequences of observationscollected over timeWe will assume the time periods are equally spaced

Time series examplesDaily IBM stock pricesMonthly rainfallAnnual Google profitsQuarterly Australian beer production

Forecasting using R Time series in R 3

Time series in R

ausgdp lt- ts(scan(gdpdat)frequency=4start=1971+24)

Class ldquotsrdquoPrint and plotting methods available

ausgdp

Qtr1 Qtr2 Qtr3 Qtr4 1971 4612 4651 1972 4645 4615 4645 4722 1973 4780 4830 4887 4933 1974 4921 4875 4867 4905 1975 4938 4934 4942 4979 1976 5028 5079 5112 5127 1977 5130 5101 5072 5069 1978 5100 5166 5244 5312 1979 5349 5370 5388 5396 1980 5388 5403 5442 5482 1981 5506 5531 5560 5583 1982 5568 5524 5452 5358 1983 5303 5320 5408 5531 1984 5624 5669 5697 5736 1985 5811 5894 5952 5965 1986 5943 5924 5935 5979 1987 6035 6097 6167 6227 1988 6256 6272 6295 6345 1989 6413 6468 6497 6511 1990 6514 6512 6490 6442 1991 6390 6346 6328 6340 1992 6362 6389 6433 6491 1993 6541 6566 6602 6671 1994 6765 6847 6890 6918 1995 6962 7018 7083 7134 1996 7173 7212 7242 7276 1997 7332 7400 7478 7550 1998 7618

Forecasting using R Time series in R 4

Time series in R

5000

6000

7000

1975 1980 1985 1990 1995Time

ausg

dp

Forecasting using R Time series in R 5

autoplot(ausgdp)

Time series in R

Residential electricity sales

elecsales

Time Series Start = 1989 End = 2008 Frequency = 1 [1] 235434 237971 231852 246899 238609 256947 257572 276272 [9] 284450 300070 310810 335750 307570 318060 322160 317620 [17] 343060 352748 363789 365500

Forecasting using R Time series in R 6

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Time plots 8

Time plotsautoplot(melsyd[EconomyClass])

0

10

20

30

1988 1989 1990 1991 1992 1993Time

mel

syd[

E

cono

my

Cla

ss]

Forecasting using R Time plots 9

Time plots

autoplot(a10 ylab=$ million xlab=Yearmain=Antidiabetic drug sales)

10

20

30

1995 2000 2005Year

$ m

illio

n

Antidiabetic drug sales

Forecasting using R Time plots 10

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 1 11

Lab Session 1

Forecasting using R Lab session 1 12

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal plots 13

Seasonal plotsggseasonplot(a10 ylab=$ million

yearlabels=TRUE yearlabelsleft=TRUE) +ggtitle(Seasonal plot antidiabetic drug sales)

1991 19911992 1992199319931994 19941995 19951996 199619971997

1998199819991999

2000 2000

20012001

20022002

2003 20032004 20042005 2005

2006 2006

20072007

2008

2008

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal plot antidiabetic drug sales

Forecasting using R Seasonal plots 14

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 4: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Time series in R

ausgdp lt- ts(scan(gdpdat)frequency=4start=1971+24)

Class ldquotsrdquoPrint and plotting methods available

ausgdp

Qtr1 Qtr2 Qtr3 Qtr4 1971 4612 4651 1972 4645 4615 4645 4722 1973 4780 4830 4887 4933 1974 4921 4875 4867 4905 1975 4938 4934 4942 4979 1976 5028 5079 5112 5127 1977 5130 5101 5072 5069 1978 5100 5166 5244 5312 1979 5349 5370 5388 5396 1980 5388 5403 5442 5482 1981 5506 5531 5560 5583 1982 5568 5524 5452 5358 1983 5303 5320 5408 5531 1984 5624 5669 5697 5736 1985 5811 5894 5952 5965 1986 5943 5924 5935 5979 1987 6035 6097 6167 6227 1988 6256 6272 6295 6345 1989 6413 6468 6497 6511 1990 6514 6512 6490 6442 1991 6390 6346 6328 6340 1992 6362 6389 6433 6491 1993 6541 6566 6602 6671 1994 6765 6847 6890 6918 1995 6962 7018 7083 7134 1996 7173 7212 7242 7276 1997 7332 7400 7478 7550 1998 7618

Forecasting using R Time series in R 4

Time series in R

5000

6000

7000

1975 1980 1985 1990 1995Time

ausg

dp

Forecasting using R Time series in R 5

autoplot(ausgdp)

Time series in R

Residential electricity sales

elecsales

Time Series Start = 1989 End = 2008 Frequency = 1 [1] 235434 237971 231852 246899 238609 256947 257572 276272 [9] 284450 300070 310810 335750 307570 318060 322160 317620 [17] 343060 352748 363789 365500

Forecasting using R Time series in R 6

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Time plots 8

Time plotsautoplot(melsyd[EconomyClass])

0

10

20

30

1988 1989 1990 1991 1992 1993Time

mel

syd[

E

cono

my

Cla

ss]

Forecasting using R Time plots 9

Time plots

autoplot(a10 ylab=$ million xlab=Yearmain=Antidiabetic drug sales)

10

20

30

1995 2000 2005Year

$ m

illio

n

Antidiabetic drug sales

Forecasting using R Time plots 10

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 1 11

Lab Session 1

Forecasting using R Lab session 1 12

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal plots 13

Seasonal plotsggseasonplot(a10 ylab=$ million

yearlabels=TRUE yearlabelsleft=TRUE) +ggtitle(Seasonal plot antidiabetic drug sales)

1991 19911992 1992199319931994 19941995 19951996 199619971997

1998199819991999

2000 2000

20012001

20022002

2003 20032004 20042005 2005

2006 2006

20072007

2008

2008

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal plot antidiabetic drug sales

Forecasting using R Seasonal plots 14

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 5: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Time series in R

5000

6000

7000

1975 1980 1985 1990 1995Time

ausg

dp

Forecasting using R Time series in R 5

autoplot(ausgdp)

Time series in R

Residential electricity sales

elecsales

Time Series Start = 1989 End = 2008 Frequency = 1 [1] 235434 237971 231852 246899 238609 256947 257572 276272 [9] 284450 300070 310810 335750 307570 318060 322160 317620 [17] 343060 352748 363789 365500

Forecasting using R Time series in R 6

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Time plots 8

Time plotsautoplot(melsyd[EconomyClass])

0

10

20

30

1988 1989 1990 1991 1992 1993Time

mel

syd[

E

cono

my

Cla

ss]

Forecasting using R Time plots 9

Time plots

autoplot(a10 ylab=$ million xlab=Yearmain=Antidiabetic drug sales)

10

20

30

1995 2000 2005Year

$ m

illio

n

Antidiabetic drug sales

Forecasting using R Time plots 10

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 1 11

Lab Session 1

Forecasting using R Lab session 1 12

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal plots 13

Seasonal plotsggseasonplot(a10 ylab=$ million

yearlabels=TRUE yearlabelsleft=TRUE) +ggtitle(Seasonal plot antidiabetic drug sales)

1991 19911992 1992199319931994 19941995 19951996 199619971997

1998199819991999

2000 2000

20012001

20022002

2003 20032004 20042005 2005

2006 2006

20072007

2008

2008

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal plot antidiabetic drug sales

Forecasting using R Seasonal plots 14

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 6: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Time series in R

Residential electricity sales

elecsales

Time Series Start = 1989 End = 2008 Frequency = 1 [1] 235434 237971 231852 246899 238609 256947 257572 276272 [9] 284450 300070 310810 335750 307570 318060 322160 317620 [17] 343060 352748 363789 365500

Forecasting using R Time series in R 6

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Time plots 8

Time plotsautoplot(melsyd[EconomyClass])

0

10

20

30

1988 1989 1990 1991 1992 1993Time

mel

syd[

E

cono

my

Cla

ss]

Forecasting using R Time plots 9

Time plots

autoplot(a10 ylab=$ million xlab=Yearmain=Antidiabetic drug sales)

10

20

30

1995 2000 2005Year

$ m

illio

n

Antidiabetic drug sales

Forecasting using R Time plots 10

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 1 11

Lab Session 1

Forecasting using R Lab session 1 12

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal plots 13

Seasonal plotsggseasonplot(a10 ylab=$ million

yearlabels=TRUE yearlabelsleft=TRUE) +ggtitle(Seasonal plot antidiabetic drug sales)

1991 19911992 1992199319931994 19941995 19951996 199619971997

1998199819991999

2000 2000

20012001

20022002

2003 20032004 20042005 2005

2006 2006

20072007

2008

2008

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal plot antidiabetic drug sales

Forecasting using R Seasonal plots 14

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 7: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Time plots 8

Time plotsautoplot(melsyd[EconomyClass])

0

10

20

30

1988 1989 1990 1991 1992 1993Time

mel

syd[

E

cono

my

Cla

ss]

Forecasting using R Time plots 9

Time plots

autoplot(a10 ylab=$ million xlab=Yearmain=Antidiabetic drug sales)

10

20

30

1995 2000 2005Year

$ m

illio

n

Antidiabetic drug sales

Forecasting using R Time plots 10

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 1 11

Lab Session 1

Forecasting using R Lab session 1 12

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal plots 13

Seasonal plotsggseasonplot(a10 ylab=$ million

yearlabels=TRUE yearlabelsleft=TRUE) +ggtitle(Seasonal plot antidiabetic drug sales)

1991 19911992 1992199319931994 19941995 19951996 199619971997

1998199819991999

2000 2000

20012001

20022002

2003 20032004 20042005 2005

2006 2006

20072007

2008

2008

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal plot antidiabetic drug sales

Forecasting using R Seasonal plots 14

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 8: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Time plots 8

Time plotsautoplot(melsyd[EconomyClass])

0

10

20

30

1988 1989 1990 1991 1992 1993Time

mel

syd[

E

cono

my

Cla

ss]

Forecasting using R Time plots 9

Time plots

autoplot(a10 ylab=$ million xlab=Yearmain=Antidiabetic drug sales)

10

20

30

1995 2000 2005Year

$ m

illio

n

Antidiabetic drug sales

Forecasting using R Time plots 10

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 1 11

Lab Session 1

Forecasting using R Lab session 1 12

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal plots 13

Seasonal plotsggseasonplot(a10 ylab=$ million

yearlabels=TRUE yearlabelsleft=TRUE) +ggtitle(Seasonal plot antidiabetic drug sales)

1991 19911992 1992199319931994 19941995 19951996 199619971997

1998199819991999

2000 2000

20012001

20022002

2003 20032004 20042005 2005

2006 2006

20072007

2008

2008

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal plot antidiabetic drug sales

Forecasting using R Seasonal plots 14

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 9: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Time series in R

Main package used in this coursegt library(fpp)

This loads

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

Forecasting using R Time series in R 7

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Time plots 8

Time plotsautoplot(melsyd[EconomyClass])

0

10

20

30

1988 1989 1990 1991 1992 1993Time

mel

syd[

E

cono

my

Cla

ss]

Forecasting using R Time plots 9

Time plots

autoplot(a10 ylab=$ million xlab=Yearmain=Antidiabetic drug sales)

10

20

30

1995 2000 2005Year

$ m

illio

n

Antidiabetic drug sales

Forecasting using R Time plots 10

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 1 11

Lab Session 1

Forecasting using R Lab session 1 12

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal plots 13

Seasonal plotsggseasonplot(a10 ylab=$ million

yearlabels=TRUE yearlabelsleft=TRUE) +ggtitle(Seasonal plot antidiabetic drug sales)

1991 19911992 1992199319931994 19941995 19951996 199619971997

1998199819991999

2000 2000

20012001

20022002

2003 20032004 20042005 2005

2006 2006

20072007

2008

2008

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal plot antidiabetic drug sales

Forecasting using R Seasonal plots 14

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 10: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Time plots 8

Time plotsautoplot(melsyd[EconomyClass])

0

10

20

30

1988 1989 1990 1991 1992 1993Time

mel

syd[

E

cono

my

Cla

ss]

Forecasting using R Time plots 9

Time plots

autoplot(a10 ylab=$ million xlab=Yearmain=Antidiabetic drug sales)

10

20

30

1995 2000 2005Year

$ m

illio

n

Antidiabetic drug sales

Forecasting using R Time plots 10

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 1 11

Lab Session 1

Forecasting using R Lab session 1 12

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal plots 13

Seasonal plotsggseasonplot(a10 ylab=$ million

yearlabels=TRUE yearlabelsleft=TRUE) +ggtitle(Seasonal plot antidiabetic drug sales)

1991 19911992 1992199319931994 19941995 19951996 199619971997

1998199819991999

2000 2000

20012001

20022002

2003 20032004 20042005 2005

2006 2006

20072007

2008

2008

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal plot antidiabetic drug sales

Forecasting using R Seasonal plots 14

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 11: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Time plotsautoplot(melsyd[EconomyClass])

0

10

20

30

1988 1989 1990 1991 1992 1993Time

mel

syd[

E

cono

my

Cla

ss]

Forecasting using R Time plots 9

Time plots

autoplot(a10 ylab=$ million xlab=Yearmain=Antidiabetic drug sales)

10

20

30

1995 2000 2005Year

$ m

illio

n

Antidiabetic drug sales

Forecasting using R Time plots 10

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 1 11

Lab Session 1

Forecasting using R Lab session 1 12

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal plots 13

Seasonal plotsggseasonplot(a10 ylab=$ million

yearlabels=TRUE yearlabelsleft=TRUE) +ggtitle(Seasonal plot antidiabetic drug sales)

1991 19911992 1992199319931994 19941995 19951996 199619971997

1998199819991999

2000 2000

20012001

20022002

2003 20032004 20042005 2005

2006 2006

20072007

2008

2008

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal plot antidiabetic drug sales

Forecasting using R Seasonal plots 14

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 12: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Time plots

autoplot(a10 ylab=$ million xlab=Yearmain=Antidiabetic drug sales)

10

20

30

1995 2000 2005Year

$ m

illio

n

Antidiabetic drug sales

Forecasting using R Time plots 10

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 1 11

Lab Session 1

Forecasting using R Lab session 1 12

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal plots 13

Seasonal plotsggseasonplot(a10 ylab=$ million

yearlabels=TRUE yearlabelsleft=TRUE) +ggtitle(Seasonal plot antidiabetic drug sales)

1991 19911992 1992199319931994 19941995 19951996 199619971997

1998199819991999

2000 2000

20012001

20022002

2003 20032004 20042005 2005

2006 2006

20072007

2008

2008

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal plot antidiabetic drug sales

Forecasting using R Seasonal plots 14

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 13: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 1 11

Lab Session 1

Forecasting using R Lab session 1 12

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal plots 13

Seasonal plotsggseasonplot(a10 ylab=$ million

yearlabels=TRUE yearlabelsleft=TRUE) +ggtitle(Seasonal plot antidiabetic drug sales)

1991 19911992 1992199319931994 19941995 19951996 199619971997

1998199819991999

2000 2000

20012001

20022002

2003 20032004 20042005 2005

2006 2006

20072007

2008

2008

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal plot antidiabetic drug sales

Forecasting using R Seasonal plots 14

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 14: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Lab Session 1

Forecasting using R Lab session 1 12

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal plots 13

Seasonal plotsggseasonplot(a10 ylab=$ million

yearlabels=TRUE yearlabelsleft=TRUE) +ggtitle(Seasonal plot antidiabetic drug sales)

1991 19911992 1992199319931994 19941995 19951996 199619971997

1998199819991999

2000 2000

20012001

20022002

2003 20032004 20042005 2005

2006 2006

20072007

2008

2008

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal plot antidiabetic drug sales

Forecasting using R Seasonal plots 14

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 15: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal plots 13

Seasonal plotsggseasonplot(a10 ylab=$ million

yearlabels=TRUE yearlabelsleft=TRUE) +ggtitle(Seasonal plot antidiabetic drug sales)

1991 19911992 1992199319931994 19941995 19951996 199619971997

1998199819991999

2000 2000

20012001

20022002

2003 20032004 20042005 2005

2006 2006

20072007

2008

2008

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal plot antidiabetic drug sales

Forecasting using R Seasonal plots 14

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 16: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Seasonal plotsggseasonplot(a10 ylab=$ million

yearlabels=TRUE yearlabelsleft=TRUE) +ggtitle(Seasonal plot antidiabetic drug sales)

1991 19911992 1992199319931994 19941995 19951996 199619971997

1998199819991999

2000 2000

20012001

20022002

2003 20032004 20042005 2005

2006 2006

20072007

2008

2008

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal plot antidiabetic drug sales

Forecasting using R Seasonal plots 14

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 17: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Seasonal plots

Data plotted against the individual ldquoseasonsrdquo in whichthe data were observed (In this case a ldquoseasonrdquo is amonth)Something like a time plot except that the data fromeach season are overlappedEnables the underlying seasonal pattern to be seenmore clearly and also allows any substantialdepartures from the seasonal pattern to be easilyidentifiedIn R ggseasonplot or seasonplot

Forecasting using R Seasonal plots 15

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 18: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Seasonal subseries plots

ggmonthplot(a10) + ylab($ million) +ggtitle(Seasonal subseries plot antidiabetic drug sales)

10

20

30

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth

$ m

illio

n

Seasonal subseries plot antidiabetic drug sales

Forecasting using R Seasonal plots 16

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 19: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Seasonal subseries plots

Data for each season collected together in time plotas separate time seriesEnables the underlying seasonal pattern to be seenclearly and changes in seasonality over time to bevisualizedIn R ggmonthplot or monthplot

Forecasting using R Seasonal plots 17

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 20: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Quarterly Australian Beer Production

beer lt- window(ausbeerstart=1992)

autoplot(beer)

ggseasonplot(beeryearlabels=TRUE)

ggmonthplot(beer)

Forecasting using R Seasonal plots 18

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 21: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Quarterly Australian Beer Production

400

450

500

1995 2000 2005Time

beer

Forecasting using R Seasonal plots 19

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 22: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Quarterly Australian Beer Production

1992

1993

1994

1995

1996

199719981999

20002001

2002

2003

2004

20052006

2007

2008400

450

500

Q1 Q2 Q3 Q4Quarter

Seasonal plot beer

Forecasting using R Seasonal plots 20

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 23: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Quarterly Australian Beer Production

400

450

500

Q1 Q2 Q3 Q4Quarter

beer

Forecasting using R Seasonal plots 21

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 24: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Seasonal or cyclic 22

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 25: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Time series patterns

Trend pattern exists when there is a long-termincrease or decrease in the data

Seasonal pattern exists when a series is influenced byseasonal factors (eg the quarter of the yearthe month or day of the week)

Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years)

Forecasting using R Seasonal or cyclic 23

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 26: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Time series patterns

8000

10000

12000

14000

1980 1985 1990 1995Year

GW

h

Australian electricity production

Forecasting using R Seasonal or cyclic 24

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 27: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Time series patterns

200

300

400

500

600

1960 1970 1980 1990Year

mill

ion

units

Australian clay brick production

Forecasting using R Seasonal or cyclic 25

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 28: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Time series patterns

40

60

80

1975 1980 1985 1990 1995Year

Tota

l sal

es

Sales of new oneminusfamily houses USA

Forecasting using R Seasonal or cyclic 26

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 29: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Time series patterns

86

88

90

0 20 40 60 80 100Day

pric

e

US Treasury Bill Contracts

Forecasting using R Seasonal or cyclic 27

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 30: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Time series patterns

0

2000

4000

6000

1820 1840 1860 1880 1900 1920Year

Num

ber

trap

ped

Annual Canadian Lynx Trappings

Forecasting using R Seasonal or cyclic 28

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 31: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 32: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Seasonal or cyclic

Differences between seasonal and cyclic patterns

seasonal pattern constant length cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern

The timing of peaks and troughs is predictable withseasonal data but unpredictable in the long term withcyclic data

Forecasting using R Seasonal or cyclic 29

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 33: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lag plots and autocorrelation 30

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 34: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Example Beer production

beer lt- window(ausbeer start=1992)gglagplot(beer lags=9 dolines=FALSE continuous=FALSE)

Forecasting using R Lag plots and autocorrelation 31

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 35: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Example Beer productionlag 1 lag 2 lag 3

lag 4 lag 5 lag 6

lag 7 lag 8 lag 9

400

450

500

400

450

500

400

450

500

400 450 500 400 450 500 400 450 500

season

1

2

3

4

Forecasting using R Lag plots and autocorrelation 32

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 36: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Lagged scatterplots

Each graph shows yt plotted against ytminusk for differentvalues of kThe autocorrelations are the correlations associatedwith these scatterplots

Forecasting using R Lag plots and autocorrelation 33

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 37: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 38: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 39: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Autocorrelation

Covariance and correlation measure extent of linearrelationship between two variables (y and X)Autocovariance and autocorrelation measure linearrelationship between lagged values of a time series y

We measure the relationship between yt and ytminus1yt and ytminus2yt and ytminus3etc

Forecasting using R Lag plots and autocorrelation 34

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 40: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 41: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

AutocorrelationWe denote the sample autocovariance at lag k by ck andthe sample autocorrelation at lag k by rk Then define

ck =1T

Tsumt=k+1

(yt minus y)(ytminusk minus y)

and rk = ckc0

r1 indicates how successive values of y relate to eachotherr2 indicates how y values two periods apart relate toeach otherrk is almost the same as the sample correlationbetween yt and ytminusk

Forecasting using R Lag plots and autocorrelation 35

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 42: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

AutocorrelationResults for first 9 lags for beer datafootnotesize

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0097 -0651 -0072 0872 -0074 -0628 -0083 0825 -0088

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 36

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 43: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Autocorrelation

r4 higher than for the other lags This is due to theseasonal pattern in the data the peaks tend to be 4quarters apart and the troughs tend to be 2 quartersapartr2 is more negative than for the other lags becausetroughs tend to be 2 quarters behind peaksTogether the autocorrelations at lags 1 2 makeup the autocorrelation or ACFThe plot is known as a correlogram

Forecasting using R Lag plots and autocorrelation 37

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 44: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

ACF

ggAcf(beer)

minus05

00

05

4 8 12 16Lag

AC

F

Series beer

Forecasting using R Lag plots and autocorrelation 38

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 45: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Recognizing seasonality in a time series

If there is seasonality the ACF at the seasonal lag (eg 12for monthly data) will be large and positive

For seasonal monthly data a large ACF value will beseen at lag 12 and possibly also at lags 24 36 For seasonal quarterly data a large ACF value will beseen at lag 4 and possibly also at lags 8 12

Forecasting using R Lag plots and autocorrelation 39

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 46: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Aus monthly electricity production

8000

10000

12000

14000

1980 1985 1990 1995Time

elec

2

Forecasting using R Lag plots and autocorrelation 40

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 47: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Aus monthly electricity production

000

025

050

075

0 12 24 36 48Lag

AC

F

Series elec2

Forecasting using R Lag plots and autocorrelation 41

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 48: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Aus monthly electricity production

Time plot shows clear trend and seasonality

The same features are reflected in the ACF

The slowly decaying ACF indicates trendThe ACF peaks at lags 12 24 36 indicateseasonality of length 12

Forecasting using R Lag plots and autocorrelation 42

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 49: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Which is which

40

60

80

0 20 40 60

chir

ps p

er m

inut

e

1 Daily temperature of cow

7

8

9

10

11

1973 1974 1975 1976 1977 1978 1979

thou

sand

s

2 Monthly accidental deaths

200

400

600

1950 1952 1954 1956 1958 1960

thou

sand

s

3 Monthly air passengers

30

60

90

1850 1860 1870 1880 1890 1900 1910

thou

sand

s

4 Annual mink trappings

00

05

10

12 246 18

AC

F

A

00

05

10

5 10 15

AC

F

B

00

05

10

5 10 15

AC

F

C

00

05

10

12 246 18

AC

F

D

Forecasting using R Lag plots and autocorrelation 43

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 50: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R White noise 44

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 51: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 52: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Example White noise

minus3

minus2

minus1

0

1

2

0 5 10 15 20 25 30 35Time

wn

White noise data is uncorrelated across time with zeromean and constant variance(Technically we require independence as well)

Forecasting using R White noise 45

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 53: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Example White noise

r1 -011r2 -023r3 -031r4 015r5 021r6 004r7 -017r8 -020r9 026r10 007

Sample autocorrelations for white noise series

For uncorrelated data we would expect eachautocorrelation to be close to zero

Forecasting using R White noise 46

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wn

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 54: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 55: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Sampling distribution of autocorrelations

Sampling distribution of rk for white noise data isasymptotically N(01T)

95 of all rk for white noise must lie withinplusmn196

radicT

If this is not the case the series is probably not WNCommon to plot lines atplusmn196

radicT when plotting

ACF These are the critical values

Forecasting using R White noise 47

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 56: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Autocorrelation

Forecasting using R White noise 48

minus02

00

02

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Lag

AC

F

Series wnExampleT = 36 and so criticalvalues atplusmn196

radic36 = plusmn0327

All autocorrelationcoefficients lie withinthese limits confirmingthat the data are whitenoise (More preciselythe data cannot bedistinguished from white noise)

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 57: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Example Pigs slaughtered

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Year

thou

sand

s

Number of pigs slaughtered in Victoria

Forecasting using R White noise 49

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 58: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Example Pigs slaughtered

minus02

00

02

12 246 18Lag

AC

F

Series pigs2

Forecasting using R White noise 50

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 59: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 60: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 61: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Example Pigs slaughtered

Monthly total number of pigs slaughtered in the state ofVictoria Australia from January 1990 through August1995 (Source Australian Bureau of Statistics)

Difficult to detect pattern in time plotACF shows some significant autocorrelation at lags 12 and 3r12 relatively large although not significant This mayindicate some slight seasonality

These show the series is not a white noise series

Forecasting using R White noise 51

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 62: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Combination graph

ggtsdisplay(pigs2 plottype=scatter)

80000

90000

100000

110000

1990 1991 1992 1993 1994 1995Time

x

pigs2

minus02

00

02

0 5 10 15 20Lag

AC

F

80000

90000

100000

110000

80000 90000 100000 110000Ytminus1

Yt

Forecasting using R White noise 52

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 63: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Outline1 Time series in R

2 Time plots

3 Lab session 1

4 Seasonal plots

5 Seasonal or cyclic

6 Lag plots and autocorrelation

7 White noise

8 Lab session 2

Forecasting using R Lab session 2 53

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2
Page 64: Forecasting using R - Rob J. Hyndman · Class: “ts ” Print and ... ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134

Lab Session 2

Forecasting using R Lab session 2 54

  • Time series in R
  • Time plots
  • Lab session 1
  • Seasonal plots
  • Seasonal or cyclic
  • Lag plots and autocorrelation
  • White noise
  • Lab session 2