- Code: Select all
*
* NNDynFore.prg
* Example of dynamic forecasting with neural networks
*
cal(q) 1959:3
open data nnetdat.rat
data(format=rats,ver) * 1996:3
set logy = log(gdpq)
set trend = t
*
compute fstart=1990:1
*
* For comparison, do an OLS fit through 1989:4 and dynamic forecast from
* there through the end of sample.
*
linreg logy * fstart-1
# constant logy{1 2}
*
uforecast olsdynamic fstart 1996:3
*
* Now fit a neural net model, using data through 1989:4. We use 5 hidden
* nodes and train to an R^2 of .999 (it doesn't quite make that in
* 500000 iterations). We need to allow the range to expand quite a bit
* to accomodate the trending data, hence the pad=.9 option. Even with
* that, the multiple step forecasts tend to trail off below trend as a
* result of the "squashing" function. With a smaller value of pad, this
* will be even more pronounced.
*
nnlearn(pad=.90,save=mv, $
hidden=5,rsquared=.999,iters=500000,trace) * fstart-1
# logy{1 2}
# logy
*
* Compute static forecasts during the hold-back period.
*
nntest fstart 1996:3 mv
# logy{1 2}
# nnstatic
*
* Compute dynamic forecasts. In order to do dynamic forecasts, we have
* to loop over a set of one-step forecasts, feeding the forecasts back
* into the calculation. We'll create a copy of logy and use that as the
* input for NNTEST. This allows us to update the input series after each
* step without changing the values of our original logy series.
*
clear nndynamic
set logydyn = logy
do time = fstart,1996:3
nntest time time mv
# logydyn{1 2}
# logydyn
*
* Store the dynamic forecast
*
compute nndynamic(time) = logydyn(time)
end do time
*
graph(key=upleft) 4
# logy
# nnstatic
# nndynamic
# olsdynamic
Data file:
