Applied Econometrics with
Time Series
Overview
Chapter 6
Time Series
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 0 / 67
Overview
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 1 / 67
Overview
Time series data: typical in macroeconomics and finance
Background reading: Brockwell and Davis (2002): Introduction to Time Series and Forecasting, 2nd edition.
Notation: yt , t = 1, . . . , n.
Brockwell and Davis (1991): Time Series – Theory and Methods, 2nd edition.
Contents: Infrastructure and “naive” methods ARMA modeling Stationarity, unit roots, and cointegration Time series regression and structural change Extensions (GARCH, structural time series models)
Franses (1998): Time Series Models for Business and Economic Forecasting Hamilton (1994): Time Series Analysis ...
Focus is on time domain methodology.
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 2 / 67
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 3 / 67
Classes for time series data Standard time series class in R is “ts”: Aimed at regular series (annual, quarterly, monthly). A “ts” object is either a numeric vector (univariate series) or a numeric matrix (multivariate series).
Time Series
"tsp" attribute reflects time series properties:
Infrastructure and “Naive” Methods
a vector of length 3 with start, end and frequency. Create via ts(): supply data (numeric vector or matrix) plus arguments start, end, and frequency. Methods for standard generic functions: plot(), lines(), str(), summary(), . . . Additional time-series-specific methods: lag(), diff(), . . . .
Applied Econometrics with R – 6 – Time Series – 4 / 67
Classes for time series data 60000
Example: Quarterly consumption of non-durables in the United Kingdom (from Franses 1998) Plot: UKNonDurables
R> data("UKNonDurables") R> plot(UKNonDurables)
Time series properties: R> tsp(UKNonDurables) 4
30000
[1] 1955 1989
Applied Econometrics with R – 6 – Time Series – 5 / 67
50000
Classes for time series data
Christian Kleiber, Achim Zeileis © 2008–2015
40000
Christian Kleiber, Achim Zeileis © 2008–2015
Subsets via window(): R> window(UKNonDurables, end = c(1956, 4))
1955
Qtr1 Qtr2 Qtr3 Qtr4 1955 24030 25620 26209 27167 1956 24620 25972 26285 27659 Christian Kleiber, Achim Zeileis © 2008–2015
1960
1965
1970
1975
1980
1985
1990
Time
Applied Econometrics with R – 6 – Time Series – 6 / 67
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 7 / 67
Classes for time series data
(Linear) filtering Linear filter: important class are finite moving averages
Drawbacks of “ts”: Only numeric time stamps (more general date/time classes?) Missing values cannot be omitted (start/end/frequency no longer sufficient for reconstructing all time stamps!) – a problem with irregular series, e.g., with many financial time series. R packages for irregular series: several, we use zoo Generalization of “ts”: time stamps of arbitrary type. Numeric vectors or matrices, "index" attribute contains vector of time stamps (not just "tsp" attribute!). Regular series can be coerced back and forth between “ts” and “zoo” via as.zoo() and as.ts(). “zoo” more convenient for daily data (e.g., “Date” time stamps) or intraday data (e.g., “POSIXct” or “chron” time stamps). More details: Zeileis and Grothendieck (JSS 2005). Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 8 / 67
(Linear) filtering
yˆt =
s X
aj yt +j ,
j =−r
t = r + 1, . . . , n − s.
If r = s, filter is called symmetric. In R: function filter() Main argument filter takes vector containing aj s. Can also apply recursive linear filters. Example: (UKDriverDeaths, Harvey and Durbin, JRSS A 1986) R> data("UKDriverDeaths") R> plot(UKDriverDeaths) R> lines(filter(UKDriverDeaths, c(1/2, rep(1, 11), 1/2)/12), + col = 2) Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 9 / 67
(Linear) filtering
2500
Further examples:
rollapply() computes functions on moving data windows:
2000
filter() also provides autoregressive (recursive) filtering. Generate 100 observations from AR(1) process:
1500
UKDriverDeaths
R> plot(rollapply(UKDriverDeaths, 12, sd))
1000
R> set.seed(1234) R> x dd_dec dd_stl plot(dd_dec$trend, ylab = "trend") R> lines(dd_stl$time.series[,"trend"], lty = 2, lwd = 2) Applied Econometrics with R – 6 – Time Series – 12 / 67
Decomposition
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 13 / 67
Decomposition 7.8 7.0
7.4
data
7.8 7.4
1970
1975
1980
1985
−0.15
0.00
remainder
0.15
7.2
7.4
trend
7.6
−0.1
0.1
seasonal
7.67.0 7.4 0.1 0.15−0.1 0.00
random
−0.15
seasonal
7.2
trend
observed
Decomposition of additive time series
1970
Time Christian Kleiber, Achim Zeileis © 2008–2015
1975
1980
1985
time Applied Econometrics with R – 6 – Time Series – 14 / 67
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 15 / 67
Decomposition
Exponential smoothing
7.6
HoltWinters() handles exponential smoothing and generalizations: Recursively reweighted lagged observations for predictions.
7.4
Default: Holt-Winters filter with additive seasonal component. Example: UKDriverDeaths
7.3
trend
7.5
Smoothing parameters determined by minimizing squared prediction error on observed data.
Historical sample up to 1982(12) (before change in legislation). 7.2
Use Holt-Winters to predict observations for 1983 and 1984.
1970
1975
1980
1985
R> plot(dd_hw, dd_pred, ylim = range(UKDriverDeaths)) R> lines(UKDriverDeaths)
Time
Christian Kleiber, Achim Zeileis © 2008–2015
R> dd_past dd_hw dd_pred ar(x) Call: ar(x = x)
Order selected 1
0.4 0.2
Fit autoregression to x via ar():
ACF
set.seed(1234) x R> R> R>
Series x
1.0
In R: acf() and pacf().
15
20
5
10
15
20
Lag
1.29 Applied Econometrics with R – 6 – Time Series – 22 / 67
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 23 / 67
Classical model-based analysis
Classical model-based analysis
0
1
seasonal and nonseasonal differences
3
4
1
Classical model-based analysis
0.5
Partial ACF
0.5
−1.0 1
2
3
4
Christian Kleiber, Achim Zeileis © 2008–2015
1
2
3
4
Lag Applied Econometrics with R – 6 – Time Series – 25 / 67
Classical model-based analysis
Preliminary analysis suggests double differencing (d = 1, D = 1), some AR and MA effects – we use p = 0, 1, 2 and q = 0, 1, 2, low-order seasonal AR and MA parts – we use P = 0, 1 and Q = 0, 1. This gives 36 parameter combinations in total. Manual solution: Set up all parameter combinations via expand.grid(). Fit each SARIMA model using arima() in for() loop. Store resulting BIC extracted from the model. For BIC, use AIC() with k = log(length(nd)).
Christian Kleiber, Achim Zeileis © 2008–2015
4
1.0
Series diff(diff(nd, 4))
1.0
Series diff(diff(nd, 4))
Lag Applied Econometrics with R – 6 – Time Series – 24 / 67
3 Lag
0.0 0
Christian Kleiber, Achim Zeileis © 2008–2015
2
Lag
−1.0
acf(diff(nd), ylim = c(-1, 1)) pacf(diff(nd), ylim = c(-1, 1)) acf(diff(diff(nd, 4)), ylim = c(-1, 1)) pacf(diff(diff(nd, 4)), ylim = c(-1, 1))
ACF
R> R> R> R>
2
0.0
nonseasonal differences
−1.0
−1.0
Empirical ACFs and PACFs for
0.5
Partial ACF
0.0
ACF
0.5
R> nd + R> R> + + R>
nd_pars plot(log(UKNonDurables)) R> lines(nd_pred$pred, col = 2)
0.0 0.6
p value
R> nd_pred R> R> +
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 32 / 67
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 33 / 67
Unit-root tests Available tests:
black white
5000
Augmented Dickey-Fuller (ADF) test: t test of H0 : % = 0 in
∆yt = α + δ t + %yt −1 +
3000
In R: adf.test() from tseries.
k X j =1
φj ∆yt −j + εt .
Phillips-Perron (PP) test: Same idea as ADF, but nonparametric (HAC) correction for autocorrelation. In R: pp.test() from tseries.
1000
PepperPrice
7000
Stationarity, unit roots, and cointegration
data("PepperPrice") plot(PepperPrice, plot.type = "single", col = 1:2) legend("topleft", c("black", "white"), bty = "n", col = 1:2, lty = rep(1,2))
1975
1980
1985
1990
1995
Time
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 34 / 67
Elliott-Rothenberg-Stock (ERS): Same idea as ADF, but GLS detrending. In R: ur.ers() from urca.
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 35 / 67
Unit-root tests
Unit-root tests
ADF in levels:
PP in levels (by default with time trend):
R> library("tseries") R> adf.test(log(PepperPrice[, "white"]))
R> pp.test(log(PepperPrice[, "white"]), type = "Z(t_alpha)")
Augmented Dickey-Fuller Test data: log(PepperPrice[, "white"]) Dickey-Fuller = -1.7, Lag order = 6, p-value = 0.7 alternative hypothesis: stationary
Phillips-Perron Unit Root Test data: log(PepperPrice[, "white"]) Dickey-Fuller Z(t_alpha) = -1.6, Truncation lag parameter = 5, p-value = 0.7 alternative hypothesis: stationary
ADF in first differences: R> adf.test(diff(log(PepperPrice[, "white"]))) Augmented Dickey-Fuller Test data: diff(log(PepperPrice[, "white"])) Dickey-Fuller = -5.3, Lag order = 6, p-value = 0.01 alternative hypothesis: stationary Warning message: In adf.test(diff(log(PepperPrice[, "white"]))) : p-value smaller than printed p-value Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 36 / 67
Stationarity tests
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 37 / 67
Stationarity tests
Kwiatkowski, Phillips, Schmidt and Shin (J. Econometrics 1992):
KPSS without time trend:
Test H0 :
R> kpss.test(log(PepperPrice[, "white"]))
rt ≡ 0 in
KPSS Test for Level Stationarity
yt = dt + rt + εt , where dt deterministic trend,
data: log(PepperPrice[, "white"]) KPSS Level = 0.91, Truncation lag parameter = 3, p-value = 0.01 Warning message: p-value smaller than printed p-value in: kpss.test(log(PepperPrice[, "white"]))
rt random walk,
εt stationary (I (0)) error process. Two variants: dt = α, level stationarity (under H0 ). dt = α + β t, trend stationarity (under H0 ).
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 38 / 67
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 39 / 67
Cointegration
Cointegration
Pepper series exhibit common nonstationary features.
R> po.test(log(PepperPrice))
Cointegration tests in R:
Phillips-Ouliaris Cointegration Test
Engle-Granger two-step method Available in po.test() from tseries (named after Phillips and Ouliaris, Econometrica 1990). Johansen test Full-information maximum likelihood approach in pth-order cointegrated VAR. Error correction form (ECM) is (without deterministic components)
∆yt = Πyt −1 +
Engle-Granger two-step with black pepper regressed on white pepper:
p−1 X j =1
data: log(PepperPrice) Phillips-Ouliaris demeaned = -24, Truncation lag parameter = 2, p-value = 0.02
Suggests both series are cointegrated. Remarks: Test with reverse regression is
po.test(log(PepperPrice[,2:1])) Γj ∆yt −j + εt .
Problem: treatment asymmetric, but concept cointegration demands symmetric treatment!
Trace and lambda-max tests available in ca.jo() from urca. Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 40 / 67
Cointegration
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 41 / 67
Cointegration
Johansen test with constant term
Values of teststatistic and critical values of test:
R> library("urca") R> pepper_jo summary(pepper_jo)
test 10pct 5pct 1pct r dd dd_dat lm(dd ~ dd1 + dd12, data = dd_dat) Call: lm(formula = dd ~ dd1 + dd12, data = dd_dat) Coefficients: (Intercept) 0.421
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 44 / 67
More on fitting dynamic regression models Approach 2: use convenience interface dynlm() from dynlm
Applied Econometrics with R – 6 – Time Series – 45 / 67
Structural change tests Features of UKDriverDeaths: Parameters of time series model unlikely to be stable throughout sample period.
Time series regression with "ts" data: Start = 1970(1), End = 1984(12)
Package strucchange implements large collection of tests for structural change (parameter instability).
Call: dynlm(formula = dd ~ L(dd) + L(dd, 12)) L(dd) 0.431
Christian Kleiber, Achim Zeileis © 2008–2015
dd12 0.511
Decrease in mean number of casualties after policy change.
R> library("dynlm") R> dynlm(dd ~ L(dd) + L(dd, 12))
Coefficients: (Intercept) 0.421
dd1 0.431
Two types of tests:
L(dd, 12) 0.511
Fluctuation tests. Tests based on F statistics.
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 46 / 67
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 47 / 67
Structural change tests
Structural change tests
Fluctuation tests:
Fluctuation tests in strucchange:
Assess structural stability by capturing fluctuation in CUSUMs or MOSUMs of
empirical fluctuation processes via efp(). Result is object of class “efp”.
residuals (OLS or recursive), model scores (empirical estimating functions), or parameter estimates (recursive or rolling).
Idea: under null hypothesis of parameter stability, resulting “fluctuation processes” exhibit limited fluctuation, under alternative of structural change, fluctuation is generally increased. Evidence for structural change if empirical fluctuation process crosses boundary that corresponding limiting process crosses only with probability α.
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 48 / 67
Structural change tests
plot() method for performing test graphically. sctest() method (for structural change test) for traditional significance test. Example: OLS-CUSUM for UKDriverDeaths OLS-CUSUM process: Scaled CUSUM of OLS residuals εˆt = yt − xt> βˆ efp(s) =
1
√ σ ˆ n
bnsc X t =1
Christian Kleiber, Achim Zeileis © 2008–2015
εˆt ,
0 ≤ s ≤ 1.
Applied Econometrics with R – 6 – Time Series – 49 / 67
Structural change tests OLS−based CUSUM test
data: dd_ocus S0 = 1.5, p-value = 0.02 R> plot(dd_ocus)
1.0 0.5 0.0
OLS-based CUSUM test
−0.5
R> sctest(dd_ocus)
−1.0
Test using maximum absolute deviation of efp (default functional)
Empirical fluctuation process
R> library("strucchange") R> dd_ocus dd_fs sctest(dd_fs) supF test data: dd_fs sup.F = 19, p-value = 0.007
Visualization: R> plot(dd_fs, main = "supF test")
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 52 / 67
Structural change tests
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 53 / 67
Structural change tests supF test
20
Further Example: German M1 money demand Lütkepohl, Teräsvirta and Wolters (JAE 1999) use error correction model (ECM) for German M1.
15
GermanM1 contains data from 1961(1) to 1995(4) on per capita
10
Load and set up model R> data("GermanM1") R> LTW m1_re plot(m1_re) Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 55 / 67
Structural change tests
Dating structural changes
RE test (recursive estimates test)
Setup is linear regression model t = nj −1 + 1, . . . , nj ,
j = 1, . . . , m + 1,
1.5
where
0.5
1.0
j = 1, . . . , m segment index, β (j ) segment-specific set of regression coefficients, {n1 , . . . , nm } set of unknown breakpoints (convention: n0 = 0 and nm+1 = n). In R: function breakpoints()
0.0
1985
1990
1995
Time Applied Econometrics with R – 6 – Time Series – 56 / 67
Dating structural changes
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 57 / 67
Dating structural changes
Example: UKDriverDeaths
●
(Intercept) dd1 dd12 1970(1) - 1973(10) 1.458 0.1173 0.6945 1973(11) - 1983(1) 1.534 0.2182 0.5723 1983(2) - 1984(12) 1.687 0.5486 0.2142
−220 −240
R> coef(dd_bp, breaks = 2)
● ● ●
−260
R> dd_bp plot(dd_bp, legend = FALSE, main = "") R> plot(dd) R> lines(fitted(dd_bp, breaks = 2), col = 4) R> lines(confint(dd_bp, breaks = 2))
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 58 / 67
−300
●
Visualization
● ●
●
0
●
●
2
●
4
6
●
●
1.2
Empirical fluctuation process
2.0
yt = xt> β (j ) + εt ,
8
Number of breakpoints
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 59 / 67
7.6
Time Series
7.4
Extensions
7.0
7.2
dd
7.8
Dating structural changes
1970
1975
1980
1985
Time
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 60 / 67
Extensions
Applied Econometrics with R – 6 – Time Series – 61 / 67
Structural time series models
Further packages for time series analysis dse – Multivariate time series modeling with state-space and vector ARMA (VARMA) models. FinTS – R companion to Tsay (2005). forecast – Univariate time series forecasting, including exponential smoothing, state space, and ARIMA models. fracdiff – ML estimation of ARFIMA models and semiparametric estimation of the fractional differencing parameter. longmemo – Convenience functions for long-memory models. mFilter – Time series filters, including Baxter-King, Butterworth, and Hodrick-Prescott. Rmetrics – Some 20 packages for financial engineering and computational finance, including GARCH modeling in fGarch. tsDyn – Nonlinear time series models: STAR, ESTAR, LSTAR. vars – (Structural) vector autoregressive (VAR) models Christian Kleiber, Achim Zeileis © 2008–2015
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 62 / 67
Basic structural model has measurement equation yt = µt + γt + εt ,
εt ∼ N (0, σε2 ) i.i.d.
Seasonal component γt (with frequency s) is
γt + 1 = −
s −1 X j =1
γt +1−j + ωt ,
ωt ∼ N (0, σω2 ) i.i.d.
Local level and trend components are
µt +1 = µt + ηt + ξt , η t + 1 = η t + ζt ,
ξt ∼ N (0, σξ2 ) i.i.d.,
ζt ∼ N (0, σζ2 ) i.i.d.
All error terms mutually independent. In R: R> dd_struct plot(cbind(fitted(dd_struct), residuals(dd_struct))) Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 63 / 67
Structural time series models
GARCH models 3
7.7 7.5
2
7.3
1 0
MarkPound
0.002 7.1 −0.002 −0.006 0.2
−1
0.1 0.0
−2
3 −0.2 −0.1 2 1 0
0
500
−3 −2 −1
residuals(dd_struct) fitted(dd_struct).sea fitted(dd_struct).slope fitted(dd_struct).level
cbind(fitted(dd_struct), residuals(dd_struct))
1000
1500
2000
Time 1970
1975
1980
1985
Time
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 64 / 67
GARCH models
Christian Kleiber, Achim Zeileis © 2008–2015
Applied Econometrics with R – 6 – Time Series – 65 / 67
GARCH models
tseries function garch() fits GARCH(p, q) with Gaussian innovations. Default is GARCH(1, 1): yt
= σt νt ,
σt2
νt ∼ N (0, 1) i.i.d.,
= ω + αyt2−1 + βσt2−1 ,
ω > 0, α > 0, β ≥ 0.
Example: DEM/GBP FX returns for 1984-01-03 through 1991-12-31
Coefficient(s): Estimate Std. Error a0 0.0109 0.0013 a1 0.1546 0.0139 b1 0.8044 0.0160
t value Pr(>|t|) 8.38