# read data (may need to include full path) y1<-scan("d:/r/My Documents/itsm2000/lake.tsm") plot(y1,type='l',xlab='Year',ylab='Level of Lake') # Fit and display linear trend x1<-1:length(y1) reg1<-lm(y1~x1) lines(x1,reg1$fitted) summary(reg1) # calculate residuals, display acf and pacf y2<-reg1$resid acf(y2) # fit AR(2) and AR(3) models with regressor reg2<-arima(y1,order=c(2,0,0),xreg=x1) reg3<-arima(y1,order=c(3,0,0),xreg=x1) # Time series diagnostics tsdiag(reg2) # Predict next 10 values pred1<-predict(reg2,n.ahead=10,newxreg=99:108,se.fit=T) pred1$pred pred1$se # Redraw time series with predictions plot(x1,y1,xlab='Year',ylab='Lake Level',type='l',xlim=c(1,108), ylim=range(c(y1,pred1$pred-2*pred1$se,pred1$pred+2*pred1$se))) lines(99:108,pred1$pred,col='blue') lines(99:108,pred1$pred-2*pred1$se,col='red') lines(99:108,pred1$pred+2*pred1$se,col='red')