Computer lesson IV

advertisement
Computer lesson IV
This computer lesson will be used to learn how to forecast time series.
Kilpisjärvi data (all data)
The whole data set is used (both spring and fall data; see previous lessons on how to prepare
the data).


Separate the data into a long and a short part ("window" is a useful command: The help
file will give you more info.)
Do the necessary processing of the data to control variance, to control the mean, and to
remove seasonal variance.
AR-procedrue
 Use "ar" to determine the order of the model
 Use arima.mle to estimate the parameters of the model
 Use arima.forecast() to forecast the time series. You must supply:
The time series on which you base the forecast
For how many time steps you want to forecast, n=
And the model you use (e.g., model= kilpis86.ar4$model)
 Investigate the content of the object you create
 "Reverse" the process of preparing the data, and plot your result (add/subtract standard
error)
ARMA-procedure
 Repeat the procedure above, but find the parameters of the ARMA-model instead of only
the AR-coefficients (Use AIC/AICC to choose model)
How was the result of the ARMA-procedure in comparison with the AR-procedure?
Why so different / so similar?
__________________________________________
D:\116100328.doc, 12.02.16, page 1 of 9
tmp <read.table("e:\\Kyrre\\Studier\\drgrad\\kurs\\Timeseries\\kilpis.txt")
# tmp <read.table("c:\\Kyrre\\Studier\\drgrad\\kurs\\Timeseries\\kilpis.txt")
kilpis.ln <- ts(log(tmp[,1]+1), start=1953, frequency=2)
kilpis.ln <- log(kilpis+1)
# kilpis.spring <- matrix(0, 45, 1)
for (i in 1:45) {kilpis.spring[i] <- kilpis[(i*2)-1]}
# kilpis.spring <- ts(kilpis.spring, start=1953)
# kilpis.fall <- matrix(0, 44, 1)
for (i in 1:44) {kilpis.fall[i] <- kilpis[i*2]}
# kilpis.fall <- ts(kilpis.fall, start=1953)
__________________________________________
D:\116100328.doc, 12.02.16, page 2 of 9
tsp(kilpis)
[1] 1953 1997
2
kilpis.stl <- stl(kilpis, "periodic")
# plot.stl(kilpis.stl)
par(mfrow=c(1,1))
ts.plot(kilpis, main="Kilpisjärvi data", xlab="Year", ylab="Trap index",
ylim=c(-2, 30))
ts.points(kilpis, pch=28, col=8)
ts.lines(kilpis.stl$seas, col=4)
ts.lines(kilpis.stl$rem, lty=1, col=3)
legend(locator(1), legend=c("Data", "Seasonal effects", "remainder"),
lty=1, col=c(1,4,3))
0 5 10 15 20 25 30
Kilpis jär
Data
S eas onal
effe
remai nder
1960
1970
1980
1990
__________________________________________
D:\116100328.doc, 12.02.16, page 3 of 9
kilpis86 <- window(kilpis.ln, end=c(1986,2))
par(mfrow=c(2,1))
ts.plot(kilpis86, main="Kilpisjärvi data", xlab="Year", ylab="ln(Trap
index)")
ts.points(kilpis86, pch=28, col=8)
kilpis86.stl <- stl(kilpis86, "periodic")
ts.plot(kilpis86.stl$rem - mean(kilpis86), kilpis86.stl$sea, lty=c(1,1),
col=c(1,3))
# mean(kilpis86)
# [1] 1.4439
2
1
0
ln(Trap index)
3
Kilpisjärvi data
1960
1970
1980
-1
0
1
Year
1960
1970
1980
Time
par(mfrow=c(2,2))
boxplot(split(kilpis86, cycle(kilpis86)), names=c("Spring", "Fall"))
__________________________________________
D:\116100328.doc, 12.02.16, page 4 of 9
0 1 2 3
S prF
ing
all
# kilpis86.mean <- kilpis86.stl$rem - mean(kilpis86)
par(mfrow=c(2,2))
acf(kilpis86.mean)
acf(kilpis86.mean, type="partial")
# library(mass)
cpgram(kilpis86.mean)
ar(kilpis86.mean)$aic
# [1] 62.13 27.62 14.00 7.57 0.00 1.99 3.38 4.11 5.94
# [12] 10.27 9.92 9.41 11.13 12.47 13.32 14.95 16.81
6.69
8.69
length(ar(kilpis86.mean)$aic)
# [1] 19
plot(0:18, ar(kilpis86.mean)$aic, xlab="order", ylab="AIC", main="AIC for
AR(p)")
-0.4 PartilACF0. 0.2 0.4 0.6
-0.5 0. ACF 0.5 1.0
S e ri e S
s e
:
ri
k
i
0
2
4
6
8
Lag
0
2
A
k
i
I
l
C
p
i s8
fo
A
I
C
0 10 20 30 40 50
0. 0.2 0.4 0.6 0.8 1.0
Se
r i es:
4
6
8
Lag
0.
0.
0.
0
0.
2
0.
4
1.
6
8
0
0
5
f r equenc y
__________________________________________
D:\116100328.doc, 12.02.16, page 5 of 9
10
15
or der
kilpis86.ar4 <- arima.mle(kilpis86.mean, model=list(order=c(4,0,0)))
# kilpis86.ar4$model$ar
# [1] 0.7502 -0.3007 0.0207 -0.3931
# sqrt(kilpis86.ar4$var.coef)
#
#
#
#
#
ar(1) ar(2) ar(3) ar(4)
ar(1) 0.1149
NA 0.0534 0.0655
ar(2)
NA 0.1483
NA 0.0534
ar(3) 0.0534
NA 0.1483
NA
ar(4) 0.0655 0.0534
NA 0.1149
kilpis86.fore <- arima.forecast(kilpis86.mean, n=21,
model=kilpis86.ar4$model)
kilpis86.fore.mean <- kilpis86.fore$mean + mean(kilpis86.stl$rem) +
kilpis86.stl$sea[1:21]
__________________________________________
D:\116100328.doc, 12.02.16, page 6 of 9
par(mfrow=c(2,1))
ts.plot(window(kilpis.ln, 1987), kilpis86.fore.mean, kilpis86.fore.mean +
1.96*kilpis86.fore$std.err, kilpis86.fore.mean 1.96*kilpis86.fore$std.err, col=c(1,3,1,1), lty=c(1,1,6,6))
ts.points(window(kilpis.ln, 1985), pch=4)
title("Via Seasonal Decomposition")
ts.plot(exp(window(kilpis.ln, 1987))-1, exp(kilpis86.fore.mean)-1,
exp(kilpis86.fore.mean + 1.96*kilpis86.fore$std.err)-1,
exp(kilpis86.fore.mean - 1.96*kilpis86.fore$std.err)-1, col=c(1,3,1,1),
lty=c(1,1,6,6))
ts.points(exp(window(kilpis.ln, 1985))-1, pch=4)
title("On a normal scale")
-1
0
1
2
3
4
Via Seasonal Decomposition
1988
1990
1992
1994
1996
1994
1996
Time
0
20
40
60
On a normal scale
1988
1990
1992
Time
__________________________________________
D:\116100328.doc, 12.02.16, page 7 of 9
# ARMA modelling
kilpis86.arma <- arima.mle(kilpis86.mean, model=list(order=c(2,0,1)),
n.cond=6)
kilpis86.arma$model
$order:
[1] 2 0 1
$ar:
[1] 1.410 -0.808
$ndiff:
[1] 0
$ma:
[1] 0.618
# sqrt(kilpis86.ar4$var.coef)
> aicc(kilpis86.arma$loglik,
[1] 111.85
> aicc(kilpis86.arma$loglik,
[1] 110.95
> aicc(kilpis86.arma$loglik,
[1] 120.03
> aicc(kilpis86.arma$loglik,
[1] 112.51
> aicc(kilpis86.arma$loglik,
[1] 115.55
2, 2, 64)
2, 1, 64)
2, 0, 64)
3, 1, 64)
3, 2, 64)
kilpis86.arma <- arima.mle(kilpis86.mean, model=list(order=c(2,0,1)),
n.cond=6)
kilpis86.fore <- arima.forecast(kilpis86.mean, n=26,
model=kilpis86.arma$model)
kilpis86.fore$mean <- kilpis86.fore$mean + mean(kilpis86.stl$rem) +
kilpis86.stl$sea[1:26]
par(mfrow=c(2,1))
ts.plot(window(kilpis.ln, 1987), kilpis86.fore$mean, kilpis86.fore$mean +
1.96*kilpis86.fore$std.err, kilpis86.fore$mean 1.96*kilpis86.fore$std.err, lty=c(1,1,6,6), col=c(1,3,1,1))
ts.points(window(kilpis.ln, 1987), pch=4)
title("ARMA(2,1) model")
ts.plot(exp(window(kilpis.ln, 1987))-1, exp(kilpis86.fore$mean)-1,
exp(kilpis86.fore$mean + 1.96*kilpis86.fore$std.err)-1,
exp(kilpis86.fore$mean - 1.96*kilpis86.fore$std.err)-1, lty=c(1,1,6,6),
col=c(1,3,1,1))
ts.points(exp(window(kilpis.ln, 1987))-1, pch=4)
title("Natural scale")
__________________________________________
D:\116100328.doc, 12.02.16, page 8 of 9
-1
0
1
2
3
4
ARMA(2,1) model
1988
1990
1992
1994
1996
1998
2000
1996
1998
2000
Time
0
10
20
30
40
50
60
Natural scale
1988
1990
1992
1994
Time
__________________________________________
D:\116100328.doc, 12.02.16, page 9 of 9
Download