Get Started

Simple forecasts from an estimated AR model

Now that you've modeled your data using the arima() command, you are ready to make simple forecasts based on your model. The predict() function can be used to make forecasts from an estimated AR model. In the object generated by your predict() command, the $pred value is the forecast, and the $se value is the standard error for the forecast.

To make predictions for several periods beyond the last observations, you can use the n.ahead argument in your predict() command. This argument establishes the forecast horizon (h), or the number of periods being forecast. The forecasts are made recursively from 1 to h-steps ahead from the end of the observed time series.

In this exercise, you'll make simple forecasts using an AR model applied to the Nile data, which records annual observations of the flow of the River Nile from 1871 to 1970.

This is a part of the course

“Time Series Analysis in R”

View Course

Exercise instructions

  • Use arima() to fit an AR model to the Nile time series. Save this as AR_fit.
  • Use predict() to make a forecast for flow of the Nile in 1971.
  • Use predict_AR along with $pred[1] to obtain the 1-step forecast.
  • Use another call to predict() to make forecasts from 1 step ahead to 10 steps ahead (1971 to 1980). To do so, set the n.ahead command equal to 10.
  • Run the pre-written code to plot your Nile data plus the forecasts and a 95% prediction interval.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Fit an AR model to Nile
AR_fit <- arima(___, order  = ___)
print(AR_fit)

# Use predict() to make a 1-step forecast
predict_AR <- predict(___)

# Obtain the 1-step forecast using $pred[1]


# Use predict to make 1-step through 10-step forecasts
predict(___, n.ahead = ___)

# Run to plot the Nile series plus the forecast and 95% prediction intervals
ts.plot(Nile, xlim = c(1871, 1980))
AR_forecast <- predict(AR_fit, n.ahead = 10)$pred
AR_forecast_se <- predict(AR_fit, n.ahead = 10)$se
points(AR_forecast, type = "l", col = 2)
points(AR_forecast - 2*AR_forecast_se, type = "l", col = 2, lty = 2)
points(AR_forecast + 2*AR_forecast_se, type = "l", col = 2, lty = 2)
Edit and Run Code

This exercise is part of the course

Time Series Analysis in R

IntermediateSkill Level
4.5+
8 reviews

Learn the core techniques necessary to extract meaningful insights from time series data.

In this chapter, you will learn the autoregressive (AR) model and several of its basic properties. You will also practice simulating and estimating the AR model in R, and compare the AR model with the random walk (RW) model.

Exercise 1: The autoregressive modelExercise 2: Simulate the autoregressive modelExercise 3: Estimate the autocorrelation function (ACF) for an autoregressionExercise 4: Persistence and anti-persistenceExercise 5: Compare the random walk (RW) and autoregressive (AR) modelsExercise 6: AR model estimation and forecastingExercise 7: Estimate the autoregressive (AR) modelExercise 8: Simple forecasts from an estimated AR model

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free