Practice of Introductory Time Series With R
Practice of Introductory Time Series With R
Practice of Introductory Time Series With R
Reference:
Cowpertwait, Paul SP, and Andrew V. Metcalfe. Introductory time series with
R. Springer Science & Business Media, 2009.
Onlinecourses.science.psu.edu,. 1.3 R Code For Two Examples In Lessons 1.1
And 1.2 | STAT 510. N.p., 2015. Web. 10 Aug. 2015.
This paper is a practice from the book called Introductory Time Series with R by by Paul Cowpertwait and Andrew Metcalfe, and from the website: onlinecourses.science.psu.edu/stat510/node/61.
All R codes and some comments below are belonged to the book and the website. Dataset
can be searched from Google.
Fact <- function(n) if (n == 1) 1 else n * Fact(n - 1) # one line function
Fact(6) # 6*5*4*3*2*1=720
## [1] 720
data(AirPassengers)
AP <- AirPassengers
aggregate(AP,FUN=mean) # to remove any seasonal effects
##
##
##
##
##
##
Time Series:
Start = 1949
End = 1960
Frequency = 1
[1] 126.6667 139.6667 170.1667 197.0000 225.0000 238.9167 284.0000
[8] 328.2500 368.4167 381.0000 428.3333 476.1667
500
300
100
Passengers (1000's)
1950
1952
1954
1956
1958
1960
Time
4000
2000
aggregate(AP)
1950
1952
1954
Time
1956
1958
1960
100
300
500
boxplot(AP ~ cycle(AP))
10
11
12
10
8
6
4
unemployed (%)
1950
1960
1970
1980
1990
2000
2010
Time
8
7
6
5
4
3
unemployed (%)
1950
1960
1970
1980
Time
1990
2000
2010
10
5
0
inflation (%)
15
1950
1960
1970
1980
1990
2000
2010
Time
10
5
0
inflation (%)
1950
1960
1970
1980
Time
1990
2000
2010
8000
2002000
100
6000
2000
Choc.ts
Beer.ts
Elec.ts
1960
1965
1970
1975
Time
1980
1985
1990
600
500
400
300
1958.0
1958.5
1959.0
1959.5
Time
1960.0
1960.5
1961.0
2000
1600
1958.0
1958.5
1959.0
1959.5
1960.0
1960.5
Time
2000
1600
300
350
400
450
500
550
600
1961.0
0.0
0.5
1.0
Global.ts
0.5
Global<-scan("http://elena.aut.ac.nz/~pcowpert/ts/global.dat")
Global.ts <- ts(Global, st = c(1856, 1), end = c(2005, 12), freq = 12)
Global.annual <- aggregate(Global.ts, FUN = mean)
plot(Global.ts)
1900
1950
Time
plot(Global.annual)
2000
0.4
0.0
0.4
Global.annual
1900
1950
2000
Time
0.4
0.0
0.4
New.series
0.8
1970
1975
1980
1985
1990
Time
10
1995
2000
2005
plot(decompose(Elec.ts))
12000
100002000
500 2000
600500
600 0
1960
1965
1970
1975
Time
11
1980
1985
1990
12000
100002000
1.05 2000
1.02 0.90
0.94
1960
1965
1970
1975
1980
1985
1990
Time
2000
6000
10000 14000
1960
1965
1970
1975
Time
12
1980
1985
1990
10
15
x
13
20
cor(x,y) # correlation
## [1] 0.3550973
0
500
ts(waveht)
500
100
200
Time
plot(ts(waveht[1:60]))
14
300
400
600
200
200
600
ts(waveht[1:60])
10
20
30
40
50
60
Time
acf(waveht)$acf[2]
0.5
0.0
ACF
0.5
1.0
Series waveht
10
15
Lag
## [1] 0.4702564
15
20
25
500
0
500
waveht[2:397]
plot(waveht[1:396],waveht[2:397])
500
500
waveht[1:396]
acf(waveht, type = c("covariance"))$acf[2]
20000
20000
ACF (cov)
60000
Series waveht
10
15
Lag
16
20
25
## [1] 33328.39
data(AirPassengers)
AP <- AirPassengers
acf(AP)
0.2
0.2
ACF
0.6
1.0
Series AP
0.0
0.5
1.0
Lag
17
1.5
1.10
1.05
1.00
0.95
0.90
ts(AP.decom$random[7:138])
20
40
60
80
100
120
Time
acf(AP.decom$random[7:138])
0.2
0.2
ACF
0.6
1.0
Series AP.decom$random[7:138]
10
15
20
Lag
#The figure suggests either a cosine shape that is whether a characteristic of an autoregressive model of
order 2 or a not effective seasonal adjustment.
18
70
90
mort
110
130
mort=scan("http://anson.ucdavis.edu/~shumway/cmort.dat")
plot(mort, type="o") # plot of mortality rate
100
200
300
Index
mort=ts(mort)
mortdiff=diff(mort,1) # creates a variable = x(t) x(t-1)
plot(mortdiff,type="o") # plot of first differences
19
400
500
20
10
0
20 10
mortdiff
100
200
300
400
Time
0.5
0.0
ACF
0.5
1.0
Series mortdiff
10
15
Lag
20
20
500
10
0
20 10
mortdifflag1
20
mortdifflag1=lag(mortdiff,-1)
plot(mortdifflag1,type="o")
100
200
300
400
500
Time
Call:
lm(formula = y[, 1] ~ y[, 2])
Residuals:
Min
1Q
-19.2758 -3.8753
Median
-0.0953
3Q
3.5725
Max
20.8169
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.04627
0.25900 -0.179
0.858
y[, 2]
-0.50636
0.03838 -13.195
<2e-16 ***
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 5.826 on 504 degrees of freedom
(2 observations deleted due to missingness)
Multiple R-squared: 0.2568, Adjusted R-squared: 0.2553
F-statistic: 174.1 on 1 and 504 DF, p-value: < 2.2e-16
21
0.4
0.0
ACF
0.8
Series mortdiffar1$residuals
10
15
Lag
22
20