# Example for model identification and forecasting # # Generate some training data from AR(1) or ARIMA(1,0,0) set.seed(1) series1a <- arima.sim(model=list(order=c(1,0,0), ar=0.8), n=100) series1 <- ts(series1a[1:90]) # Graph the data. par(mfrow=c(3,1)) plot(series1a) title("AR(1))") acf(series1) acf(series1, type="partial") # fit ARIMA model to first 90 observations fit <- arima(series1, order=c(1,0,0)) # and compute forecasts for last 10. pred <- predict(fit, n.ahead=10) # graph them. par(mfrow=c(1,1)) plot(c(series1,pred$pred), ylim=c(-4,4), type="n") lines(1:90,series1, lty=1) points(series1, pch=1) points(pred$pred, pch=3) lines(91:100, pred$pred + 2*pred$se, lty=2) lines(91:100, pred$pred - 2*pred$se, lty=2) points(91:100, series1a[91:100], pch=1) title("95% Forecast intervals") legend("topleft", c("data", "forecasts"), pch=c(1,3))