You are on page 1of 17

Review

YIK LUN, KEI


allen29@ucla.edu

1. arima
library("fUnitRoots")
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

Loading
Loading
Loading
Loading

required
required
required
required

package:
package:
package:
package:

urca
timeDate
timeSeries
fBasics

Rmetrics Package fBasics


Analysing Markets and calculating Basic Statistics
Copyright (C) 2005-2014 Rmetrics Association Zurich
Educational Software for Financial Engineering and Computational Science
Rmetrics is free software and comes with ABSOLUTELY NO WARRANTY.
https://www.rmetrics.org --- Mail to: info@rmetrics.org
Attaching package: 'fUnitRoots'
The following objects are masked from 'package:urca':
punitroot, qunitroot, unitrootTable

setwd("~/Desktop/Chicago")
data<-read.table("dgnp82.txt")
gnp=ts(data[,1],frequency=12,start=c(1947,1))
plot(gnp)

0.04
0.02
0.02

0.00

gnp

1950

1955
Time

decom <- decompose(gnp,"multiplicative")


acf(decom$random[13:length(gnp)-6])

1960

0.2 0.4 0.6 0.8 1.0


0.2

ACF

Series decom$random[13:length(gnp) 6]

10

15

Lag

Box.test(gnp,lag=log(length(gnp)),type='Ljung')
##
## Box-Ljung test
##
## data: gnp
## X-squared = 40.507, df = 5.1705, p-value = 1.44e-07
adfTest(gnp,lag=3,type=c("c")) #reject the null of having unit root
## Warning in adfTest(gnp, lag = 3, type = c("c")): p-value smaller than
## printed p-value
##
## Title:
## Augmented Dickey-Fuller Test
##
## Test Results:
##
PARAMETER:
##
Lag Order: 3
##
STATISTIC:
##
Dickey-Fuller: -6.3523
##
P VALUE:
##
0.01
3

20

##
## Description:
## Sat Oct 10 22:51:09 2015 by user:
acf(gnp)#MA(2)

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series gnp

0.0

0.5

1.0
Lag

pacf(gnp)

1.5

0.2
0.1
0.0
0.1

Partial ACF

0.3

Series gnp

0.5

1.0

1.5

Lag
m1=ar(gnp,method="mle")
m1$aic;m1$order
##
0
## 27.8466897
##
6
## 4.0520840
##
12
## 7.1975452

1
2.7416324
7
6.0254750

2
1.6032416
8
5.9046676

3
0.0000000
9
7.5718635

4
0.3027852
10
7.8953337

5
2.2426608
11
9.6788727

## [1] 3
m2=arima(gnp,order=c(3,0,0))
which((1-pnorm(abs(m2$coef)/sqrt(diag(m2$var.coef))))*2 > 0.05)
## ar3
##
3
Box.test(m2$resid,lag=log(length(gnp)),type='Ljung')
##
## Box-Ljung test
##
## data: m2$resid
## X-squared = 2.2871, df = 5.1705, p-value = 0.8251
5

p1=c(1,-m2$coef[1:3])
roots<-polyroot(p1)
roots
## [1]

1.590253+1.063882i -1.920152+0.000000i

1.590253-1.063882i

2*pi/acos(1.590253/sqrt((1.590253)^2+(1.063882)^2))
## [1] 10.65638
predict(m2,12)$pred
##
##
##
##
##
##
##
##
##

Jan
Feb
Mar
Apr
May
1961
1962 0.008181442 0.007936845 0.007820046 0.007703826 0.007677234
Jun
Jul
Aug
Sep
Oct
1961
0.001236254 0.004555519
1962 0.007663757 0.007670833 0.007674662
Nov
Dec
1961 0.007454906 0.007958518
1962

AR(3) is better
gnp1=diff(gnp)
ar(gnp1,method="mle")
##
##
##
##
##
##
##
##
##
##
##

Call:
ar(x = gnp1, method = "mle")
Coefficients:
1
2
-0.5545 -0.2698
9
-0.2051
Order selected 9

3
-0.3396

4
-0.3988

5
-0.3697

sigma^2 estimated as

6
-0.2396

0.0001001

m2=arima(gnp,order=c(9,0,0))
Box.test(m2$resid,lag=log(length(gnp)),type='Ljung')
##
## Box-Ljung test
##
## data: m2$resid
## X-squared = 0.16379, df = 5.1705, p-value = 0.9996

7
-0.1212

8
-0.1999

which((1-pnorm(abs(m2$coef)/sqrt(diag(m2$var.coef))))*2 > 0.05)


## ar3 ar4 ar5 ar6 ar7 ar8 ar9
##
3
4
5
6
7
8
9
hw <- HoltWinters (gnp)
plot(hw)

0.04
0.02
0.00
0.02

Observed / Fitted

HoltWinters filtering

1948

1950

1952

1954
Time

gnp.predict <- predict(hw, n.ahead = 4 * 12)


ts.plot(gnp, gnp.predict, lty = 1:2)

1956

1958

1960

1962

0.04
0.02
0.00
0.02

1950

1955

1960

Time

Log transformations in Harmonic seasonal model


data("AirPassengers")
AP<-AirPassengers
plot(AP)

1965

600
500
400
100

200

300

AP

1950

1952

1954

1956
Time

plot(log(AP))

1958

1960

6.5
6.0
5.5
5.0

log(AP)

1950

1952

1954

1956

1958

Time

SIN <- COS <- matrix(nr = length(AP), nc = 6)


for (i in 1:6) {
SIN[, i] <- sin(2 * pi * i * time(AP))
COS[, i] <- cos(2 * pi * i * time(AP))
}
TIME <- (time(AP) - mean(time(AP)))/sd(time(AP))
AP.lm1 <- lm(log(AP) ~ TIME + I(TIME^2) + I(TIME^3) + I(TIME^4) +
SIN[,1] + COS[,1] + SIN[,2] + COS[,2] + SIN[,3] + COS[,3] +
SIN[,4] + COS[,4] + SIN[,5] + COS[,5] + SIN[,6] + COS[,6])
summary(AP.lm1)
##
##
##
##
##
##
##
##
##
##
##
##
##
##

Call:
lm(formula = log(AP) ~ TIME + I(TIME^2) + I(TIME^3) + I(TIME^4) +
SIN[, 1] + COS[, 1] + SIN[, 2] + COS[, 2] + SIN[, 3] + COS[,
3] + SIN[, 4] + COS[, 4] + SIN[, 5] + COS[, 5] + SIN[, 6] +
COS[, 6])
Residuals:
Min
1Q
-0.132003 -0.036965

Median
0.003728

3Q
0.028642

Max
0.104841

Coefficients:
(Intercept)

Estimate Std. Error t value Pr(>|t|)


5.588e+00 7.503e-03 744.685 < 2e-16 ***
10

1960

##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

TIME
4.269e-01
I(TIME^2)
-6.570e-02
I(TIME^3)
-3.900e-03
I(TIME^4)
1.110e-02
SIN[, 1]
2.773e-02
COS[, 1]
-1.476e-01
SIN[, 2]
5.890e-02
COS[, 2]
5.662e-02
SIN[, 3]
-2.742e-02
COS[, 3]
-8.831e-03
SIN[, 4]
-3.208e-02
COS[, 4]
1.102e-02
SIN[, 5]
-2.134e-02
COS[, 5]
5.807e-03
SIN[, 6]
1.662e+07
COS[, 6]
-3.961e-03
--Signif. codes: 0 '***'

1.007e-02 42.382 < 2e-16 ***


1.578e-02 -4.162 5.77e-05 ***
5.190e-03 -0.751 0.453746
5.927e-03
1.873 0.063429 .
5.696e-03
4.868 3.28e-06 ***
5.664e-03 -26.055 < 2e-16 ***
5.666e-03 10.395 < 2e-16 ***
5.660e-03 10.004 < 2e-16 ***
5.661e-03 -4.844 3.64e-06 ***
5.660e-03 -1.560 0.121216
5.662e-03 -5.666 9.31e-08 ***
5.662e-03
1.946 0.053874 .
5.667e-03 -3.766 0.000252 ***
5.660e-03
1.026 0.306861
1.107e+08
0.150 0.880928
7.598e-03 -0.521 0.603006
0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.048 on 127 degrees of freedom


Multiple R-squared: 0.9895, Adjusted R-squared: 0.9882
F-statistic:
748 on 16 and 127 DF, p-value: < 2.2e-16

AP.lm2 <- lm(log(AP) ~ TIME + I(TIME^2) + SIN[,1] + COS[,1] +


SIN[,2] + COS[,2] + SIN[,3] + SIN[,4] + COS[,4] + SIN[,5])
summary(AP.lm2)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

Call:
lm(formula = log(AP) ~ TIME + I(TIME^2) + SIN[, 1] + COS[, 1] +
SIN[, 2] + COS[, 2] + SIN[, 3] + SIN[, 4] + COS[, 4] + SIN[,
5])
Residuals:
Min
1Q
-0.129656 -0.035765

Median
0.003219

3Q
0.030055

Max
0.113226

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.579298
0.006047 922.625 < 2e-16 ***
TIME
0.420073
0.004058 103.521 < 2e-16 ***
I(TIME^2)
-0.037381
0.004539 -8.236 1.47e-13 ***
SIN[, 1]
0.028083
0.005713
4.916 2.56e-06 ***
COS[, 1]
-0.147186
0.005702 -25.813 < 2e-16 ***
SIN[, 2]
0.059062
0.005704 10.355 < 2e-16 ***
COS[, 2]
0.056798
0.005702
9.961 < 2e-16 ***
SIN[, 3]
-0.027312
0.005702 -4.790 4.39e-06 ***
SIN[, 4]
-0.031997
0.005701 -5.612 1.11e-07 ***
COS[, 4]
0.011115
0.005702
1.949 0.053353 .
SIN[, 5]
-0.021269
0.005701 -3.731 0.000282 ***
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.04838 on 133 degrees of freedom
11

## Multiple R-squared: 0.9888, Adjusted R-squared: 0.988


## F-statistic: 1178 on 10 and 133 DF, p-value: < 2.2e-16
pacf(resid(AP.lm2))
library(nlme)

0.2
0.2

0.0

Partial ACF

0.4

0.6

Series resid(AP.lm2)

10

15
Lag

AP.gls <- gls(log(AP) ~ TIME + I(TIME^2) + SIN[,1] + COS[,1] +


SIN[,2] + COS[,2] + SIN[,3] + SIN[,4] + COS[,4] + SIN[,5],cor = corAR1(0.6))
plot(1:length(AP),log(AP),type="l")
lines(1:length(AP),predict(AP.lm2),col="red",type="l")
lines(1:length(AP),predict(AP.gls),col="blue",type="l")

12

20

6.5
6.0
5.5
5.0

log(AP)

20

40

60

80

1:length(AP)

par(mfrow=c(2,2))
plot(AP.lm2)

13

100

120

140

62

5.0

5.5

6.0

Normal QQ
2

0
3

17

Standardized residuals

0.05
0.15

Residuals

Residuals vs Fitted
2

62 17

5.0

5.5

6.0

Residuals vs Leverage
0

Cook's distance

62

1.0

17

Standardized residuals

ScaleLocation
2

Theoretical Quantiles

0.0

Standardized residuals

Fitted values

62
17

0.00

Fitted values

0.04

0.08

Leverage

par(mfrow=c(1,1))
new.t <- time(ts(start = 1961, end = c(1970, 12), fr = 12))
TIME <- (new.t - mean(time(AP)))/sd(time(AP))
SIN <- COS <- matrix(nr = length(new.t), nc = 6)
for (i in 1:6) {
COS[, i] <- cos(2 * pi * i * new.t)
SIN[, i] <- sin(2 * pi * i * new.t)
}
new.dat <- data.frame(TIME = as.vector(TIME), SIN = SIN,
COS = COS)
AP.pred.ts <- exp(ts(predict(AP.lm2, new.dat), st = 1961,
fr = 12))
ts.plot(log(AP), log(AP.pred.ts), lty = 1:2)

14

0.12

7.0
6.5
6.0
5.5
5.0

1950

1955

1960
Time

ts.plot(AP, AP.pred.ts, lty = 1:2)

15

1965

1970

1000
800
600
400
200

1950

1955

1960
Time

sigma <- summary(AP.lm2)$sigma


lognorm.correction.factor <- exp((1/2) * sigma^2)
empirical.correction.factor <- mean(exp(resid(AP.lm2)))
AP.pred.ts <- AP.pred.ts * empirical.correction.factor
ts.plot(AP, AP.pred.ts, lty = 1:2)

16

1965

1970

1000
800
600
400
200

1950

1955

1960

1965

1970

Time

Reference:
Cowpertwait, Paul SP, and Andrew V. Metcalfe. Introductory time
series withR. Springer Science & Business Media, 2009.

17

You might also like