/* Introduction to Time Series Analysis, DG ECFIN Laura Mayoral November 2021 */ /******************VAR models************** *Goal: Fit a VAR to: inflation, unemployment rate, fed funds Do several analyses with it: Forecasting, Granger causality,etc See Chapter 9, Introduction to time series in STATA by Sean Becketti ***********************************************************************/ set more off clear capture log close log using var1, replace *Replace by your own path cd "/Users/lauramayoral/Dropbox/docum_dropbox/clases/ECFIN_time series/STATA/files" /* reduced-form VAR */ *the red line reflects that the data has been updated with respect to the original paper by Stock and Watson use varexample, clear describe tsset local x = tq(2002q1) + 0.5 tsline inflation, xline(`x') ylabel(2 4 6 8 10 12) name(inflation, replace) tsline unrate, xline(`x') ylabel(4 6 8 10) name(unrate, replace) tsline fedfunds, xline(`x') ylabel(0 5 10 15) name(fedfunds, replace) graph combine inflation unrate fedfunds graph export VAR_data.eps, replace summarize summarize fedfunds inflation unrate if date<=tq(2002q1) summarize fedfunds inflation unrate if date>tq(2002q1) ******************************************************** *model selection ******************************************************** *LRtest: test that the coefficients of the last lag are jointly equal to zero--General to specific varsoc inflation unrate fedfunds if date<=tq(2002q1), maxlag(12) ******************************************************** *estimation ******************************************************** *step: sets the forecast horizon *we estimate a VAR with 4 lags, to match stock and watsons model. Alternatively, you can also estimate a VAR(2), as suggested by the HQ IC. varbasic inflation unrate fedfunds if date<=tq(2002q1), lags(1 2 3 4) step(12) nograph *look at the eigenvalues--all should be smaller than 1 varstable, graph graph export VAR_eigen.eps, replace ******************************************************** *other tests ******************************************************** *1. test of residual autocorrelation: *residuals do not look very good--re-specify the model? varlmar *2. Normality of residuals varnorm *3. significance of lags included in the model varwle ******************************************************************************** *Forecasting ******************************************************************************** *Two types of forecasts: one step ahead and multiple steps ahead *ONE-STEP AHEAD FORECASTS *Estimate again the model quietly var inflation unrate fedfunds if date<=tq(2002q1), lags(1 2 3 4) /* 1-step-ahead conditional forecasts Inflation */ predict f_inflation *predict test1, equation(#1) *predict test2, equation(inflation) *summarize label variable f_inflation "forecast" *compare f_inflation test1 *compare f_inflation test2 *drop test1 test2 compare inflation f_inflation if date>tq(2002q1) *OUT of sample prediction: tsline inflation f_inflation if date>tq(2002q1), lpattern(solid dash) graph export /home/sls/itsus/eps/VAR_inflfore.eps, replace *and take a look at the residuals predict r_inflation, equation(inflation) residuals tsline r_inflation if date>tq(2002q1), yline(0) graph export /home/sls/itsus/eps/VAR_inflres.eps, replace sum r_inflation if date<=tq(2002q1) sum r_inflation if date>tq(2002q1) ***Compute confidence bands for the prediction. *stdp: computes standard error of the prediction predict s_inflation, equation(inflation) stdp generate l_inflation = f_inflation - 1.96*s_inflation generate u_inflation = f_inflation + 1.96*s_inflation twoway (rarea u_inflation l_inflation date, fintensity(inten10)) (line inflation f_inflation date, lpattern(solid dash)) in -40/l, yline(0) legend(order(2 3 1) label(1 "95% CI")) name(inflation, replace) *twoway (rarea u_inflation l_inflation date, fintensity(inten10)) (line inflation f_inflation date, lpattern(solid dash)) in -40/l, yline(0) legend(order(2 3 1) label(1 "95% CI")) name(inflation, replace) fysize(150) fxsize(75) /* Unemployment rate */ predict f_unrate, equation(unrate) label variable f_unrate "forecast" tsline unrate f_unrate if date>tq(2002q1), lpattern(solid dash) predict r_unrate, equation(unrate) residuals sum unrate if date>tq(2002q1) & datetq(2008q4) compare unrate f_unrate if date>tq(2002q1) tsline r_unrate, yline(0) sum r_unrate if date<=tq(2002q1) sum r_unrate if date>tq(2002q1) predict s_unrate, equation(unrate) stdp generate l_unrate = f_unrate - 1.96*s_unrate generate u_unrate = f_unrate + 1.96*s_unrate label variable unrate "Unemp. rate" twoway (rarea u_unrate l_unrate date, fintensity(inten10)) (line unrate f_unrate date, lpattern(solid dash)) in -40/l, legend(order(2 3 1) label(1 "95% CI")) name(unrate, replace) *twoway (rarea u_unrate l_unrate date, fintensity(inten10)) (line unrate f_unrate date, lpattern(solid dash)) in -40/l, legend(order(2 3 1) label(1 "95% CI")) name(unrate, replace) fysize(150) fxsize(75) /* Fed funds */ predict f_fedfunds, equation(fedfunds) label variable f_fedfunds "forecast" tsline fedfunds f_fedfunds if date>tq(2002q1), lpattern(solid dash) predict r_fedfunds, equation(fedfunds) residuals compare fedfunds f_fedfunds if date>tq(2002q1) tsline r_fedfunds, yline(0) sum r_fedfunds if date<=tq(2002q1) sum r_fedfunds if date>tq(2002q1) predict s_fedfunds, equation(fedfunds) stdp generate l_fedfunds = f_fedfunds - 1.96*s_fedfunds generate u_fedfunds = f_fedfunds + 1.96*s_fedfunds twoway (rarea u_fedfunds l_fedfunds date, fintensity(inten10)) (line fedfunds f_fedfunds date, lpattern(solid dash)) in -40/l, yline(0) legend(order(2 3 1) label(1 "95% CI")) name(fedfunds, replace) *twoway (rarea u_fedfunds l_fedfunds date, fintensity(inten10)) (line fedfunds f_fedfunds date, lpattern(solid dash)) in -40/l, yline(0) legend(order(2 3 1) label(1 "95% CI")) name(fedfunds, replace) fysize(150) fxsize(75) graph combine inflation unrate fedfunds *graph combine inflation unrate fedfunds, cols(1) ysize(7) graph export /home/sls/itsus/eps/VAR_condfore.eps, replace ************************************************************ /* Dynamic forecasts: multiple steps-ahead */ fcast compute F_, step(40) fcast graph F_inflation F_unrate F_fedfunds, observed lpattern(dash) graph export /home/sls/itsus/eps/VAR_dynfore.eps, replace describe F_infl* list date inflation F_inflation if date>tq(1999q4) & datetq(2000q4) & date