### This file contains some examples from Ch. 1 R library(ts) # to read jj.dat from a file. #x <- scan("E:\\Stat207\\Class\\Examples\\Ch1\\jj.dat") # read data from the class website. x <- scan("http://www.sci.csueastbay.edu/~esuess/Statistics_6871/Handouts/Examples/Ch1/jj.dat") # you can make x a regular time series object x <- ts(x) # plot the data win.graph() ts.plot(x,gpars=list(xlab="Quarter", ylab="J&J Earning Per Share")) # log and plot win.graph() x.log <- log(x) ts.plot(x.log,gpars=list(xlab="Quarter", ylab="Log of J&J Earning Per Share")) # difference the log data and plot win.graph() dx <- diff(x.log) ts.plot(dx, gpars=list(xlab="Quarter", ylab="J&J Earning Per Share Growth Rate")) # all 3 plots - one on top of the other win.graph() par(mfrow=c(3,1)) ts.plot(x,gpars=list(xlab="Quarter", ylab="J&J Earning Per Share")) ts.plot(x.log,gpars=list(xlab="Quarter", ylab="Log of J&J Earning Per Share")) ts.plot(dx, gpars=list(xlab="Quarter", ylab="J&J Earning Per Share Growth Rate")) # Detrend & Regression z <- 1:length(x.log) fit <- lm(x.log~z) win.graph() par(mfrow=c(1,1)) min(x.log) max(x.log) ts.plot(x.log, gpars=list(xlab="Quarter", ylab="Log of J&J Earning Per Share",ylim=c(-1,3)) ) par(new=T) x.hat <- fit$fitted ts.plot(x.hat, gpars=list(xlab="", ylab="", ylim=c(-1,3))) # both lines on plot #detrended series win.graph() dt <- x.log-x.hat ts.plot(dt) # compare difference with detrended data win.graph() par(mfrow=c(2,1)) acf(dx) # remember dx <- diff(x.log) acf(dt) # matrix of plots of dx(t) vs dx(t-h) for h=1,2,...,9 win.graph() lag.plot(dx, lags=9, layout=c(3,3)) # dummy variable regression u <- matrix(contr.sum(4, contrasts=F),4,84) # matrix of dummy variables u <- t(u) # transpose fit <- lm(x.log~z + u[,1:3]) # fit on 3 cols of dummy summary(fit) # get details of the regression noise <- fit$residuals # get residuals win.graph() acf(noise) win.graph() ts.plot(noise)