0% found this document useful (0 votes)
3K views13 pages

Non-Stationary Models

This document summarizes techniques for modeling non-stationary time series data using R, including differencing, autoregressive integrated moving average (ARIMA) models, and generalized autoregressive conditional heteroskedasticity (GARCH) models. Examples are provided using electricity consumption, beer sales, and temperature data to illustrate differencing, ARIMA modeling, including seasonal ARIMA, and GARCH modeling to account for time-varying volatility. Code and output are included from fitting these models in R.

Uploaded by

api-285777244
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views13 pages

Non-Stationary Models

This document summarizes techniques for modeling non-stationary time series data using R, including differencing, autoregressive integrated moving average (ARIMA) models, and generalized autoregressive conditional heteroskedasticity (GARCH) models. Examples are provided using electricity consumption, beer sales, and temperature data to illustrate differencing, ARIMA modeling, including seasonal ARIMA, and GARCH modeling to account for time-varying volatility. Code and output are included from fitting these models in R.

Uploaded by

api-285777244
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Non-stationary Models

YIK LUN, KEI


[email protected]
This paper is a practice from the book called Introductory Time Series with R
by Cowpertwait, Paul SP, and Andrew V. Metcalfe. All R codes and comments
below are belonged to the book and authors.

Differencing

10000
6000
2000

Elec.ts

14000

CBE <- read.table("http://staff.elena.aut.ac.nz/Paul-Cowpertwait/ts/cbe.dat", header = T)


Elec.ts <- ts(CBE[, 3], start = 1958, freq = 12)
plot(Elec.ts)

1960

1965

1970

1975
Time

plot(diff(Elec.ts))

1980

1985

1990

500
0
500
1500

diff(Elec.ts)

1960

1965

1970

1975
Time

plot(diff(log(Elec.ts)))

1980

1985

1990

0.15
0.05
0.05
0.15

diff(log(Elec.ts))

1960

1965

1970

1975
Time

IMA(1, 1)
Beer.ts <- ts(CBE[, 2], start = 1958, freq = 12)
plot(Beer.ts)

1980

1985

1990

200
150
100

Beer.ts

1960

1965

1970

1975
Time

plot(diff(Beer.ts))

1980

1985

1990

60
40
20
0
20
60

diff(Beer.ts)

1960

1965

1970

1975

1980

1985

Time

Beer.ima <- arima(Beer.ts, order = c(0, 1, 1))


Beer.ima
##
##
##
##
##
##
##
##
##
##

Call:
arima(x = Beer.ts, order = c(0, 1, 1))
Coefficients:
ma1
-0.3334
s.e.
0.0558
sigma^2 estimated as 360.4:

log likelihood = -1723.27,

acf(resid(Beer.ima))

aic = 3450.53

1990

0.4
0.4

0.0

ACF

0.8

Series resid(Beer.ima)

0.0

0.5

1.0

1.5

2.0

Lag

Beer.1991 <- predict(Beer.ima, n.ahead = 12)


sum(Beer.1991$pred)

## [1] 2365.412

Seasonal ARIMA(p, d, q)(P, D, Q)s


get.best.arima <- function(x.ts, maxord = c(1,1,1,1,1,1))
{
best.aic <- 1e8
n <- length(x.ts)
for (p in 0:maxord[1]) for(d in 0:maxord[2]) for(q in 0:maxord[3])
for (P in 0:maxord[4]) for(D in 0:maxord[5]) for(Q in 0:maxord[6]){
fit <- arima(x.ts, order = c(p,d,q),seas = list(order = c(P,D,Q),frequency(x.ts)), method = "CSS")
fit.aic <- -2 * fit$loglik + (log(n) + 1) * length(fit$coef)
if (fit.aic < best.aic){
best.aic <- fit.aic
best.fit <- fit
best.model <- c(p,d,q,P,D,Q)
}
}
list(best.aic, best.fit, best.model)

}
best.arima.elec <- get.best.arima( log(Elec.ts),maxord = c(2,2,2,2,2,2))
## Warning in arima(x.ts, order = c(p, d, q), seas = list(order = c(P, D,
## Q), : possible convergence problem: optim gave code = 1
## Warning in arima(x.ts, order = c(p, d, q), seas = list(order = c(P, D,
## Q), : possible convergence problem: optim gave code = 1
## Warning in arima(x.ts, order = c(p, d, q), seas = list(order = c(P, D,
## Q), : possible convergence problem: optim gave code = 1
best.fit.elec <- best.arima.elec[[2]]
acf( resid(best.fit.elec) )

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series resid(best.fit.elec)

0.0

0.5

1.0
Lag

best.arima.elec [[3]]
## [1] 0 1 1 2 0 2
ts.plot( cbind( window(Elec.ts,start = 1981),
exp(predict(best.fit.elec,12)$pred) ), lty = 1:2)

1.5

2.0

14000
12000
10000
8000

1982

1984

1986

1988

1990

Time

library(tseries)
stemp <- scan("http://staff.elena.aut.ac.nz/Paul-Cowpertwait/ts/stemp.dat")
stemp.ts <- ts(stemp, start = 1850, freq = 12)
plot(stemp.ts)

1992

0.5
0.0
1.0

0.5

stemp.ts

1850

1900

1950

2000

Time

stemp.best <- get.best.arima(stemp.ts, maxord = rep(2,6))


stemp.best[[3]]

## [1] 1 1 2 2 0 1
stemp.arima <- arima(stemp.ts, order = c(1,1,2),seas = list(order = c(2,0,1), 12))
t( confint(stemp.arima) )

##
ar1
ma1
ma2
sar1
sar2
sma1
## 2.5 % 0.8317390 -1.447400 0.3256700 0.8576802 -0.02501883 -0.9690534
## 97.5 % 0.9127946 -1.312553 0.4530475 1.0041396 0.07413424 -0.8507034
stemp.arima <- arima(stemp.ts, order = c(1,1,2),seas = list(order = c(1,0,1), 12))
t( confint(stemp.arima) )

##
ar1
ma1
ma2
sar1
sma1
## 2.5 % 0.8303972 -1.445048 0.3242583 0.9243522 -0.969491
## 97.5 % 0.9108145 -1.311231 0.4508911 0.9956641 -0.867927
stemp.res <- resid(stemp.arima)
acf(stemp.res)

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series stemp.res

0.0

0.5

1.0

1.5
Lag

acf(stemp.res^2)

10

2.0

2.5

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series stemp.res^2

0.0

0.5

1.0

1.5
Lag

stemp.garch <- garch(stemp.res, trace = F)


t(confint(stemp.garch))

##
a0
a1
b1
## 2.5 % 1.064251e-05 0.03299181 0.9249315
## 97.5 % 1.485814e-04 0.06525680 0.9630787
stemp.garch.res <- resid(stemp.garch)[-1]
acf(stemp.garch.res)

11

2.0

2.5

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series stemp.garch.res

10

15
Lag

acf(stemp.garch.res^2)

12

20

25

30

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series stemp.garch.res^2

10

15

20

25

30

Lag

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

13

You might also like