log using tsdemo.log, replace
* Data from Enders, Applied Econometric Time Series, 1995.

set scheme s2mono

webuse wpi1, clear
label var wpi "US Wholesale Price Index (WPI)"
gen double dlnwpi = D.ln_wpi
label var dlnwpi "US WPI -- log difference"

* graph the series and its log difference
tsline wpi   , tlabels(#7) ylabels(, angle(horizontal)) name(fig1a)
tsline dlnwpi, tlabels(#7) ylabels(, angle(horizontal)) name(fig1b)
graph combine fig1a fig1b, col(1) ysize(6.5) xsize(8.5) title(Figure 1) 
graph export fig1.pdf, replace 

* plot the autocorrelogram and partial autocorrelogram
ac  dlnwpi, ylabels(-.4(.2).6, angle(horizontal)) name(fig2a)
pac dlnwpi, ylabels(-.4(.2).6, angle(horizontal)) name(fig2b)
graph combine fig2a fig2b, col(1) iscale(0.6) ysize(6.5) xsize(8.5)    ///
      title(Figure 2) 
graph export fig2.pdf, replace

* Fit an ARIMA(1,1,1) model to the series, with an additional 4th order MA term
arima dlnwpi if tin(,1985q4), ar(1) ma(1 4)

* Generate dynamic forecasts for 1986-1990
local exante tin(1986q1,1990q4)                            // used many times
predict double dlnwpihat if `exante', xb dynamic(.)
gen fcerr = dlnwpi - dlnwpihat
label var fcerr "Forecast error from inflation predictions"

* plot the actual, predicted, and forecast error series
tsline dlnwpi dlnwpihat if `exante',                                        ///
   ylabels(, angle(horizontal) format(%4.2f)) yline(0) ytitle("")           ///
   title("Out-of-sample dynamic forecasts for US WPI inflation, 1986-1990") ///
   name(fig3a)
tsline fcerr if `exante',                                                   ///
   ylabels(, angle(horizontal) format(%4.2f)) yline(0) ytitle("")           ///
   title(Forecast errors) recast(area) name(fig3b)
graph combine fig3a fig3b, col(1) iscale(0.6) ysize(6.5) xsize(8.5)    ///
      title(Figure 3)
graph export fig3.pdf, replace
log close