Technical Tips On Time Series With Stata: Gustavo Sánchez
Technical Tips On Time Series With Stata: Gustavo Sánchez
with Stata
Gustavo Sánchez
Senior Statistician
StataCorp
clear
input str12 date
"1/01/2008"
"1/02/2008"
"1/03/2008"
"1/04/2008"
"1/05/2008"
end
generate mydate1=date(date,"DMY")
format mydate1 %td
generate mydate2=date(date,"DMY“)
format mydate2 %tdmon-DD,_CCYY
TIP 1:Specifying the time structure
Date formats
Example – Daily format
. list date mydate1 mydate2
+--------------------------------------+
| date mydate1 mydate2 |
|--------------------------------------|
1. | 1/01/2008 01jan2008 jan-01, 2008 |
2. | 1/02/2008 01feb2008 feb-01, 2008 |
3. | 1/03/2008 01mar2008 mar-01, 2008 |
4. | 1/04/2008 01apr2008 apr-01, 2008 |
5. | 1/05/2008 01may2008 may-01, 2008 |
+--------------------------------------+
TIP 1:Specifying the time structure
Date formats
Example – Daily format
. tsset mydate1
time variable: mydate1, 01jan2008 to 01may2008,
but with gaps
delta: 1 day
+-----------+
| mydate1 |
|-----------|
2. | 01feb2008 |
3. | 01mar2008 |
4. | 01apr2008 |
+-----------+
TIP 1:Specifying the time structure
Date formats
Example – Clock format
clear
Input str20 etime y
"06feb2010 12:40:00" 2
"06feb2010 12:42:00" 5
"06feb2010 12:44:00" 7
"06feb2010 12:46:00" 6
"06feb2010 12:48:00" 9
end
. generate my_ly=l.y
(1 missing value generated)
. list mytime y ly my_ly
+---------------------------------+
| mytime y my_ly |
|---------------------------------|
1. | 06feb2010 12:40:00 2 . |
2. | 06feb2010 12:42:00 5 2 |
3. | 06feb2010 12:44:00 7 5 |
4. | 06feb2010 12:46:00 6 7 |
5. | 06feb2010 12:48:00 9 6 |
+---------------------------------+
TIP 2: Predictions with -arima-
Kalman Filter recursions
Let’s consider the following moving average
(MA1) model:
yt t 1 t ; t ~ i.i.d .N (0, )
2
Command line
to fit the model: arima y, ma(1)
WHY?
TIP 2: Predictions with -arima-
Kalman Filter recursions
- Code for manual predictions
use http://www.stata-press.com/data/r11/lutkepohl,clear
arima dlinvestment, ma(1)
predict double yhat
scalar b0 = _b[_cons]
scalar t1 = [ARMA]_b[L1.ma]
gen double my_yhat = b0
gen double myehat = dlinvestment - b0 in 2
forvalues i = 3/91 {
qui replace my_yhat = my_yhat ///
+ t1*L.myehat in `i'
qui replace myehat = dlinvestment - my_yhat in `i'
}
TIP 2: Predictions with -arima-
Kalman Filter recursions
List first 12 predictions
. list qtr yhat my_yhat in 1/13,sep(11)
+--------------------------------+
| qtr yhat my_yhat |
|--------------------------------|
1. | 1960q1 .01686688 .01686688 |
2. | 1960q2 .01686688 .01686688 |
3. | 1960q3 .02052151 .02062398 |
4. | 1960q4 .01478403 .0147996 |
5. | 1961q1 .01312365 .01312617 |
6. | 1961q2 .00326376 .00326418 |
7. | 1961q3 .02471242 .02471249 |
8. | 1961q4 .01691061 .01691062 |
9. | 1962q1 .01412974 .01412975 |
10. | 1962q2 .00643301 .00643301 |
11. | 1962q3 .01940009 .0194001 |
|--------------------------------|
12. | 1962q4 .01649863 .01649863 |
13. | 1963q1 .01749646 .01749646 |
+--------------------------------+
TIP 2: Predictions with -arima-
Kalman Filter recursions
Stata uses the recursive formula for the Kalman filter
prediction based on:
Where:
+--------------------------------------------------------+
| qtr yhat my_yhat pt myratio |
|--------------------------------------------------------|
1. | 1960q1 .01686688 .01686688 .00192542 . |
2. | 1960q2 .01686688 .01686688 .00192542 .97272668 |
3. | 1960q3 .02052151 .02052151 .00005251 .99923589 |
4. | 1960q4 .01478403 .01478403 1.471e-06 .99997858 |
5. | 1961q1 .01312365 .01312365 4.125e-08 .9999994 |
|--------------------------------------------------------|
6. | 1961q2 .00326376 .00326376 1.157e-09 .99999998 |
7. | 1961q3 .02471242 .02471242 3.243e-11 1 |
8. | 1961q4 .01691061 .01691061 9.092e-13 1 |
9. | 1962q1 .01412974 .01412974 2.549e-14 1 |
10. | 1962q2 .00643301 .00643301 7.147e-16 1 |
+--------------------------------------------------------+
TIP 3: Initial shock for Impulse response
functions (IRF) after -var-
VAR model
Yt 1Yt 1 2 Yt 2 ... p Yt p t
Where:
.04
.02
-.02
0 5 10 0 5 10
step
95% CI orthogonalized irf
Graphs by irfname, impulse variable, and response variable
TIP 3: Initial shock for Impulse response
functions after -var-
What is the magnitude of the shock in the IRF graph?
order1, Y1, Y1 order1, Y1, Y2
-.5
0 5 10 0 5 10
step
95% CI impulse response function (irf)
Graphs by irfname, impulse variable, and response variable
TIP 3: Initial shock for Impulse response
functions after -var-
What is the magnitude of the shock in the IRF graph?
order1, Y1, Y1 order1, Y1, Y2
-irf graph,oirf-: orthogonal IRF .06
Cholesky decomposition
step
from the constraints imposed on the SVAR
95% CI orthogonalized irf
the magnitude of the shock corresponds to Graphs by irfname, impulse variable, and response variable
one unit standard deviation
TIP 3: Initial shock for Impulse response
functions after -var-
Let’s fit a VAR model:
use http://www.stata-press.com/data/r11/lutkepohl
var dlinvestment dlincome, lags(1/2) dfk
TIP 3: Initial shock for Impulse response
functions after -var-
. var dlinvestment dlincome,lags(1/2) dfk
Vector autoregression
Equation Parms RMSE R-sq chi2 P>chi2
----------------------------------------------------------------
dlinvestment 5 .04424 0.0856 8.32989 0.0802
dlincome 5 .011403 0.1027 10.1916 0.0373
------------------------------------------------------------------------------
| Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
dlinvestment |
dlinvestment |
L1. | -.2274192 .1053092 -2.16 0.031 -.4338214 -.021017
L2. | -.1159636 .1057698 -1.10 0.273 -.3232686 .0913415
dlincome |
L1. | .7103053 .3948248 1.80 0.072 -.0635372 1.484148
L2. | .5149489 .3935121 1.31 0.191 -.2563206 1.286218
_cons | -.0012273 .0111362 -0.11 0.912 -.0230539 .0205993
-------------+----------------------------------------------------------------
dlincome |
dlinvestment |
L1. | .0597466 .0271441 2.20 0.028 .0065451 .1129481
L2. | .0563513 .0272629 2.07 0.039 .002917 .1097855
dlincome |
L1. | .0209461 .1017687 0.21 0.837 -.1785169 .220409
L2. | .0833252 .1014303 0.82 0.411 -.1154745 .2821249
_cons | .0150368 .0028704 5.24 0.000 .0094108 .0206627
------------------------------------------------------------------------------
TIP 3: Initial shock for Impulse response
functions after -var-
Plot the IRF function for a shock in dlinvestment
use http://www.stata-press.com/data/r11/lutkepohl var
dlinvestment dlincome, lags(1/2) dfk
irf create order1, step(10) set(myirf1,replace)
irf graph oirf, impulse(dlinvestment) ///
response(dlinvestment dlincome)
TIP 3: Initial shock for Impulse response
functions after -var-
Plot the IRF function for a shock in dlinvestment
irf graph oirf, impulse(dlinvestment) ///
response(dlinvestment dlincome)
order1, dlinvestment, dlincome order1, dlinvestment, dlinvestment
.06
.04
.02
-.02
0 5 10 0 5 10
step
95% CI orthogonalized irf
Graphs by irfname, impulse variable, and response variable
TIP 3: Initial shock for Impulse response
functions after -var-
Table for the OIRF function for a shock in dlinvestment
. irf table oirf, irf(order1) impulse(dlinvestment) ///
> response(dlincome dlinvestment)
constraint 1 [z]L.z = 0
constraint 2 [y]z = 1
sspace (z L.z, state noconstant) ///
(y w z,noerror ), constraints(1/2)
TIP 4:
State Space estimation for linear regression
use http://www.stata-press.com/data/r11/lutkepohl,clear
constraint 1 [z]L.z = 0
constraint 2 [dlinvestment]z = 1
sspace (z L.z, state noconstant) (dlinvestment dlincome z,noerror ), constraints(1/2) nolog
State-space model
Sample: 1960q2 - 1982q4 Number of obs = 91
Wald chi2(1) = 0.88
Log likelihood = 154.44197 Prob > chi2 = 0.3487
( 1) [z]L.z = 0
( 2) [dlinvestment]z = 1
------------------------------------------------------------------------------
| OIM
dlinvestment | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
z |
z |
L1. | (omitted)
-------------+----------------------------------------------------------------
dlinvestment |
z | 1 . . . . .
dlincome | .3668678 .3914794 0.94 0.349 -.4004178 1.134153
_cons | .0096556 .008925 1.08 0.279 -.007837 .0271483
-------------+----------------------------------------------------------------
var(z) | .0019651 .0002913 6.75 0.000 .0013941 .0025361
------------------------------------------------------------------------------
Note: Tests of variances against zero are conservative and are provided only for reference.
TIP 4: How do I fit my unobserved
component model with –sspace-
Random Walk yt yt 1
zt zt 1 t
State Space representation
yt z t
- Command specification
constraint 1 [z]L.z = 1
constraint 2 [y]z = 1
sspace (z L.z, state noconstant) ///
(y z,noerror noconstant), constraints(1/2)
TIP 4:
State Space estimation for Random Walk
use http://www.stata-press.com/data/r11/lutkepohl,clear
constraint 1 [z]L.z = 1
constraint 2 [dlinvestment]z = 1
sspace (z L.z, state noconstant) (dlinvestment z,noerror noconstant), constraints(1/2) nolog
State-space model
Sample: 1960q2 - 1982q4 Number of obs = 91
Log likelihood = 112.76541
( 1) [z]L.z = 1
( 2) [dlinvestment]z = 1
------------------------------------------------------------------------------
| OIM
dlinvestment | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
z |
z |
L1. | 1 . . . . .
-------------+----------------------------------------------------------------
dlinvestment |
z | 1 . . . . .
-------------+----------------------------------------------------------------
var(z) | .0046812 .0006978 6.71 0.000 .0033135 .006049
------------------------------------------------------------------------------
Note: Model is not stationary.
Note: Tests of variances against zero are conservative and are provided only for reference.
TIP 5:VEC – Johansen identification
Reduced form for a VEC model
p 1
zt a bt zt 1 i zt i t
i 1
Where:
zt : I(1) Endogenous variables
z1t z1t 1 p 1
z1t i
z a bt z z
2 t 2 t 1
i 1
i 2 t i t
z3t
z3t 1
z 3 t i
Where:
11 12 z
11 12 13 1t 1
zt 1 21 22 z
22 23 2t 1
31 32 21 z3t 1
31 32 z3t 1
TIP 5:
- Restrictions based on Johansen normalization (Default)
use http://www.stata-press.com/data/r11/lutkepohl,clear
vec linvestment lincome lconsumption, rank(2) lags(2) noetable trend(none)
Which implies
( 1) [_ce1]linvestment = 1
( 2) [_ce1]lincome = -.75
( 3) [_ce2]lconsumption = 1
( 4) [_ce2]lincome = -.85
------------------------------------------------------------------------------
beta | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
_ce1 |
linvestment | 1 . . . . .
lincome | -.75 . . . . .
lconsumption | -.0343804 .0122816 -2.80 0.005 -.0584519 -.010309
-------------+----------------------------------------------------------------
_ce2 |
linvestment | -.1745742 .00322 -54.22 0.000 -.1808852 -.1682632
lincome | -.85 . . . . .
lconsumption | 1 . . . . .
------------------------------------------------------------------------------
Summary
Tip 1: Specifying the time structure
Tip 2: Predictions with –arima-. Kalman Filter recursions
Tip 3: Initial shock for Impulse response functions after -var-
Tip 4: Unobserved component models with –sspace-