0% found this document useful (0 votes)
19 views73 pages

2 Tsgraphics

The document discusses time series objects and plots in R. It explains how to create ts objects and the parameters for different frequencies. Examples are given of plotting various time series data including Australian GDP, electricity sales, and temperature. Different plot types for time series are also demonstrated.

Uploaded by

s3979517
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views73 pages

2 Tsgraphics

The document discusses time series objects and plots in R. It explains how to create ts objects and the parameters for different frequencies. Examples are given of plotting various time series data including Australian GDP, electricity sales, and temperature. Different plot types for time series are also demonstrated.

Uploaded by

s3979517
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

Topic 2

Time Series Graphics

2
Topic 2 - Outline

1 Time series in R
2 Time plots
3 Seasonal plots
4 Seasonal or cyclic?
5 Lag plots and autocorrelation
6 White noise

2
ts objects and ts function

A time series is stored in a ts object in R:


a list of numbers
information about times those numbers were recorded.
Example

Year Observation
2012 123
2013 39
2014 78
2015 52
2016 110

y <- ts(c(123,39,78,52,110), start=2012)


3
ts objects and ts function

For observations that are more frequent than once


per year, add a frequency argument.
E.g., monthly data stored as a numerical vector z:
y <- ts(y, frequency=12, start=c(2003, 1))

4
ts objects and ts function

ts(data, frequency, start)


Type of data frequency start example
Annual
Quarterly
Monthly
Daily
Weekly
Hourly
Half-hourly

5
ts objects and ts function

ts(data, frequency, start)


Type of data frequency start example
Annual 1
Quarterly
Monthly
Daily
Weekly
Hourly
Half-hourly

5
ts objects and ts function

ts(data, frequency, start)


Type of data frequency start example
Annual 1 1995
Quarterly
Monthly
Daily
Weekly
Hourly
Half-hourly

5
ts objects and ts function

ts(data, frequency, start)


Type of data frequency start example
Annual 1 1995
Quarterly 4
Monthly
Daily
Weekly
Hourly
Half-hourly

5
ts objects and ts function

ts(data, frequency, start)


Type of data frequency start example
Annual 1 1995
Quarterly 4 c(1995,2)
Monthly
Daily
Weekly
Hourly
Half-hourly

5
ts objects and ts function

ts(data, frequency, start)


Type of data frequency start example
Annual 1 1995
Quarterly 4 c(1995,2)
Monthly 12
Daily
Weekly
Hourly
Half-hourly

5
ts objects and ts function

ts(data, frequency, start)


Type of data frequency start example
Annual 1 1995
Quarterly 4 c(1995,2)
Monthly 12 c(1995,9)
Daily
Weekly
Hourly
Half-hourly

5
ts objects and ts function

ts(data, frequency, start)


Type of data frequency start example
Annual 1 1995
Quarterly 4 c(1995,2)
Monthly 12 c(1995,9)
Daily 7 or 365.25
Weekly
Hourly
Half-hourly

5
ts objects and ts function

ts(data, frequency, start)


Type of data frequency start example
Annual 1 1995
Quarterly 4 c(1995,2)
Monthly 12 c(1995,9)
Daily 7 or 365.25 1 or c(1995,234)
Weekly
Hourly
Half-hourly

5
ts objects and ts function

ts(data, frequency, start)


Type of data frequency start example
Annual 1 1995
Quarterly 4 c(1995,2)
Monthly 12 c(1995,9)
Daily 7 or 365.25 1 or c(1995,234)
Weekly 52.18
Hourly
Half-hourly

5
ts objects and ts function

ts(data, frequency, start)


Type of data frequency start example
Annual 1 1995
Quarterly 4 c(1995,2)
Monthly 12 c(1995,9)
Daily 7 or 365.25 1 or c(1995,234)
Weekly 52.18 c(1995,23)
Hourly
Half-hourly

5
ts objects and ts function

ts(data, frequency, start)


Type of data frequency start example
Annual 1 1995
Quarterly 4 c(1995,2)
Monthly 12 c(1995,9)
Daily 7 or 365.25 1 or c(1995,234)
Weekly 52.18 c(1995,23)
Hourly 24 or 168 or 8,766
Half-hourly

5
ts objects and ts function

ts(data, frequency, start)


Type of data frequency start example
Annual 1 1995
Quarterly 4 c(1995,2)
Monthly 12 c(1995,9)
Daily 7 or 365.25 1 or c(1995,234)
Weekly 52.18 c(1995,23)
Hourly 24 or 168 or 8,766 1
Half-hourly

5
ts objects and ts function

ts(data, frequency, start)


Type of data frequency start example
Annual 1 1995
Quarterly 4 c(1995,2)
Monthly 12 c(1995,9)
Daily 7 or 365.25 1 or c(1995,234)
Weekly 52.18 c(1995,23)
Hourly 24 or 168 or 8,766 1
Half-hourly 48 or 336 or 17,532

5
ts objects and ts function

ts(data, frequency, start)


Type of data frequency start example
Annual 1 1995
Quarterly 4 c(1995,2)
Monthly 12 c(1995,9)
Daily 7 or 365.25 1 or c(1995,234)
Weekly 52.18 c(1995,23)
Hourly 24 or 168 or 8,766 1
Half-hourly 48 or 336 or 17,532 1

5
Australian GDP
ausgdp <- ts(x, frequency=4, start=c(1971,3))
Class: “ts”
Print 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 6
Australian GDP

autoplot(ausgdp)

7000
ausgdp

6000

5000

1975 1980 1985 1990 1995


Time
7
Residential electricity sales

elecsales
## Time Series:
## Start = 1989
## End = 2008
## Frequency = 1
## [1] 2354.34 2379.71 2318.52 2468.99 2386.09
## [6] 2569.47 2575.72 2762.72 2844.50 3000.70
## [11] 3108.10 3357.50 3075.70 3180.60 3221.60
## [16] 3176.20 3430.60 3527.48 3637.89 3655.00

8
Class package

> library(fpp2)

9
Class package

> library(fpp2)
This loads:
some data for use in examples and exercises

9
Class package

> library(fpp2)
This loads:
some data for use in examples and exercises
forecast package (for forecasting functions)
ggplot2 package (for graphics functions)
fma package (for lots of time series data)
expsmooth package (for more time series data)

9
Outline

1 Time series in R
2 Time plots
3 Seasonal plots
4 Seasonal or cyclic?
5 Lag plots and autocorrelation
6 White noise

10
Time plots

autoplot(melsyd[,"Economy.Class"])

30
melsyd[, "Economy.Class"]

20

10

1988 1989 1990 1991 1992 1993


Time
11
Time plots

autoplot(a10) + ylab("$ million") + xlab("Year") +


ggtitle("Antidiabetic drug sales")
Antidiabetic drug sales
30

20
$ million

10

1995 2000 2005


Year

12
Your turn

Create plots of the following time series: dole,


bricksq, lynx, goog
Use help() to find out about the data in each
series.
For the last plot, modify the axis labels and title.

13
Are time plots best?

autoplot(elecdaily[,"Temperature"]) +
xlab("Week") + ylab("Max temperature")

40
Max temperature

30

20

10

0 10 20 30 40 50
Week

14
Are time plots best?

qplot(time(elecdaily), elecdaily[,"Temperature"]) +
xlab("Week") + ylab("Max temperature")

40
Max temperature

30

20

10

0 20 40
Week

15
Are time plots best?

Maxtemp
40

30

20

10

0 20 40
Week

16
Are time plots best?

17
Outline

1 Time series in R
2 Time plots
3 Seasonal plots
4 Seasonal or cyclic?
5 Lag plots and autocorrelation
6 White noise

18
Seasonal plots
ggseasonplot(a10, year.labels=TRUE, year.labels.left=TRUE) +
ylab("$ million") +
ggtitle("Seasonal plot: antidiabetic drug sales")
Seasonal plot: antidiabetic drug sales
30 2008
2007
2007
2006 2006
20 2005 2005
2008 2004
$ million

2004
2003
2002 2003
2001 2002
2000 2001
2000
1999
10 1999 1998
1997
1998
1997
1996 1996
1995
1994 1995
1993
1993 1994
1992
1992 1991
1991
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Month
19
Seasonal plots

Data plotted against the individual “seasons” in


which the data were observed. (In this case a
“season” is a month.)
Something like a time plot except that the data
from each season are overlapped.
Enables the underlying seasonal pattern to be
seen more clearly, and also allows any
substantial departures from the seasonal pattern
to be easily identified.
In R: ggseasonplot()
20
Seasonal polar plots

ggseasonplot(a10, polar=TRUE) + ylab("$ million")


Seasonal plot: a10

Jan
year
Dec Feb
1991
1992
1993
20
Nov Mar 1994
1995
1996
10
1997
1998
$ million

Oct Apr 1999


2000
2001
2002
2003
Sep May 2004
2005
2006
2007
Aug Jun
2008
Jul

Month
21
Seasonal subseries plots

ggsubseriesplot(a10) + ylab("$ million") +


ggtitle("Subseries plot: antidiabetic drug sales")
Subseries plot: antidiabetic drug sales
30

20
$ million

10

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Month

22
Seasonal subseries plots

Data for each season collected together in time


plot as separate time series.
Enables the underlying seasonal pattern to be
seen clearly, and changes in seasonality over
time to be visualized.
In R: ggsubseriesplot()

23
Quarterly Australian Beer Production

beer <- window(ausbeer,start=1992)


autoplot(beer)

500
beer

450

400

1995 2000 2005 2010


Time
24
Quarterly Australian Beer Production

ggseasonplot(beer,year.labels=TRUE)
Seasonal plot: beer
1992
1994
1997
1999
1995
1998
1993
1996
2002
500 2000
2001
2006
2003
2009
2008
2005
2007

450 2004

400

2010
Q1 Q2 Q3 Q4
Quarter

25
Quarterly Australian Beer Production

ggsubseriesplot(beer)

500
beer

450

400

Q1 Q2 Q3 Q4
Quarter

26
Your turn

The arrivals data set comprises quarterly


international arrivals (in thousands) to Australia from
Japan, New Zealand, UK and the US.
Use autoplot() and ggseasonplot() to
compare the differences between the arrivals
from these four countries.
Can you identify any unusual observations?

27
Outline

1 Time series in R
2 Time plots
3 Seasonal plots
4 Seasonal or cyclic?
5 Lag plots and autocorrelation
6 White noise

28
Time series patterns

Trend pattern exists when there is a long-term


increase or decrease in the data.
Seasonal pattern exists when a series is influenced
by seasonal factors (e.g., the quarter of
the year, the month, or day of the week).
Cyclic pattern exists when data exhibit rises and
falls that are not of fixed period (duration
usually of at least 2 years).

29
Time series components

Differences between seasonal and cyclic patterns:


seasonal pattern constant length; cyclic pattern
variable length
average length of cycle longer than length of
seasonal pattern
magnitude of cycle more variable than
magnitude of seasonal pattern

30
Time series patterns

autoplot(window(elec, start=1980)) +
ggtitle("Australian electricity production") +
xlab("Year") + ylab("GWh")
Australian electricity production

14000

12000
GWh

10000

8000

1980 1985 1990 1995


Year 31
Time series patterns

autoplot(bricksq) +
ggtitle("Australian clay brick production") +
xlab("Year") + ylab("million units")
Australian clay brick production
600

500
million units

400

300

200

1960 1970 1980 1990


Year 32
Time series patterns

autoplot(hsales) +
ggtitle("Sales of new one-family houses, USA") +
xlab("Year") + ylab("Total sales")
Sales of new one−family houses, USA

80
Total sales

60

40

1975 1980 1985 1990 1995


Year 33
Time series patterns

autoplot(ustreas) +
ggtitle("US Treasury Bill Contracts") +
xlab("Day") + ylab("price")
US Treasury Bill Contracts

90
price

88

86

0 20 40 60 80 100 34
Day
Time series patterns

autoplot(lynx) +
ggtitle("Annual Canadian Lynx Trappings") +
xlab("Year") + ylab("Number trapped")
Annual Canadian Lynx Trappings

6000
Number trapped

4000

2000

0
1820 1840 1860 1880 1900 1920
Year 35
Seasonal or cyclic?

Differences between seasonal and cyclic patterns:


seasonal pattern constant length; cyclic pattern
variable length
average length of cycle longer than length of
seasonal pattern
magnitude of cycle more variable than
magnitude of seasonal pattern

36
Seasonal or cyclic?

Differences between seasonal and cyclic patterns:


seasonal pattern constant length; cyclic pattern
variable length
average length of cycle longer than length of
seasonal pattern
magnitude of cycle more variable than
magnitude of seasonal pattern
The timing of peaks and troughs is predictable with
seasonal data, but unpredictable in the long term
with cyclic data.
36
Outline

1 Time series in R
2 Time plots
3 Seasonal plots
4 Seasonal or cyclic?
5 Lag plots and autocorrelation
6 White noise

37
Autocorrelation

Correlation: measure extent of linear relationship


between two variables (y and X).

41
Autocorrelation

Correlation: measure extent of linear relationship


between two variables (y and X). Autocorrelation:
measure linear relationship between lagged values
of a time series y.

41
Autocorrelation

Covariance and correlation: measure extent of linear


relationship between two variables (y and X).
Autocovariance and autocorrelation: measure linear
relationship between lagged values of a time series y.
We measure the relationship between:
yt and yt−1
yt and yt−2
yt and yt−3
etc.

41
Autocorrelation

We denote the sample autocovariance at lag k by ck


and the sample autocorrelation at lag k by rk . Then
define
1 X T
ck = (yt − ȳ)(yt−k − ȳ)
T t=k+1
and rk = ck /c0

42
Autocorrelation

We denote the sample autocovariance at lag k by ck


and the sample autocorrelation at lag k by rk . Then
define
1 X T
ck = (yt − ȳ)(yt−k − ȳ)
T t=k+1
and rk = ck /c0

r1 indicates how successive values of y relate to each other


r2 indicates how y values two periods apart relate to each
other
rk is almost the same as the sample correlation between yt
and yt−k . 42
Autocorrelation
Results for first 9 lags for beer data:

r1 r2 r3 r4 r5 r6 r7 r8 r9

-0.102 -0.657 -0.060 0.869 -0.089 -0.635 -0.054 0.832 -0.108

ggAcf(beer)
Series: beer

0.5
ACF

0.0

−0.5

4 8 12 16
Lag
43
Autocorrelation

r4 higher than for the other lags. This is due to


the seasonal pattern in the data: the peaks
tend to be 4 quarters apart and the troughs tend
to be 2 quarters apart.
r2 is more negative than for the other lags
because troughs tend to be 2 quarters behind
peaks.
Together, the autocorrelations at lags 1, 2, . . . ,
make up the autocorrelation or ACF.
The plot is known as a correlogram
44
ACF

ggAcf(beer)
Series: beer

0.5
ACF

0.0

−0.5

4 8 12 16
Lag
45
Trend and seasonality in ACF plots

When data have a trend, the autocorrelations for


small lags tend to be large and positive.
When data are seasonal, the autocorrelations
will be larger at the seasonal lags (i.e., at
multiples of the seasonal frequency)
When data are trended and seasonal, you see a
combination of these effects.

46
Aus monthly electricity production

elec2 <- window(elec, start=1980)


autoplot(elec2)

14000

12000
elec2

10000

8000

1980 1985 1990 1995


Time
47
Aus monthly electricity production

ggAcf(elec2, lag.max=48)
Series: elec2

0.75

0.50
ACF

0.25

0.00

0 12 24 36 48
Lag

48
Aus monthly electricity production

Time plot shows clear trend and seasonality.


The same features are reflected in the ACF.
The slowly decaying ACF indicates trend.
The ACF peaks at lags 12, 24, 36, . . . , indicate
seasonality of length 12.

49
Google stock price

autoplot(goog)

800

700
goog

600

500

400

0 200 400 600 800 1000


Time

50
Google stock price

ggAcf(goog, lag.max=100)
Series: goog
1.00

0.75
ACF

0.50

0.25

0.00

0 20 40 60 80 100
Lag

51
Your turn

We have introduced the following graphics functions:


gglagplot
ggAcf
Explore the following time series using these
functions. Can you spot any seasonality, cyclicity and
trend? What do you learn about the series?
hsales
usdeaths
bricksq
sunspotarea
gasoline 52
Which is which?

1. Daily temperature of cow 2. Monthly accidental deaths 3. Monthly air passengers 4. Annual mink trappings

11 600

90
chirps per minute

80 10

thousands

thousands

thousands
400
9 60
60

8
200
30
40 7

0 20 40 60 1974 1976 1978 1950 1952 1954 1956 1958 1960 1860 1880 1900

A B C D
1.0 1.0 1.0 1.0

0.5 0.5 0.5 0.5


ACF

ACF

ACF

ACF
0.0 0.0 0.0 0.0

6 12 18 24 5 10 15 5 10 15 6 12 18 24

53
Outline

1 Time series in R
2 Time plots
3 Seasonal plots
4 Seasonal or cyclic?
5 Lag plots and autocorrelation
6 White noise

54
Example: White noise

wn <- ts(rnorm(36))
autoplot(wn)

1
wn

−1

−2

0 10 20 30
Time
55
Example: White noise
Series: wn
r1 0.03
r2 -0.13 0.2

r3 0.17
r4 0.09
ACF
0.0

r5 -0.15
r6 0.04 −0.2

r7 0.03
r8 -0.07 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Lag
r9 0.00
r10 -0.13

Sample autocorrelations for white noise series.


We expect each autocorrelation to be close to zero.
56

You might also like