Get Started

Fitting an AR(1) model

Recall that you use the ACF and PACF pair to help identify the orders \(p\) and \(q\) of an ARMA model. The following table is a summary of the results:

AR(\(p\)) MA(\(q\)) ARMA(\(p,q\))
ACF Tails off Cuts off
after lag \(q\)
Tails off
PACF Cuts off
after lag \(p\)
Tails off Tails off

In this exercise, you will generate data from the AR(1) model, $$X_t = .9 X_{t-1} + W_t,$$ look at the simulated data and the sample ACF and PACF pair to determine the order. Then, you will fit the model and compare the estimated parameters to the true parameters.

Throughout this course, you will be using sarima() from the astsa package to easily fit models to data. The command produces a residual diagnostic graphic that can be ignored until diagnostics is discussed later in the chapter.

This is a part of the course

“ARIMA Models in R”

View Course

Exercise instructions

  • The package astsa is preloaded.
  • Use the prewritten arima.sim() command to generate 100 observations from an AR(1) model with AR parameter .9. Save this to x.
  • Plot the generated data using plot().
  • Plot the sample ACF and PACF pairs using the acf2() command from the astsa package.
  • Use sarima() from astsa to fit an AR(1) to the previously generated data. Examine the t-table and compare the estimates to the true values. For example, if the time series is in x, to fit an AR(1) to the data, use sarima(x, p = 1, d = 0, q = 0) or simply sarima(x, 1, 0, 0).

Hands-on interactive exercise

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

# Generate 100 observations from the AR(1) model
x <- arima.sim(model = list(order = c(1, 0, 0), ar = .9), n = 100) 

# Plot the generated data 


# Plot the sample P/ACF pair


# Fit an AR(1) to the data and examine the t-table

Edit and Run Code

This exercise is part of the course

ARIMA Models in R

IntermediateSkill Level
4.6+
16 reviews

Become an expert in fitting ARIMA (autoregressive integrated moving average) models to time series data using R.

You will discover the wonderful world of ARMA models and how to fit these models to time series data. You will learn how to identify a model, how to choose the correct model, and how to verify a model once you fit it to data. You will learn how to use R time series commands from the stats and astsa packages.

Exercise 1: AR and MA modelsExercise 2: Fitting an AR(1) model
Exercise 3: Fitting an AR(2) modelExercise 4: Fitting an MA(1) modelExercise 5: AR and MA togetherExercise 6: Fitting an ARMA modelExercise 7: Identify an ARMA modelExercise 8: Model choice and residual analysisExercise 9: Model choice - IExercise 10: Model choice - IIExercise 11: Residual analysis - IExercise 12: Residual analysis - IIExercise 13: ARMA get in

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