Get Started

Estimate the autoregressive (AR) model

For a given time series x we can fit the autoregressive (AR) model using the arima() command and setting order equal to c(1, 0, 0). Note for reference that an AR model is an ARIMA(1, 0, 0) model.

In this exercise, you'll explore additional qualities of the AR model by practicing the arima() command on a simulated time series x as well as the AirPassengers data. This command allows you to identify the estimated slope (ar1), mean (intercept), and innovation variance (sigma^2) of the model.

Both xand the AirPassengers data are preloaded in your environment. The time series x is shown in the figure on the right.

This is a part of the course

“Time Series Analysis in R”

View Course

Exercise instructions

  • Use arima() to fit the AR model to the series x. Closely examine the output from this command.
  • What are the slope (ar1), mean (intercept), and innovation variance (sigma^2) estimates from your previous command? Type them into your R workspace.
  • Now, fit the AR model to AirPassengers, saving the results as AR. Use print() to display the fitted model AR.
  • Finally, use the commands provided to plot the AirPassengers, calculate the fitted values, and add them to the figure.

Hands-on interactive exercise

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

# Fit the AR model to x
arima(___, order = ___)

# Copy and paste the slope (ar1) estimate


# Copy and paste the slope mean (intercept) estimate


# Copy and paste the innovation variance (sigma^2) estimate


# Fit the AR model to AirPassengers
AR <-
print(AR)

# Run the following commands to plot the series and fitted values
ts.plot(AirPassengers)
AR_fitted <- AirPassengers - residuals(AR)
points(AR_fitted, 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) model
Exercise 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