*
* HISTORY.RPF
* Example of historical decomposition for a VAR
*
* RATS User's Guide, Example from Section 7.7.
*
open data oecdsample.rat
calendar(q) 1981
data(format=rats) 1981:1 2006:4 can3mthpcp canexpgdpchs $
   canexpgdpds canm1s canusxsr usaexpgdpch
*
set logcangdp  = log(canexpgdpchs)
set logcandefl = log(canexpgdpds)
set logcanm1   = log(canm1s)
set logusagdp  = log(usaexpgdpch)
set logexrate  = log(canusxsr)
*
system(model=canmodel)
variables logcangdp logcandefl logcanm1 $
          logexrate can3mthpcp logusagdp
lags 1 to 4
det constant
end(system)
*
estimate(noprint,noftests,resids=resids)
*
* This sets the range for the historical decomposition, so the base
* forecasts are computed using data through 2002:4 (the period before
* 2003:1).
*
compute hstart=2003:1
compute hend  =2006:4
***********************************************************************
* These pull information out of the model. The VARLABELS are the labels
* of the dependent variables,  while the SHOCKLABELS are the labels of
* the shocks. Here (with Choleski shocks), they are the same. In
* practice, they might not be.  And in practice, you might want to use
* more descriptive labels, which you can do using COMPUTE, for instance,
* something like compute varlabels=||"Canadian Real GDP","GDP
* Deflator",...||
*
compute neqn=%modelsize(canmodel)
dec vect[int] depvar(neqn)
dec vect[string] varlabels(neqn) shocklabels(neqn)
ewise varlabels(i)=%modellabel(canmodel,i)
ewise shocklabels(i)=%modellabel(canmodel,i)
compute depvar=%modeldepvars(canmodel)
*
* If you want non-Choleski shocks, you can use a FACTOR option on the
* HISTORY. You might want to input more descriptive values for the
* SHOCKLABELS if you do that.
*
history(model=canmodel,add,results=history,from=hstart,to=hend)
*
* This does a separate page of graphs for each dependent variable, with
* each page having a 3 x 2 layout with the graphs of the effects of the
* six shocks on that variable. With a different model size, you would
* need to change the VFIELDS and HFIELDS to match.
*
do j=1,neqn
   spgraph(vfields=3,hfields=2,window=varlabels(j),$
     header="Historical Decomposition of "+varlabels(j))
   do i=1,neqn
      *
      * This graphs the actual data (including four pre-sample values),
      * the base forecast, and the effects of shock I on variable J.
      *
      graph(header="Effect of "+shocklabels(i)) 3
      # depvar(j) hstart-4 hend
      # history(1,j)
      # history(1+i,j)
   end do j
   spgraph(done)
end do i
*
* This does a stacked bar graph for the effects of the shocks, overlaid
* with the actual data (just for the first dependent variable, Canadian
* GDP). (For various reasons, this probably isn't a great idea but is
* being shown for illustration).
*
* This uses the NOADD option so the effects of the shocks are separated
* from the underlying forecast.
*
history(model=canmodel,results=history,noadd,from=hstart,to=hend)
*
* Arrange the series to be graphed to have the effects of the shocks
* first and the dependent variable last.
*
dec vect[int] byshock(%nvar+1)
ewise byshock(i)=%if(i==%nvar+1,depvar(1),history(i+1,1))
*
* Put together the corresponding key labels
*
dec vect[strings] histlabels(%nvar+1)
ewise histlabels(i)=%if(i==%nvar+1,%l(depvar(1)),shocklabels(i))
*
* This shifts the colors so the overlay line for the data is black and
* the bars are colored.
*
dec vect[int] symbols(%nvar+1)
ewise symbols(i)=%if(i==%nvar+1,1,i+1)
*
smpl hstart hend
graph(series=byshock,style=stackedvar,overlay=line,$
  symbols=symbols,key=below,klabels=histlabels,$
  vlabel="Shocks",ovlabel="Data")
smpl
