# Let's simulate an AR series and then produce our descriptive plots # We want to do this a few times with different model specifications, # so let's create a function. # karsim<-function(alpha,n) { x<-arima.sim(n,model=list(ar=alpha,)) kdescrip(x,"Simulated AR Series") return(x) } x<-karsim(c(0,.8),200) #EXAMPLE OF PARTIAL AUTOCORRELATIONS for n=200 par(mfrow=c(2,2)) # PROGRAM 3 # K. ENSOR, (ensor@rice.edu) # Rice University #first partial plot(x[1:199],x[2:200],main="First PACF") # Scatterplot of obs. 1 lag apart comppacf<-c(cor(x[1:199],x[2:200])) # Correlation of those observations #second partial - lag separation is 2 X1<-matrix(0,nrow=198,ncol=1) #Set up design matrix for input X1[ ,1]<-x[2:199] # Data is center block rf1<-lsfit(X1,x[3:200],intercept=F) # x is regressed on its past rf1<-rf1$residuals # extract residuals rb1<-lsfit(X1,x[1:198],intercept=F) # x is regressed on its future rb1<-rb1$residuals # extract residuals plot(rf1,rb1,main="Second PACF") # Scatterplot of residuals from 2 regs. comppacf<- c(comppacf,cor(rf1,rb1)) # Compute the correlation and store. #third partial - lag separation is 3 X2<-matrix(0,nrow=197,ncol=2) X2[ ,1]<-c(x[2:198]) X2[ ,2]<-c(x[3:199]) yf2<-c(x[4:200]) rf2<-lsfit(X2,yf2,intercept=F) # regression on the past rf2<-rf2$residuals yb2<-c(x[1:197]) rb2<-lsfit(X2,yb2,intercept=F) # regression on the future rb2<-rb2$residuals plot(rf2,rb2,main="Third PACF") comppacf<- c(comppacf,cor(rf2,rb2)) #fourth partial X3<-matrix(0,nrow=196,ncol=3) X3[ ,1]<-x[2:197] X3[ ,2]<-x[3:198] X3[ ,3]<-x[4:199] rf3<-lsfit(X3,x[5:200],intercept=F) # regression on the past rf3<-rf3$residuals rb3<-lsfit(X3,x[1:196],intercept=F) # regression on the future rb3<-rb3$residuals plot(rf3,rb3,main="Fourth PACF") comppacf<-c(comppacf,cor(rf3,rb3)) # Compare to values computed from Splus spluspacf<-acf(x,type="partial",plot=F) # Set up vector to extract values