nyse = ts(scan("http://www.stat.ualberta.ca/~wiens/stat479/S&Sdatasets/nyse.dat")) nyse = ts(scan("nyse.dat")) plot(nyse) # Compare with Figure 1.4 ## Set the CRAN mirror to Canada (BC) (Menu: Packages -> Set CRAN mirror) ## Install package tseries (Packages -> Install package) ## Load it: library(tseries) nyse.g = garch(nyse, order=c(1,1)) summary(nyse.g) win.graph() plot(nyse.g) names(nyse.g) resids = nyse.g$resid shapiro.test(resids) # Verification that "residuals" refers to w_t^hat, not eta_t^hat: win.graph() sigma = nyse.g$fitted.values[,1] eta = sigma*resids plot(nyse, eta, type = "l") win.graph() par(mfrow=c(2,1)) u = predict(nyse.g) ## See help(predict.garch): u contains predictions +/- one ## conditional standard deviation v = nyse.g$fitted.values # gives +/- one s.d., so w = u+v gives predictions +/- two s.d. plot(800:1000, nyse[800:1000], ylim = c(-.16,.06), type = "l", xlab = "Time", ylab = "Returns +/- one s.d.") lines(u[,1], col = "blue", lty="dashed") lines(u[,2], col = "blue", lty="dashed") w = u+v plot(800:1000, nyse[800:1000], ylim = c(-.16,.06), type = "l", xlab = "Time", ylab = "Returns +/- 2 s.d.") lines(w[,1], col = "blue", lty="dashed") lines(w[,2], col = "blue", lty="dashed")