# K. Ensor (ensor@rice.edu) # Rice Univeristy # # Three functions are created. # kacf - returns the ACF and plots it correctly # kpacf - returns the PACF and plots it correctly # kdescrip - produces # - time series plot # - histogram # - correlogram # - partial correlogram # - lagged scatterplots up to lag 10 # INPUT is x (the series) and xtitle (the title of the series) # kacf<-function(x) { xacf<-acf(x,plot=F,type="correlation") nlag<-length(xacf$lag) print(nlag) nzero<-rep(0,nlag) plot(c(-1,0,xacf$lag,nlag+1),c(0,0,nzero,0),type="l",ylim=c(-1,1),xlim=c(0,nlag-1),xlab="Lag",ylab="ACF") segments(xacf$lag,nzero,xacf$lag,nzero+xacf$acf) title("ACF") return(xacf) } kpacf<-function(x) { xpacf<-acf(x,plot=F,type="partial") nlag<-length(xpacf$lag) nzero<-rep(0,nlag) plot(c(-1,0,xpacf$lag,nlag+1),c(0,0,nzero,0),type="l",ylim=c(-1,1),xlim=c(0,nlag-1),xlab="Lag",ylab="ACF") segments(xpacf$lag,nzero,xpacf$lag,nzero+xpacf$acf) title("PACF") return(xpacf) } #DESCRIPTIVE PLOTS kdescrip<-function(x,xtitle) { split.screen(c(2,1)) #split screen into two parts split.screen(c(1,3),screen=2) #split the bottom graph into three parts screen(1) ts.plot(x) title(main=xtitle) screen(3) hist(x,main="Histogram",xlab=xtitle) screen(4) xacf<-kacf(x) #kacf(x) screen(5) kpacf(x) close.screen(all=T) lag.plot(x,layout=c(2,5),lags=10) }