##################################################################### ### R code for lake.tsm example from Chapter 1 of Brockwell & Davis (2002) # Read in data. These are resids from fitting line to lake.tsm x <- scan("lak.tsm") par(mfrow=c(2,2)) ts.plot(x,main="Series X(t)") x1 <- x[1:97] x2 <- x[2:98] plot(x1,x2,ylab="X(t)",xlab="X(t-1)") # # Regress x1 against x2 myfit <- lm(x2~x1-1) abline(myfit) summary(myfit) z <- myfit$residuals ts.plot(z,main="Series Z(t)") acf(z,main="Series Z(t)",ylim=c(-1,1)) ##################################################################### ### Trivariate time series plot to illustrate cointegration ### # replace "postscript" with "pdf" to save plot, "coint.pdf", as pdf postscript(file="coint.eps",horizontal=FALSE,width=6,height=5,pointsize=9) # Simulate data: Z and W are independent Gaussian white noise sequences Z <- rnorm(100); W <- rnorm(length(Z)); X <- Z; Y <- Z; # X is a random walk, Y is random walk plus noise for (t in 1:length(Z)){ X[t] <- sum(Z[1:t]) Y[t] <- X[t]+W[t] } # form a ts structure (myts) for plotting with ts.plot myts <- ts(matrix(c(X,Y,Y-X), length(X), 3), start=c(1,1,1)) ts.plot(myts, gpars=list(main="Cointegration Example", xlab="time", ylab="", lty=c(2,3,1))) # draw a line along x-axis for reference lines(c(1,length(X)),c(0,0)) # add a legend, with math expressions (subscripts) legend(0,10,legend=c(expression(X[t]),expression(Y[t]),expression(Y[t]-X[t])),lty=c(2,3,1)) # this is needed if saving plot to a file, generates the actual plot file dev.off()