RATS 11.1
RATS 11.1

NNDYNFORE.RPF is an example of using a neural net model to do dynamic multiple step forecasts. The NNTEST instruction only does "static" forecasts, so dynamic forecasts require looping over the horizons and feeding the forecasts back in as the inputs. This is most conveniently done by making a copy of the endogenous variable and using the copy for the inputs.

 

Full Program

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. (This is an unrestricted AR(2), which

* can pick up a unit root and extrapolate a linear trend).

*

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 the actual data starting five years (twenty quarters) before the

* start of the forecast period.

*

graph(key=upleft) 4

# logy fstart-20 *

# nnstatic

# nndynamic

# olsdynamic

 

Graph


Copyright © 2026 Thomas A. Doan