Professional Documents
Culture Documents
1. arima
library("fUnitRoots")
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
Loading
Loading
Loading
Loading
required
required
required
required
package:
package:
package:
package:
urca
timeDate
timeSeries
fBasics
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
1960
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
0.04
0.02
0.00
0.02
Observed / Fitted
HoltWinters filtering
1948
1950
1952
1954
Time
1956
1958
1960
1962
0.04
0.02
0.00
0.02
1950
1955
1960
Time
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
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)
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 '***'
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
0.2
0.2
0.0
Partial ACF
0.4
0.6
Series resid(AP.lm2)
10
15
Lag
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
15
1965
1970
1000
800
600
400
200
1950
1955
1960
Time
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