/* Introduction to Time Series Analysis, DG ECFIN Laura Mayoral November 2021 */ /******************Multivariate Non Stationary models ************** This file shows how to test for cointegration using STATA 1) Engle and Granger two step method 2) Johansen's method See Becketti, Chapter 10 ***********************************************************************/ *Example 1: Engle and Granger two step approach /*This example uses annual data on the average per-capita disposable personal income in the eight U.S. Bureau of Economic Analysis (BEA) regions of the United States. We use data from 1948–2002 in logarithms. Unit-root tests on these series fail to reject the null hypothesis that per-capita disposable income in each region contains a unit root. Because capital and labor can move easily between the different regions of the United States, we would expect that no one series will diverge from all the remaining series and that cointegrating relationships exist.*/ *Load data (in built in stata) *note: the data has very few obs., results won't be very reliable. webuse rdinc, clear tsset tsline ln_ne ln_se *install the E-G command, in case you don't have it ssc install egranger *1. start by testing for unit roots: we can't reject dfgls ln_ne dfgls ln_se *2. Engle and granger two steps in one command *you can include a trend in the first step and enough lags to run the ADF test in the residuals egranger ln_ne ln_se, lags(4) regress *we can't reject residuals are I(1): no cointegration ******************************************************************************** /*Example 2: We have data on monthly unemployment rates in Indiana, Illinois, Kentucky, and Missouri from January 1978 through December 2003. We suspect that factor mobility will keep the unemployment rates in equilibrium. The following graph plots the data.*/ use https://www.stata-press.com/data/r17/urates, clear tsset tsline missouri indiana kentucky illinois egranger missouri indiana kentucky illinois, lags(7) regress *you can also estimate the ECM which is a VAR for the differences and includes the residuals from the previous equation (the cointegration relationship) egranger missouri indiana kentucky illinois, ecm lags(3) **************************************************************************** *johansen's full-estimation method: SEE CHAPTER 10 BICKETTI FOR A MORE DETAILED *EXPLANTION OF THIS EXAMPLE **************************************************************************** set more off clear capture log close log using cointegration_example, replace *Load data for this example *this dataset contains construction wages in different states in the US *Goal: to check whether wages equalize across policital boundaries use "/Users/lauramayoral/Dropbox/docum_dropbox/clases/ECFIN_time series/STATA/files/conwages.dta", clear tsset describe tsline conwgdc conwgmd conwgva, lpattern(solid dash longdash_dot) graph export COINT_wagelevels.eps, replace correlate conwgdc conwgmd conwgva *Constructing indices: generate dcwage = 1000 * conwgdc/lfdcs generate mdwage = 1000 * conwgmd/lfmds generate vawage = 1000 * conwgva/lfvas label variable dc "DC" label variable md "MD" label variable va "VA" format dcwage mdwage vawage %6.0fc tsline dcwage mdwage vawage, lpattern(solid dash longdash_dot) graph export COINT_wages.eps, replace /* Is there a unit root? */ ac vawage graph export COINT_acva.eps, replace dfgls vawage dfuller vawage, trend lags(4) /* How many lags? */ varsoc dcwage mdwage vawage varsoc dcwage mdwage vawage, maxlag(8) *two types of tests: *eigenvalue and trace vecrank dcwage mdwage vawage, max ic *trace vecrank dcwage mdwage vawage,ic *2 cointegrating relationships /* Boil the ocean: All the combinations of vecrank tests */ vecrank dcwage mdwage vawage, max ic lags(1) trend(trend) /* Case 1: Unrestricted trend */ vecrank dcwage mdwage vawage, max ic lags(2) trend(trend) vecrank dcwage mdwage vawage, max ic lags(3) trend(trend) vecrank dcwage mdwage vawage, max ic lags(1) trend(rtrend) /* Case 2: Restricted trend */ vecrank dcwage mdwage vawage, max ic lags(2) trend(rtrend) vecrank dcwage mdwage vawage, max ic lags(3) trend(rtrend) vecrank dcwage mdwage vawage, max ic lags(1) trend(constant) /* Case 3: Unrestricted constant */ vecrank dcwage mdwage vawage, max ic lags(2) trend(constant) vecrank dcwage mdwage vawage, max ic lags(3) trend(constant) vecrank dcwage mdwage vawage, max ic lags(1) trend(rconstant) /* Case 4: Restricted constant */ vecrank dcwage mdwage vawage, max ic lags(2) trend(rconstant) vecrank dcwage mdwage vawage, max ic lags(3) trend(rconstant) vecrank dcwage mdwage vawage, max ic lags(1) trend(none) /* Case 5: No trend */ vecrank dcwage mdwage vawage, max ic lags(2) trend(none) vecrank dcwage mdwage vawage, max ic lags(3) trend(none) /* Estimate the VECMs */ vec dcwage mdwage vawage, rank(2) lags(3) trend(trend) estimates store trendlags3 test [D_dcwage]_trend [D_mdwage]_trend [D_vawage]_trend quietly vec dcwage mdwage vawage, rank(2) lags(3) trend(rtrend) estimates store rtrendlags3 lrtest trendlags3 rtrendlags3 quietly vec dcwage mdwage vawage, rank(2) lags(3) trend(constant) estimates store constantlags3 lrtest rtrendlags3 constantlags3 quietly vec dcwage mdwage vawage, rank(2) lags(3) trend(rconstant) estimates store rconstantlags3 lrtest constantlags3 rconstantlags3 quietly vec dcwage mdwage vawage, rank(2) lags(3) trend(none) estimates store nonelags3 lrtest rconstantlags3 nonelags3 quietly vec dcwage mdwage vawage in 4/l, rank(2) lags(2) trend(none) estimates store nonelags2 lrtest nonelags3 nonelags2 constraint 1 [D_dcwage]L1._ce2=0 constraint 2 [D_mdwage]L1._ce1=0 vec dcwage mdwage vawage, rank(2) lags(3) trend(none) aconstraints(1/2) constraint 3 [_ce1]dcwage=1 constraint 4 [_ce1]mdwage=0 constraint 5 [_ce2]dcwage=0 constraint 6 [_ce2]mdwage=1 vec dcwage mdwage vawage, rank(2) lags(3) trend(none) aconstraints(1/2) bconstraints(3/6) estimates store final lrtest nonelags3 final /* Tests of the residuals of the final specification */ vecstable veclmar vecnorm /* Examine the cointegrating relationships for reasonableness */ predict ce1, ce equation(_ce1) label variable ce1 "CE for DC/VA" predict ce2, ce equation(_ce2) label variable ce2 "CE for MD/VA" tsline ce1 ce2, yline(0) lpattern(solid longdash_dot) ytitle("Dollars per person") graph export COINT_ce.eps, replace generate diffdcva = dcwage - vawage label variable diffdcva "DC/VA wage difference" tsline ce1 diffdcva, yline(0) lpattern(solid longdash_dot) ytitle("Dollars per person") graph export COINT_cediff.eps, replace ac ce1, name(ac1, replace) ac ce2, name(ac2, replace) graph combine ac1 ac2 graph export COINT_ceac.eps, replace /* IRFs and forecasts */ estimates restore final irf create wages, set(wages) step(20) irf cgraph (wages dcwage dcwage oirf) (wages dcwage mdwage oirf) (wages dcwage vawage oirf) (wages mdwage dcwage oirf) (wages mdwage mdwage oirf) (wages mdwage vawage oirf) (wages vawage dcwage oirf) (wages vawage mdwage oirf) (wages vawage vawage oirf), legend(off) yscale(range(0)) graph export COINT_oirf.eps, replace fcast compute O_, step(8) fcast graph O_dcwage O_mdwage O_vawage, ylabel(1500 2000 2500 3000) graph export COINT_Ofcast.eps, replace generate newce1 = O_dcwage - 0.8615781*O_vawage label variable newce1 "Projected CE" label variable O_vawage "VA forecast" local x = tq(2011:1) two conn ce1 newce1 date if date>tq(2008:4), yline(0) xline(`x') lpattern(solid dash) graph export COINT_newce.eps, replace label variable O_dcwage "DC forecast" label variable O_mdwage "MD forecast" label variable O_vawage "VA forecast" two conn dcwage O_dcwage mdwage O_mdwage vawage O_vawage date if date>tq(2008:4), lpattern(solid dash solid dash_dot solid longdash_dot) xline(`x') msymbol(smcircle smcircle smdiamond smdiamond smplus smplus) graph export COINT_Ofcasto.eps, replace fcast compute w_, step(32) dynamic(tq(2006:1)) fcast compute W_, step(16) dynamic(tq(2010:1)) label variable w_dcwage "End of 2005 forecast" label variable W_dcwage "End of 2009 forecast" label variable dcwage "DC actuals" tsline dcwage w_dcwage W_dcwage if date>tq(2003:4), lpattern(solid dash longdash_dot) graph export COINT_Mfcast.eps, replace log close ******************************************************************