Appendix B: R and S-Plus Commands for Neural Network

The following commands are used in R or S-Plus to build the 3–2–1 skip-layer feed-forward network of Example 4.7. A line starting with # denotes a comment. The data file is m-ibmln.txt. The library used is nnet.

# load the data into R or S-Plus workspace.

x_scan(file=‘m-ibmln.txt’)

# select the output: r(t)

y_x[4:864]

# obtain the input variables: r(t-1), r(t-2), and r(t-3)

ibm.x_cbind(x[3:863]_,x[2:862],x[1:861])

# build a 3-2-1 network with skip layer connections

# and linear output.

ibm.nn_nnet(ibm.x,y,size=2,linout=T,skip=T,maxit=10000,

decay=1e-2,reltol=1e-7,abstol=1e-7,range=1.0)

# print the summary results of the network

summary(ibm.nn)

# compute \& print the residual sum of squares.

sse_sum((y-predict(ibm.nn,ibm.x))ˆ2)

print(sse)

#eigen(nnet.Hess(ibm.nn,ibm.x,y),T)\$values

# setup the input variables in the forecasting subsample

ibm.p_cbind(x[864:887],x[863:886],x[862:885])

# compute the forecasts

yh_predict(ibm.nn,ibm.p)

# The observed returns in the forecasting subsample

yo_x[865:888]

# compute \& print the sum of squares of forecast errors

ssfe_sum((yo-yh)ˆ2)

print(ssfe)

# quit S-Plus or R

q()

Exercises

4.1 Consider the daily simple returns of Johnson & Johnson stock from January 1998 to December 2008. The data are in the file d-jnj9808.txt or can be obtained from CRSP. Convert the returns into log returns in percentage. (a) Build a GJR model for the log return series. Write down the fitted model. Is the leverage ...

Get Analysis of Financial Time Series, Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.