# fit recruits library(ts) #rec <- scan("E:\\Stat207\\Class\\Examples\\Ch2\\recruit.dat") rec <- scan("http://www.sci.csueastbay.edu/~esuess/Statistics_6871/Handouts/Handout6/recruit.dat") x <- ts(rec) # x is the time series object par(mfrow=c(3,1)) ts.plot(x, gpars=list(main="Recruits (x)")) acf(x) acf(x,type="partial") #indicates AR(2) fit but we'll also fit an ARMA(1,1) rec.ar2 <- ar(x, order.max = 10) # type help(ar) #to see what you get, e.g. rec.ar2$ar #gives parameters rec.ar2$aic # gives AIC from lag 0 to order.max # NOTE arima DOES FIT A CONSTANT rec.ar2 <- arima0(x, order=c(2,0,0)) # some results rec.ar2$var.coef rec.ar2$aic rec.ar2$sigma2 # now the ARMA(1,1) fit rec.arma <- arima0(x, order=c(1,0,1)) rec.arma$var.coef rec.arma$aic # Forecast 12 time units ahead for AR(2) model rec.ar2.fore <- predict(rec.arma,n.ahead=12) cbind(rec.ar2.fore$pred,rec.ar2.fore$se) par(mfrow=c(1,1)) ts.plot(x,rec.ar2.fore$pred,rec.ar2.fore$pred+2*rec.ar2.fore$se, rec.ar2.fore$pred-2*rec.ar2.fore$se)