Get Started

Estimating an AR Model

You will estimate the AR(1) parameter, \(\small \phi\), of one of the simulated series that you generated in the earlier exercise. Since the parameters are known for a simulated series, it is a good way to understand the estimation routines before applying it to real data.

For simulated_data_1 with a true \(\small \phi\) of 0.9, you will print out the estimate of \(\small \phi\). In addition, you will also print out the entire output that is produced when you fit a time series, so you can get an idea of what other tests and summary statistics are available in statsmodels.

This is a part of the course

“Time Series Analysis in Python”

View Course

Exercise instructions

  • Import the class ARIMA in the module statsmodels.tsa.arima.model.
  • Create an instance of the ARIMA class called mod using the simulated data simulated_data_1 and the order (p,d,q) of the model (in this case, for an AR(1)), is order=(1,0,0).
  • Fit the model mod using the method .fit() and save it in a results object called res.
  • Print out the entire summary of results using the .summary() method.
  • Just print out an estimate of \(\small \phi\) using the .params[1] attribute (no parentheses).

Hands-on interactive exercise

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

# Import the ARIMA module from statsmodels
from statsmodels.tsa.arima.model import ARIMA

# Fit an AR(1) model to the first simulated data
mod = ARIMA(___, order=___)
res = mod.___

# Print out summary information on the fit
print(res.___)

# Print out the estimate for phi
print("When the true phi=0.9, the estimate of phi is:")
print(res.___)
Edit and Run Code

This exercise is part of the course

Time Series Analysis in Python

IntermediateSkill Level
4.5+
19 reviews

In this four-hour course, you’ll learn the basics of analyzing time series data in Python.

In this chapter you'll learn about autoregressive, or AR, models for time series. These models use past values of the series to predict the current value.

Exercise 1: Describe AR ModelExercise 2: Simulate AR(1) Time SeriesExercise 3: Compare the ACF for Several AR Time SeriesExercise 4: Match AR Model with ACFExercise 5: Estimating and Forecasting AR ModelExercise 6: Estimating an AR Model
Exercise 7: Forecasting with an AR ModelExercise 8: Let's Forecast Interest RatesExercise 9: Compare AR Model with Random WalkExercise 10: Choosing the Right ModelExercise 11: Estimate Order of Model: PACFExercise 12: Estimate Order of Model: Information Criteria

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