time series with r - freeexploratory data analysis beginning ts with r how recognising a white noise...
TRANSCRIPT
![Page 1: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/1.jpg)
EDA Identification of ARIMA Case study
Time Series with RSummer School on Mathematical Methods in Finance and Economy
Thibault LAURENT
Toulouse School of Economics
June 2010 (slides modified in August 2010)
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 2: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/2.jpg)
EDA Identification of ARIMA Case study
Exploratory Data AnalysisBeginning TS with RHow recognising a white NoiseOther R tools
Identification of ARIMA
Case study
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 3: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/3.jpg)
EDA Identification of ARIMA Case study
Beginning TS with R
What is a ts object ?
I we simulate a random walk with cumsum and rnorm:> wn <- cumsum(rnorm(240, 0, 1))
I we create a ts object with the function ts, using a frequencyof 12 (we observe a phenomenon monthly), starting in 1990:> wn.ts <- ts(wn, start = 1990, frequency = 12)
I Time can be considered like that: one unit corresponds to oneyear. A year is divided into 12 (months). Thus, it is easy tovisualize on the x-axis the beginning of each year. The valuesof time are given by the time function:> time(wn.ts)
I We can print only a part of the series by using the window
function, here for year 2001:> window(wn.ts, 2001, 2001.95)
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 4: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/4.jpg)
EDA Identification of ARIMA Case study
Beginning TS with R
Plot a ts object
> plot(wn.ts, main = "Simulated Random Walk", xlab = "time in year")
Simulated Random Walk
time in year
wn.
ts
1990 1995 2000 2005 2010
−10
−4
04
User may then use functions lines, points, abline, etc. tocomplete the graphic.
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 5: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/5.jpg)
EDA Identification of ARIMA Case study
Beginning TS with R
The lag plot
We can obtain a lag plot of the observations by using the functionlag.plot, applied here to the LakeHuron data included in R (seehelp(LakeHuron) for more details):
> str(LakeHuron)
> lag.plot(LakeHuron, 9, do.lines = FALSE)
●
●
●●
●
● ●
●
●●●●
●
●
●●
● ●
●●
●●
● ●●
●
●● ●
●●●●●
●
●
●
●
●
●
●
●
●●
●●
●●
●●
● ●
●
●
●
●
●
●●
●
●● ●
●
●
●●
●
●
●●●
●●
●●
●
●
●
●
●
●
●
● ●
●
●
●
●
●
●
●
●●
●
●
●
lag 1
Lake
Hur
on57
657
858
058
2
576 578 580 582
●
●
●●
●
● ●
●
●●●●
●
●
●●
●●
●●
●●
●●●
●
●●●
●●●●●
●
●
●
●
●
●
●
●
●●
●●
●●
●●
● ●
●
●
●
●
●
● ●
●
● ● ●
●
●
●●
●
●
●●●
●●
●●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
●
lag 2
Lake
Hur
on
●
●
●●
●
●●
●
●●●●
●
●
● ●
●●
● ●
●●
● ●●
●
●●●
●●●●●
●
●
●
●
●
●
●
●
●●
●●
●●
●●
● ●
●
●
●
●
●
●●
●
● ●●
●
●
●●
●
●
●●●
●●
●●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
lag 3
Lake
Hur
on
576 578 580 582
●
●
●●
●
●●
●
●●●●
●
●
●●
●●
●●
●●
●●●
●
●●●
●●●● ●
●
●
●
●
●
●
●
●
●●
●●
●●
●●
●●
●
●
●
●
●
●●
●
●●●
●
●
●●
●
●
●●●
●●
●●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
lag 4
Lake
Hur
on
●
●
●●
●
●●
●
●●●●
●
●
●●
● ●
● ●
●●
●●●
●
●●●
●●●● ●
●
●
●
●
●
●
●
●
●●
●●
●●
●●
●●
●
●
●
●
●
● ●
●
●● ●
●
●
●●
●
●
●● ●
●●
●●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
lag 5
Lake
Hur
on
●
●
●●
●
●●
●
●●●●
●
●
●●
●●
●●
●●
● ●●
●
●●●
●● ●●●
●
●
●
●
●
●
●
●
●●
●●
●●
●●
●●
●
●
●
●
●
● ●
●
● ● ●
●
●
●●
●
●
●● ●
●●
●●
●
●
●
●
●
●
●
● ●
●
●
●
●
●
●
●
lag 6
Lake
Hur
on
576
578
580
582
●
●
●●
●
●●
●
●●●●
●
●
● ●
● ●
● ●
●●
●●●
●
●●●
● ● ●●●
●
●
●
●
●
●
●
●
●●
●●
●●
●●
●●
●
●
●
●
●
●●
●
● ●●
●
●
●●
●
●
●●●
●●
●●
●
●
●
●
●
●
●
● ●
●
●
●
●
●
●
lag 7
Lake
Hur
on57
657
858
058
2
●
●
●●
●
●●
●
●● ●●
●
●
●●
●●
●●
●●
●●●
●
●●●
● ●●● ●
●
●
●
●
●
●
●
●
●●
●●
●●
●●
●●
●
●
●
●
●
●●
●
●●●
●
●
●●
●
●
●●●
●●
●●
●
●
●
●
●
●
●
● ●
●
●
●
●
●
lag 8
Lake
Hur
on
576 578 580 582
●
●
●●
●
●●
●
● ●●●
●
●
● ●
● ●
●●
●●
●●●
●
●● ●
●●●● ●
●
●
●
●
●
●
●
●
● ●
●●
●●
●●
● ●
●
●
●
●
●
● ●
●
●●●
●
●
●●
●
●
●●●
●●
●●
●
●
●
●
●
●
●
●●
●
●
●
●
lag 9
Lake
Hur
on
Obviously, the time series is strongly auto-correlated...
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 6: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/6.jpg)
EDA Identification of ARIMA Case study
Beginning TS with R
ACF/PACF graphic
ACF graphic
> acf(wn, ylim = c(-1, 1))
0 5 10 15 20
−1.
00.
01.
0
Lag
AC
F
Series wn
PACF
> pacf(wn, ylim = c(-1, 1))
5 10 15 20
−1.
00.
01.
0
LagP
artia
l AC
F
Series wn
In this case, the series is not stationary (ACF decreasing nonexponentially)
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 7: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/7.jpg)
EDA Identification of ARIMA Case study
Beginning TS with R
Structural decomposition
You can draw a decomposition of the series in trend + season +error in the case of a series with seasonality (defined withfrequency option in function ts) by using the function stl. Forexample, with the nottem data:> plot(stl(nottem, "per"))
3040
5060
data
−10
05
10
seas
onal
4849
5051
tren
d
−6
−2
02
4
1920 1925 1930 1935 1940
rem
aind
er
time
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 8: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/8.jpg)
EDA Identification of ARIMA Case study
How recognising a white Noise
A qq-plot graphic
In the following slides, we present some tools which are useful fordetecting a white noiseA gaussian sample:
> s.norm <- rnorm(250, 0, 1)
> qqnorm(s.norm, col = "blue")
> qqline(s.norm, col = "red")
●
●
●
●
●
●
●
●
●
●
●●●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●●●
●
●
●●
●●
●●
●
●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●●
●
●
●●
●●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●●
●
●●
●
●●
●
●●
●
●
●●
●
●
−3 −2 −1 0 1 2 3
−4
−3
−2
−1
01
2
Normal Q−Q Plot
Theoretical Quantiles
Sam
ple
Qua
ntile
s
The random walk series:
> qqnorm(wn, col = "blue")
> qqline(wn, col = "red")
●●●
●
●
●●
●
●
●
●●
●
●●
●
●
●●●●
●
●
●●
●●●●
●
●
●
●
●
●●
●
●
●●
●
●●
●●
●
●
●
●
●●
●
●
●
●●
●
●
●●
●●
●
●
●●
●
●
●●
●●
●●
●
●●●
●
●●
●●
●●
●
●●●●
●
●●
● ●
●●●●
●
●●
●●
●
●●
●
●
●
●●
●●
●●●
●●
●●
●
●●
●●●
●
●●●
●
●●
●
●●
●●●●
●
●
●●●●●
●●
●●
●●●●
●
●
●●●●
●●
●●
●●●●●●
●●●
●
●
●
●
●●
●●
●●
●●●
●
●
●●
●●●
●●
●●
●●●
●●
●
●●
●●
●●
●
●●
●●
●●
●●
●●
●●●
●
●●●
●●
●
●●
●
●●●●
●
−3 −2 −1 0 1 2 3
−10
−8
−6
−4
−2
02
4
Normal Q−Q Plot
Theoretical Quantiles
Sam
ple
Qua
ntile
s
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 9: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/9.jpg)
EDA Identification of ARIMA Case study
How recognising a white Noise
Tests based on Skewness/Kurtosis values
Skewness/Kurtosis may be close to 0 if the series is white noise.
> require(fUtilities)
> skewness(wn)
[1] -0.8164437
attr(,"method")
[1] "moment"
> kurtosis(wn)
[1] -0.1958579
attr(,"method")
[1] "excess"
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 10: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/10.jpg)
EDA Identification of ARIMA Case study
How recognising a white Noise
Tests of normality
Jarque-Bera test:> require(tseries)
> jarque.bera.test(wn)
Jarque Bera Test
data: wn
X-squared = 27.2963, df = 2, p-value = 1.182e-06
Shapiro-Wilk normality test:> shapiro.test(wn)
Shapiro-Wilk normality test
data: wn
W = 0.9218, p-value = 6.243e-10
For the random walk, the hypothesis of normality is not acceptedin the two tests...
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 11: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/11.jpg)
EDA Identification of ARIMA Case study
How recognising a white Noise
Ljung-Box statistic
Use of the function Box.test for examining the null hypothesis ofindependence in a given time series.
> Box.test(wn, lag = 1, type = "Ljung-Box")
> Box.test(wn, lag = 2, type = "Ljung-Box")
> Box.test(wn, lag = 3, type = "Ljung-Box")
Shortcoming of this function: it can be applied only lag by lag. Toappear soon: package outilST developped by Aragon (2010).
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 12: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/12.jpg)
EDA Identification of ARIMA Case study
Other R tools
Other R tools
I The function Lag included in Hmisc computes a lag vector:> require(Hmisc)
> Lag(1:10, 2)
I The function diff and diffinv returns respectively thevectors ∆yt = yt+1 − yt and ∆−1yt :> diff(cumsum(1:10))
> diffinv(cumsum(1:10))
I The function lowess may be used to smooth the time series:> plot(wn.ts, main = "Simulated Random Walk", xlab = "time in year")
> lines(lowess(wn.ts), col = "blue", lty = "dashed")
I See also http://cran.r-project.org/doc/contrib/
Ricci-refcard-ts.pdf
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 13: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/13.jpg)
EDA Identification of ARIMA Case study
Exploratory Data Analysis
Identification of ARIMAAR simulated examplesARIMA simulated examplesOther R tools
Case study
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 14: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/14.jpg)
EDA Identification of ARIMA Case study
AR simulated examples
Case of an AR(1)
Consider the following AR(1) model :
yt = −18 − 0.8yt−1 + zt , t = 1, ..., 200
with zt ∼ N(0, 1.5). Notice that E(Y ) = −10.How can we simulate this series and analyze it ?
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 15: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/15.jpg)
EDA Identification of ARIMA Case study
AR simulated examples
Simulation
1. Simulate the white noise zt :> set.seed(951)
> n2 = 250
> noise = rnorm(n2, 0, sqrt(1.5))
2. Apply the recurrence relation yt = −0.8 × yt−1 + (zt − 18) byusing the function filter with a initial valuey0 = E(Y ) = −10 (init=-10):> noise18 = noise - 18
> y.n = filter(noise18, c(-0.8), side = 1, method = "recursive",
+ init = -10)
3. Delete the beginning of the series:> y.n = y.n[-c(1:50)]
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 16: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/16.jpg)
EDA Identification of ARIMA Case study
AR simulated examples
Representation of the series
> plot(y.n, type = "l", xlab = "time", main = "Simulated AR(1)")
0 50 100 150 200
−14
−8
−4
Simulated AR(1)
time
y.n
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 17: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/17.jpg)
EDA Identification of ARIMA Case study
AR simulated examples
Analysis of the ACF/PACF graphic
> op <- par(mfrow = c(2, 1))
> acf(y.n, main = "ACF and PACF of the simulated AR(1)",
+ ylim = c(-1, 1))
> pacf(y.n, main = "", ylim = c(-1, 1))
> par(op)
0 5 10 15 20
−1.
00.
01.
0
Lag
AC
F
ACF and PACF of the simulated AR(1)
5 10 15 20
−1.
00.
01.
0
Lag
Par
tial A
CF
The analysis of the ACF (decreasing exponentially) and the PACF(close to 0 for p > 1) strongly suggest an AR(1).
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 18: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/18.jpg)
EDA Identification of ARIMA Case study
AR simulated examples
Fitting an AR(1)
We may use the function arima to fit an ARIMA(p,d,q).
> (y.fit = arima(y.n, order = c(1, 0, 0)))
Call:
arima(x = y.n, order = c(1, 0, 0))
Coefficients:
ar1 intercept
-0.7899 -10.0269
s.e. 0.0432 0.0497
sigma^2 estimated as 1.574: log likelihood = -329.65, aic = 665.29
Notice that the value associated to intercept is not the intercept,but the mean (see http:
//www.stat.pitt.edu/stoffer/tsa2/Rissues.htm#Issue1)
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 19: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/19.jpg)
EDA Identification of ARIMA Case study
AR simulated examples
Diagnostic plots
The object created by arima contains several informations and maybe used by the function tsdiag which plots different graphics forchecking that the residuals are white noise.
> tsdiag(y.fit)
Standardized Residuals
Time
0 50 100 150 200
−3
−1
1
0 5 10 15 20
0.0
0.4
0.8
Lag
AC
F
ACF of Residuals
●● ● ●
●●
●●
●●
2 4 6 8 10
0.0
0.4
0.8
p values for Ljung−Box statistic
lag
p va
lue
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 20: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/20.jpg)
EDA Identification of ARIMA Case study
AR simulated examples
Forecasting
Forecast 10 ahead:> y.fore = predict(y.fit, n.ahead = 10)
> U = y.fore$pred + 2 * y.fore$se
> L = y.fore$pred - 2 * y.fore$se
> miny = min(y.n, L)
> maxy = max(y.n, U)
> ts.plot(window(ts(y.n, 150, 200)), y.fore$pred, col = 1:2,
+ ylim = c(miny, maxy), main = "Forecast 10 ahead")
> lines(U, col = "blue", lty = "dashed")
> lines(L, col = "blue", lty = "dashed")
Forecast 10 ahead
Time
150 160 170 180 190 200 210
−14
−8
−4
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 21: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/21.jpg)
EDA Identification of ARIMA Case study
ARIMA simulated examples
Case of an ARIMA(1,1,2)
Consider the following ARMA(1,2) model :
yt = −0.9 − 0.8yt−1 + zt − 0.3zt−1 + 0.6zt−2, t = 1, ..., 200
with zt ∼ N(0, 4). We can re-write it as:
yt = −0.5 + 1−0.3B+0.6B2
1+0.8B zt , t = 1, ..., 200
The following model is an ARIMA(1,1,2):
∆yt = −0.5 + 1−0.3B+0.6B2
1+0.8B zt , t = 1, ..., 200
How can we simulate this series and analyze it ?
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 22: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/22.jpg)
EDA Identification of ARIMA Case study
ARIMA simulated examples
Simulation
1. simulate an ARMA(1,2) with the function arima.sim:> set.seed(121181)
> yd.n = -0.5 + arima.sim(n = 200, list(ar = -0.8, ma = c(-0.3,
+ 0.6)), sd = 2, n.start = 50)
2. apply the function diffinv to the previous ARMA(1,2):> y2.int <- diffinv(yd.n)
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 23: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/23.jpg)
EDA Identification of ARIMA Case study
ARIMA simulated examples
ACF and PACF of the initial series
The analysis of the ACF (decreasing non exponentially) confirmsthe non stationarity of the series.
> op <- par(mfrow = c(3, 1))
> plot(y2.int, type = "l", xlab = "time", main = "Simulated ARIMA(1,1,1)")
> acf(y2.int, main = "", ylim = c(-1, 1))
> pacf(y2.int, main = "", ylim = c(-1, 1))
> par(op)
Simulated ARIMA(1,1,1)
time
y2.in
t
0 50 100 150 200
−80
−40
0
0 5 10 15 20
−1.
00.
01.
0
Lag
AC
F
5 10 15 20
−1.
00.
01.
0
Lag
Par
tial A
CF
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 24: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/24.jpg)
EDA Identification of ARIMA Case study
ARIMA simulated examples
ACF and PACF of the differenciated series
The analysis of the differenciated series suggests an ARMA (ACFand PACF decrease exponentially).> diff.y2.int <- diff(y2.int)
> op <- par(mfrow = c(3, 1))
> plot(diff.y2.int, type = "l", xlab = "time", main = "Differenciated series")
> acf(diff.y2.int, main = "", ylim = c(-1, 1))
> pacf(diff.y2.int, main = "", ylim = c(-1, 1))
> par(op)Simulated ARIMA(1,1,1)
time
diff.
y2.in
t
0 50 100 150 200
−10
05
0 5 10 15 20
−1.
00.
01.
0
Lag
AC
F
ACF and PACF of the simulated ARIMA(1,1,1)
5 10 15 20
−1.
00.
01.
0
Lag
Par
tial A
CF
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 25: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/25.jpg)
EDA Identification of ARIMA Case study
ARIMA simulated examples
Identification of an ARMA(p,q)
I apply the methodology for selecting parameters in an OLSmodel such as “backward” or “forward”, by using the functionarima. The function t_stat in package outilST will give thep-values of each parameter.
I The MINIC (Minimum Information Criterion) method may beused to identify the parameters p and q (to appear soon:function armaselect in package outilST) which compares aspecific criteria in several models.
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 26: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/26.jpg)
EDA Identification of ARIMA Case study
ARIMA simulated examples
MINIC method
armaselect of package outilST returns Schwartz’ BayesianCriterion (SBC) value for different models:
> armaselect(diff.y2.int, max.p = 15, max.q = 15)
p q sbc
[1,] 1 2 268.9689
[2,] 2 2 270.8024
[3,] 1 3 271.5127
[4,] 1 4 274.8745
[5,] 5 0 274.8792
It gives the ARMA(1,2) as the best model...
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 27: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/27.jpg)
EDA Identification of ARIMA Case study
ARIMA simulated examples
Fitting an ARIMA(1,1,2)
We may use the function Arima included in package forecast to fitan ARIMA(1,1,2) to the initial series.> require(forecast)
This is forecast 2.04
> (y2.fit = Arima(y2.int, order = c(1, 1, 2), include.drift = TRUE))
Series: y2.int
ARIMA(1,1,2) with drift
Call: Arima(x = y2.int, order = c(1, 1, 2), include.drift = TRUE)
Coefficients:
ar1 ma1 ma2 drift
-0.8175 -0.2345 0.4635 -0.4593
s.e. 0.0475 0.0719 0.0743 0.0891
sigma^2 estimated as 3.473: log likelihood = -409.5
AIC = 828.99 AICc = 829.3 BIC = 845.48Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 28: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/28.jpg)
EDA Identification of ARIMA Case study
Other R tools
Unit root tests
The package urca contains two functions useful to detect apossible non stationarity in the series:
I The function ur.df computes the Augmented Dickey-Fullertest. The choice of test (option test=’trend’ ortest=’drift’) may be suggested by the series itself...
I The function ur.kpss computes the Kwiatkowski test with thedifferent options type=’tau’ or type=’mu’.
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 29: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/29.jpg)
EDA Identification of ARIMA Case study
Other R tools
Fit an ARMAX or SARIMA
I The option xreg in Arima may be used to fit an ARMAX. Forexample, to adjust the model yt = β0 + β1xt + ut ,ut = φut−1 + zt , t = 1...T :> temps = time(LakeHuron)
> mod1.lac = Arima(LakeHuron, order = c(1, 0, 0), xreg = temps,
+ method = "ML")
I the option seasonal=list(order=c(P,D,Q),period=per) maybe used to fit a SARIMA. For example:> fitm = Arima(nott1, order = c(1, 0, 0), list(order = c(2,
+ 1, 0), period = 12))
> summary(fitm)
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 30: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/30.jpg)
EDA Identification of ARIMA Case study
Exploratory Data Analysis
Identification of ARIMA
Case study
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 31: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/31.jpg)
EDA Identification of ARIMA Case study
A case study
1. Choose a series on http://fr.finance.yahoo.com/ andfind the Code. Here, we choose the Danone stock price(Code=BP.NA)
2. Import the series by using function priceIts of package its> require(its)
> danone = priceIts(instrument = "BN.PA", start = "2008-01-03",
+ end = "2010-07-31", quote = "Close")
> str(danone)
3. missing values ?> manq = complete.cases(danone) == FALSE
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 32: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/32.jpg)
EDA Identification of ARIMA Case study
Representation of the series
> plot(danone, main = "Danone quotation the last 2 years",
+ ylab = "in euros")35
4555
Danone quotation the last 2 years
xxx[
vpoi
nts,
j]
2008 2009 2010
In general, with a financial series, we are interested by the returnyt−yt−1
yt−1.
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 33: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/33.jpg)
EDA Identification of ARIMA Case study
Analysis of the returns
The function returns in package fSeries computes the returns andthe function its creates an irregular time series:
> library(fSeries)
> y.ret <- its(returns(danone, percentage = TRUE), danone@dates)
Kurtosis and Skewness tests indicate a strong heteroscedasticity(high value of kurtosis) with more negative returns than positivereturns (negative value of skewness).
> require(fBasics)
> dagoTest(y.ret)
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 34: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/34.jpg)
EDA Identification of ARIMA Case study
Representation of the returns and their square
We notice at the end of 2008 a strong variation...> op <- par(mfrow = c(2, 1))
> plot(y.ret, main = "Returns of the Danone quotation",
+ ylab = "percent")
> plot(y.ret^2, main = "Square returns of the Danone quotation",
+ ylab = "percent^2")
> par(op)
−5
05
Returns of the Danone quotation
perc
ent
2008 2009 2010
020
60
Square returns of the Danone quotation
perc
ent²
2008 2009 2010
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 35: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/35.jpg)
EDA Identification of ARIMA Case study
Analysis of the ACF/PACF graph
> op <- par(mfrow = c(2, 1))
> acf(na.omit([email protected]), ylim = c(-1, 1), main = "ACF and PACF of the returns")
> pacf(na.omit([email protected]), ylim = c(-1, 1), main = "")
> par(op)
0 5 10 15 20 25
−1.
00.
01.
0
Lag
AC
F
ACF and PACF of the returns
0 5 10 15 20 25
−1.
00.
01.
0
Lag
Par
tial A
CF
We thus try to adjust an AR(2)
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 36: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/36.jpg)
EDA Identification of ARIMA Case study
Fit a first model
> (ret.fit = Arima(na.omit([email protected]), order = c(2, 0,
+ 0), include.mean = FALSE))
> t_stat(ret.fit)
> tsdiag(ret.fit)
This model seems acceptable but we must verify whether there isheteroscedasticity in the residuals.
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 37: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/37.jpg)
EDA Identification of ARIMA Case study
Conditional heteroscedasticty test
The function ArchTest included in package FinTS computes aconditional heteroscedasticty test:
> require(FinTS)
> rr <- ret.fit$residuals
> ArchTest(rr, lag = 12)
As we observe heteroscedasticty, we try to fit a GARCH(1,1), withthe function garchFit included in package fGarch:
> require(fGarch)
> res.garch <- garchFit(~garch(1, 1), data = rr, trace = FALSE,
+ na.action = na.pass)
> summary(res.garch)
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 38: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/38.jpg)
EDA Identification of ARIMA Case study
GARCH
To combine the AR(2) with the GARCH(1,1) applied to theresiduals, we compute the following model:
> res2.garch <- garchFit(~arma(2, 0) + garch(1, 1), data = na.omit([email protected]),
+ include.mean = FALSE, trace = FALSE, na.action = na.pass)
> summary(res2.garch)
The model can finally be written as :yt = −0.83yt−1 − 0.88yt−2 + εt with εt = σtzt withσ2t = 0.07 + 0.1εt−1 + 0.88σ2t−1
Thibault LAURENT Toulouse School of Economics
Time series with R
![Page 39: Time Series with R - FreeExploratory Data Analysis Beginning TS with R How recognising a white Noise Other R tools Identi cation of ARIMA Case study Thibault LAURENT Toulouse School](https://reader033.vdocuments.net/reader033/viewer/2022051900/5feeb82a1b834b013c5b90f0/html5/thumbnails/39.jpg)
EDA Identification of ARIMA Case study
Prediction of a GARCH
> pred.zcond = predict(res2.garch, n.ahead = 30, trace = FALSE,
+ mse = "cond", plot = TRUE)
0 50 100 150 200
−4
−2
02
46
Index
x
Prediction with confidence intervals
X̂t+h
X̂t+h − 1.96 MSEX̂t+h + 1.96 MSE
Thibault LAURENT Toulouse School of Economics
Time series with R