2 Tsgraphics
2 Tsgraphics
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
Year Observation
2012 123
2013 39
2014 78
2015 52
2016 110
4
ts objects and ts function
5
ts objects and ts function
5
ts objects and ts function
5
ts objects and ts function
5
ts objects and ts function
5
ts objects and ts function
5
ts objects and ts function
5
ts objects and ts function
5
ts objects and ts function
5
ts objects and ts function
5
ts objects and ts function
5
ts objects and ts function
5
ts objects and ts function
5
ts objects and ts function
5
ts objects and ts function
5
Australian GDP
ausgdp <- ts(x, frequency=4, start=c(1971,3))
Class: “ts”
Print and plotting methods available.
ausgdp
autoplot(ausgdp)
7000
ausgdp
6000
5000
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
20
$ million
10
12
Your turn
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
Jan
year
Dec Feb
1991
1992
1993
20
Nov Mar 1994
1995
1996
10
1997
1998
$ million
Month
21
Seasonal subseries plots
20
$ million
10
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Month
22
Seasonal subseries plots
23
Quarterly Australian Beer Production
500
beer
450
400
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
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
29
Time series components
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
autoplot(bricksq) +
ggtitle("Australian clay brick production") +
xlab("Year") + ylab("million units")
Australian clay brick production
600
500
million units
400
300
200
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
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?
36
Seasonal or cyclic?
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
41
Autocorrelation
41
Autocorrelation
41
Autocorrelation
42
Autocorrelation
r1 r2 r3 r4 r5 r6 r7 r8 r9
ggAcf(beer)
Series: beer
0.5
ACF
0.0
−0.5
4 8 12 16
Lag
43
Autocorrelation
ggAcf(beer)
Series: beer
0.5
ACF
0.0
−0.5
4 8 12 16
Lag
45
Trend and seasonality in ACF plots
46
Aus monthly electricity production
14000
12000
elec2
10000
8000
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
49
Google stock price
autoplot(goog)
800
700
goog
600
500
400
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
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
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