# Autoregression Models for Time Series Forecasting With Python

Last Updated on September 7, 2021

Autoregression is a time series model that uses observations from previous time steps as input to a regression equation to predict the value at the next time step.

It is a very simple idea that can result in accurate forecasts on a range of time series problems.

In this tutorial, you will discover how to implement an autoregressive model for time series forecasting with Python.

After completing this tutorial, you will know:

• How to explore your time series data for autocorrelation.
• How to develop an autocorrelation model and use it to make predictions.
• How to use a developed autocorrelation model to make rolling predictions.

Kick-start your project with my new book Time Series Forecasting With Python, including step-by-step tutorials and the Python source code files for all examples.

Let’s get started.

• Updated May/2017: Fixed small typo in autoregression equation.
• Updated Apr/2019: Updated the link to dataset.
• Updated Sep/2019: Updated examples to use latest plotting API.
• Updated Apr/2020: Changed AR to AutoReg due to API change.

Autoregression Models for Time Series Forecasting With Python
Photo by Umberto Salvagnin, some rights reserved.

## Autoregression

A regression model, such as linear regression, models an output value based on a linear combination of input values.

For example:

Where yhat is the prediction, b0 and b1 are coefficients found by optimizing the model on training data, and X is an input value.

This technique can be used on time series where input variables are taken as observations at previous time steps, called lag variables.

For example, we can predict the value for the next time step (t+1) given the observations at the last two time steps (t-1 and t-2). As a regression model, this would look as follows:

Because the regression model uses data from the same input variable at previous time steps, it is referred to as an autoregression (regression of self).

### Stop learning Time Series Forecasting the slow way!

Take my free 7-day email course and discover how to get started (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

## Autocorrelation

An autoregression model makes an assumption that the observations at previous time steps are useful to predict the value at the next time step.

This relationship between variables is called correlation.

If both variables change in the same direction (e.g. go up together or down together), this is called a positive correlation. If the variables move in opposite directions as values change (e.g. one goes up and one goes down), then this is called negative correlation.

We can use statistical measures to calculate the correlation between the output variable and values at previous time steps at various different lags. The stronger the correlation between the output variable and a specific lagged variable, the more weight that autoregression model can put on that variable when modeling.

Again, because the correlation is calculated between the variable and itself at previous time steps, it is called an autocorrelation. It is also called serial correlation because of the sequenced structure of time series data.

The correlation statistics can also help to choose which lag variables will be useful in a model and which will not.

Interestingly, if all lag variables show low or no correlation with the output variable, then it suggests that the time series problem may not be predictable. This can be very useful when getting started on a new dataset.

In this tutorial, we will investigate the autocorrelation of a univariate time series then develop an autoregression model and use it to make predictions.

Before we do that, let’s first review the Minimum Daily Temperatures data that will be used in the examples.

## Minimum Daily Temperatures Dataset

This dataset describes the minimum daily temperatures over 10 years (1981-1990) in the city Melbourne, Australia.

The units are in degrees Celsius and there are 3,650 observations. The source of the data is credited as the Australian Bureau of Meteorology.

The code below will load the dataset as a Pandas Series.

Running the example prints the first 5 rows from the loaded dataset.

A line plot of the dataset is then created.

Minimum Daily Temperature Dataset Plot

## Quick Check for Autocorrelation

There is a quick, visual check that we can do to see if there is an autocorrelation in our time series dataset.

We can plot the observation at the previous time step (t-1) with the observation at the next time step (t+1) as a scatter plot.

This could be done manually by first creating a lag version of the time series dataset and using a built-in scatter plot function in the Pandas library.

But there is an easier way.

Pandas provides a built-in plot to do exactly this, called the lag_plot() function.

Below is an example of creating a lag plot of the Minimum Daily Temperatures dataset.

Running the example plots the temperature data (t) on the x-axis against the temperature on the previous day (t-1) on the y-axis.

Minimum Daily Temperature Dataset Lag Plot

We can see a large ball of observations along a diagonal line of the plot. It clearly shows a relationship or some correlation.

This process could be repeated for any other lagged observation, such as if we wanted to review the relationship with the last 7 days or with the same day last month or last year.

Another quick check that we can do is to directly calculate the correlation between the observation and the lag variable.

We can use a statistical test like the Pearson correlation coefficient. This produces a number to summarize how correlated two variables are between -1 (negatively correlated) and +1 (positively correlated) with small values close to zero indicating low correlation and high values above 0.5 or below -0.5 showing high correlation.

Correlation can be calculated easily using the corr() function on the DataFrame of the lagged dataset.

The example below creates a lagged version of the Minimum Daily Temperatures dataset and calculates a correlation matrix of each column with other columns, including itself.

This is a good confirmation for the plot above.

It shows a strong positive correlation (0.77) between the observation and the lag=1 value.

This is good for one-off checks, but tedious if we want to check a large number of lag variables in our time series.

Next, we will look at a scaled-up version of this approach.

## Autocorrelation Plots

We can plot the correlation coefficient for each lag variable.

This can very quickly give an idea of which lag variables may be good candidates for use in a predictive model and how the relationship between the observation and its historic values changes over time.

We could manually calculate the correlation values for each lag variable and plot the result. Thankfully, Pandas provides a built-in plot called the autocorrelation_plot() function.

The plot provides the lag number along the x-axis and the correlation coefficient value between -1 and 1 on the y-axis. The plot also includes solid and dashed lines that indicate the 95% and 99% confidence interval for the correlation values. Correlation values above these lines are more significant than those below the line, providing a threshold or cutoff for selecting more relevant lag values.

Running the example shows the swing in positive and negative correlation as the temperature values change across summer and winter seasons each previous year.

Pandas Autocorrelation Plot

The statsmodels library also provides a version of the plot in the plot_acf() function as a line plot.

In this example, we limit the lag variables evaluated to 31 for readability.

Statsmodels Autocorrelation Plot

Now that we know how to review the autocorrelation in our time series, let’s look at modeling it with an autoregression.

Before we do that, let’s establish a baseline performance.

## Persistence Model

Let’s say that we want to develop a model to predict the last 7 days of minimum temperatures in the dataset given all prior observations.

The simplest model that we could use to make predictions would be to persist the last observation. We can call this a persistence model and it provides a baseline of performance for the problem that we can use for comparison with an autoregression model.

We can develop a test harness for the problem by splitting the observations into training and test sets, with only the last 7 observations in the dataset assigned to the test set as “unseen” data that we wish to predict.

The predictions are made using a walk-forward validation model so that we can persist the most recent observations for the next day. This means that we are not making a 7-day forecast, but 7 1-day forecasts.

Running the example prints the mean squared error (MSE).

The value provides a baseline performance for the problem.

The expected values for the next 7 days are plotted (blue) compared to the predictions from the model (red).

Predictions From Persistence Model

## Autoregression Model

An autoregression model is a linear regression model that uses lagged variables as input variables.

We could calculate the linear regression model manually using the LinearRegession class in scikit-learn and manually specify the lag input variables to use.

Alternately, the statsmodels library provides an autoregression model where you must specify an appropriate lag value and trains a linear regression model. It is provided in the AutoReg class.

We can use this model by first creating the model AutoReg() and then calling fit() to train it on our dataset. This returns an AutoRegResults object.

Once fit, we can use the model to make a prediction by calling the predict() function for a number of observations in the future. This creates 1 7-day forecast, which is different from the persistence example above.

The complete example is listed below.

Running the example the list of coefficients in the trained linear regression model.

The 7 day forecast is then printed and the mean squared error of the forecast is summarized.

A plot of the expected (blue) vs the predicted values (red) is made.

The forecast does look pretty good (about 1 degree Celsius out each day), with big deviation on day 5.

Predictions From Fixed AR Model

The statsmodels API does not make it easy to update the model as new observations become available.

One way would be to re-train the AutoReg model each day as new observations become available, and that may be a valid approach, if not computationally expensive.

An alternative would be to use the learned coefficients and manually make predictions. This requires that the history of 29 prior observations be kept and that the coefficients be retrieved from the model and used in the regression equation to come up with new forecasts.

The coefficients are provided in an array with the intercept term followed by the coefficients for each lag variable starting at t-1 to t-n. We simply need to use them in the right order on the history of observations, as follows:

Below is the complete example.

Again, running the example prints the forecast and the mean squared error.

We can see a small improvement in the forecast when comparing the error scores.

Predictions From Rolling AR Model

This section provides some resources if you are looking to dig deeper into autocorrelation and autoregression.

## Summary

In this tutorial, you discovered how to make autoregression forecasts for time series data using Python.

Specifically, you learned:

• About autocorrelation and autoregression and how they can be used to better understand time series data.
• How to explore the autocorrelation in a time series using plots and statistical tests.
• How to train an autoregression model in Python and use it to make short-term and rolling forecasts.

## Want to Develop Time Series Forecasts with Python?

#### Develop Your Own Forecasts in Minutes

...with just a few lines of python code

Discover how in my new Ebook:
Introduction to Time Series Forecasting With Python

It covers self-study tutorials and end-to-end projects on topics like: Loading data, visualization, modeling, algorithm tuning, and much more...

### 195 Responses to Autoregression Models for Time Series Forecasting With Python

1. Gary Bake January 5, 2017 at 11:35 pm #

Thank you Jason for the awesome article

In case anyone hits the same problem I had –
It was failing to be imported due to three rows in the temperature column containing ‘?’.
Once these were removed the data imported ok.

• Jason Brownlee January 6, 2017 at 9:10 am #

Thanks for the heads up Gary.

• yaser alsultan December 16, 2019 at 3:57 pm #

hi man , what do you think the best model that can deal with hour

• Jason Brownlee December 17, 2019 at 6:29 am #

There are no best algorithms. I recommend testing a suite of methods in order to discover what works best for your specific dataset.

2. Tim Melino January 14, 2017 at 10:28 am #

Hey Jason, thanks for the article. How would you go about forecasting from the end of the file when expected value is not known?

• Jason Brownlee January 15, 2017 at 5:27 am #

Hi Tim, you can use mode.predict() as in the example and specify the index of the time step to be predicted.

3. Farrukh Jalali January 25, 2017 at 4:16 pm #

Hi Jason,

Thanks for all of your wonderful blogs. They are really helping a lot. One question regarding this post is that I believe that AR modeling also presume that time series is stationary as the observations should be i.i.d. .Does that AR function from statsmodels library checks for stationary and use the de-trended de-seasonalized time series by itself if required? Also, if we use sckit learn library for AR model as you described do we need to check for and make adjustments by ourselfs for this?

• Jason Brownlee January 26, 2017 at 4:45 am #

Hi Farrukh, great question.

The AR in statsmodels does assume that the data is stationary.

If your data is not stationary, you must make it stationary (e.g. differencing and other transforms).

• Farrukh Jalali January 31, 2017 at 12:19 pm #

Thanks for the answer. Though we did not conduct proper test here for trend/seasonal stationarity check in the example above but from figure apparently it seems like that there is a seasonal effect. So in that case whether applying AR model is good to go?

• Jason Brownlee February 1, 2017 at 10:39 am #

Great question Farrukh.

AR is designed to be used on stationary data, meaning data with no seasonal or trend information.

4. Farrukh Jalali January 31, 2017 at 11:07 pm #

Or to be specific, is it OK to apply AR model direct here on the given data without checking the seasonality and removing it if present which is showing some signs in first graph apparently?

• Muneeb November 11, 2020 at 4:19 am #

Hi. I am working on something similar and i have the same question? in the above AR model, the normal non transformed time series has been used and it has not been made stationary? is that correct and applicable?

• Jason Brownlee November 11, 2020 at 6:53 am #

It is a good idea to make the series stationary before using an AR model.

5. Anthony of Sydney March 8, 2017 at 5:09 am #

Dear Dr Jason,
I had a go at the ‘persistence model’ section.
The dataset I used was the sp500.csv dataset.

As soon as I try to compute the “test_score”, I get the following error,

Any idea,
Anthony of Sydney NSW

• Jason Brownlee March 8, 2017 at 9:46 am #

It looks like you need to convert your data to floating point values.

E.g. in numpy this might be:

6. Anthony of Sydney who noticed that it was a string not a float March 8, 2017 at 5:30 am #

Dear Dr Jason,
Fixed the problem. You cannot assume that all *.csv numbers are floats or ints. For some reason, the numbers seem to be enclosed in quotes. Note that the data is is for the sp500.csv not the above exercise.
Note the numbers in the output are enclosed in quotes:

It works now,
Regards
Anthony from Sydney

• Jason Brownlee March 8, 2017 at 9:46 am #

Glad to hear you fixed it Anthony.

7. Anthony from Sydney March 8, 2017 at 6:07 am #

Dear Dr Jason,
The problem has been fixed. The values in the array were strings, so I had to convert them to strings.

So I converted the strings in each array to float.

Hope that helps others trying to convert values to appropriate data types in order to do numerical calculations.
Anthony from Sydney NSW

8. Johnson S April 17, 2017 at 6:39 am #

how can I show my prediction as date, instead of log, for example I have data set incident number for each week, I want to predict the following week
week1 669
week2 443
week3 555

so on april week 1 I want to show time series the prediction of week1 week2 of April

• Jason Brownlee April 18, 2017 at 8:28 am #

Sorry, I’m not sure I understand. Perhaps you can give a fuller example of inputs and outputs?

9. Chika April 30, 2017 at 2:46 am #

Thanks for this wonderful tutorial. I discovered your articles on facebook last year. Since then I have been following all your tutorials and I must confess that, though I started learning about machine learning in less than a year, my knowledge base has tremendously increased as a result of this free services you have been posting for all to see on the website.
Thanks once more for this generosity Dr Jason.

My question is that, I have carefully followed all your procedures in this article on my data set that was recorded every 10mins for 2 months. Please I would like to know which time lag is appropriate for forecasting to see the next 7 days value or more or less.

My time series is a stationary one according to the test statistics and histogram I have applied on it. but I still don’t know if a reasonable conclusion can be reached with a data set that was recorded for 2 months every 10mins daily.

10. Soy May 14, 2017 at 4:35 am #

Dr Jason,
Thank you so much for this post. I have finally learned how to go from theory to practice.

• Jason Brownlee May 14, 2017 at 7:33 am #

I’m so glad to hear that Soy, thanks!

11. Akshit Mantri June 8, 2017 at 10:35 pm #

Dr Jason, How do i predict the low and high confidence interval of prediction of an AR model?

• Jason Brownlee June 9, 2017 at 6:25 am #
• Akshit Mantri June 9, 2017 at 5:40 pm #

Thanks for the reply. It was a very good post. But, it was for ARIMA model. I am having some problem with the ARIMA model I cannot use it. Is there such a confidence interval forecasting for AR model?

• Jason Brownlee June 10, 2017 at 8:18 am #

Yes, I believe the same approach applies. Use the forecast() function.

12. Daoud June 11, 2017 at 9:10 pm #

ValueError: On entry to DLASCL parameter number 4 had an illegal value
I ma found above error when i use
model = AR(train) ## no error
model_fit = model.fit() ## show above error

13. Samuel N June 15, 2017 at 6:10 am #

Thanks for the great write-up Jason. One question though , I am interested in doing an autoregression on my timeseries data every nth days. For example. Picking the first 20 days and predicting the value of the 20th day. Then picking the next 20 days (shift 1 over) and predict the value of the 21st day and so forth until the end of my dataset. How can this be achieved in code? Thanks.

14. Dhineshkumar June 15, 2017 at 5:49 pm #

Hi Jason,

Thanks for your article. I have few doubts.

1) We do analysis on the autocorrelation plots and auto-correlation function only after making the time series stationary right?

2) For the time series above, the correlation value is maximum for lag=1. So is it like the value at t-1 is given more weight while doing Autoregression?

3) When the AR model says, for lag-29 model the MSE is minimum. Does it mean the regression model constructed with values from t to t-29 gave minimum MSE?

Thank you

• Jason Brownlee June 16, 2017 at 7:52 am #

1. Ideally, yes, analysis after the data is stationary.

2. Yes.

3. Yes.

• Dhineshkumar June 16, 2017 at 6:46 pm #

Thanks.

15. TaeWoo Kim June 20, 2017 at 10:10 am #

Hey Jason. Thanks for the awesome tutorial.

in this line…

print(‘Lag: %s’ % model_fit.k_ar)
print(‘Coefficients: %s’ % model_fit.params)

What is the “layman” explanation of what lag and coeffcients are?

Is it that “lag” is what the AR model determined that the significance ends (i.e. after this number, the autocorrelation isn’t “strong enough) and that the coeff. are the p-value of null hypotehsis on “intensity” of the autocorrelation?

• Jason Brownlee June 21, 2017 at 8:08 am #

Lag observations are observations at prior time steps.

Lag coefficients are the weightings on those lag observations.

• TaeWoo Kim June 22, 2017 at 5:03 am #

Thank you Jason. That answer has still gotten me more confused. 🙂

The section where you manually calculate the predictions.. you’re specifying history.append(obs). So does this mean that the test data up to t0 is required to predict t1?

In another words, this model can only predict 1 time period away ?

If I am wrong, how do I tweak the code to predict up to n periods away?

• TaeWoo Kim June 22, 2017 at 5:49 am #

Example you gave with statsmodels.tsa.ar_model.AR is for single step prediction, am I correct?

• Jason Brownlee June 22, 2017 at 6:13 am #

Correct, but the forecast() function for ARIMA (and maybe AR) can make a multi-step forecast. It might just not be very good.

16. Yigit Yazicioglu September 30, 2017 at 12:29 am #

Dear Jason

Thanks for this wonderful guideline. I have a problem with my dataset and I am digging in time series modeling.

I try to model a physical model such that

y(t+1) = y(t) + f(a(t), b(t), c(t)) with AR models.

Actually problem is predict metal temperature using basic heat transfer where y means metal Temperature and f is a function of some sensors data like coolant mass flow rate, coolant temperaute, gas flowrate and gas temperatue.

Which model do you advise to use?

17. Monika October 13, 2017 at 3:48 am #

Hi Jason…
I read your few articles and found very helpful. I am very new to machine learning and would like to ask the meaning of these prediction points such as predicted=14.349246 so what is the meaning of this value does it mean???

how does it help to understanding the prediction?

• Jason Brownlee October 13, 2017 at 5:50 am #

Sorry, I don’t follow. Perhaps you can rephrase your question?

18. Shantanu October 16, 2017 at 6:30 am #

Thanks for the wonderful article.
I have a question: How can we make a prediction based on multiple columns by AR?
For an example, there are columns of
Date, Pricing, ABC, PQR
ABC, PQR contribute in predicting prices. So I want to predict pricing based on these columns as well.
Thank you

• Jason Brownlee October 16, 2017 at 3:46 pm #

It may be possible, but I do not have a worked example, sorry.

19. Ryan Berger October 22, 2017 at 4:00 am #

How could you make this a Deep Autoregressive Network with Keras?

• Jason Brownlee October 22, 2017 at 5:33 am #

A deep MLP will do the trick!

20. Forouq November 10, 2017 at 3:28 am #

Thank you for the amazing tutorial
I have a question though. According to Pandas’ Autocorrelation plot, the maximum correlation is gained when lag=1. But the AR model selects lag=29 to build the autoregression.
I checked this code on my dataset, and the autoregression with lag=1 performed much better on test case that lag=14 chosen by AR model. Can you explain this? I thought that autocorrelation checks for linear relationship, thus, the autoregression which maps a linear function to the data should naturally perform best on the lag variable giving the maximum Pearson correlation.

• Jason Brownlee November 10, 2017 at 10:37 am #

Perhaps the method was confused by noise in the data or small sample size?

21. Duplec November 19, 2017 at 12:45 am #

What if time is also included along with the date?
1981-01-01,20.7 is like 1981-01-01 03:00:00,20.7

22. sam November 26, 2017 at 11:46 am #

Hi great tutorial. Just wanted to ask how would I change the order or lag in the code? Also if you had any tutorials for understanding how to use the statsmodels library.

23. At December 31, 2017 at 4:25 am #

Thanks a lot! I ran into a problem while developing the AR model when some of the dates were dropped. I had to mention the frequency parameter even though I was already supplying the date-times

• Jason Brownlee December 31, 2017 at 5:21 am #

Interesting. Did it fix your issues?

24. Carolyn March 14, 2018 at 1:52 am #

Hi Jason,

Excellent article! Any chance of a blog post on how to do vector autoregression with big data? Sometimes an LSTM is overkill, and even a vanilla RNN can be overkill, so something with just plain old autoregression would be great.

The VAR package in Python does this, but it runs into memory issues very quickly with large, sparse datasets.

If you know of any other tools for vector autoregression, any insight you have would be appreciated!

• Jason Brownlee March 14, 2018 at 6:31 am #

Thanks for the suggestion Carolyn.

25. Harshil April 23, 2018 at 7:04 pm #

Dear Jason,

Thank you so much for your wonderful article. I have a doubt regarding Data driven forecasting. I need to forecast appliance energy which depends upon 26 variables.I have data of appliance energy along with 26 variables of 3 months. With the help of 26 variables How can I forecast appliance energy for future?

26. Praveen May 27, 2018 at 10:21 pm #

Hey Jason!
Thank you for the article. I have a doubt regarding this. How can I make predictions for future dates, that are not present in the dataset?

27. Ashish June 21, 2018 at 5:43 pm #

can you please provide me a detaile example over VAR model?

• Jason Brownlee June 22, 2018 at 6:03 am #

I hope to cover the method in the future, thanks for the suggestion.

28. Bhaskar Sharan July 10, 2018 at 6:29 pm #

from pandas import Series
from matplotlib import pyplot
series = Series.from_csv(‘G:\Study_Material\daily-minimum-temperatures.csv’)
series.plot()
pyplot.show()

ha an error as : Empty ‘DataFrame’: no numeric data to plot

how to resolve it sir?

• Jason Brownlee July 11, 2018 at 5:53 am #

Perhaps double check you have loaded the data correctly?

29. Vijay July 30, 2018 at 10:53 am #

Its a wonderful post that I came across and thanks a lot putting up great content with great examples. I am new to machine learning and I have a question regarding the use of ARIMA for sparse timeseries. I have events that can recur every day, week, once a few weeks, or monthly. Typical example of this could be business meetings. Different meetings could happen at different frequencies. Is it appropriate to use ARIMA for predicting the underlying pattern. My real world problem involves predicting the size of virtual meetings based on their past history. Lets assume a service like hangout. I am trying to see if ARIMA would be an appropriate algorithm for predicting resource requirement for a virtual meeting based on its history. I tried ARIMA based on this tutorial but the results weren’t convincing. I wasn’t sure if its appropriate to model this problem as a time series problem and is ARIMA a good choice for such problems.

• Jason Brownlee July 30, 2018 at 2:16 pm #

Perhaps try it as a starting point.

30. Tim Boons August 7, 2018 at 6:53 pm #

I’m trying to work out o AR model to forecast a series using a lag of 192.

The series has a datapoint every 15 min but the you receive the data, the day after it was measured.
So you have to forecast the next day (D+1) with data of the previous day (D-1) hence a lag of 192 in datapoints that are 15 min apart.

Is there a way to contrain the AR() function to all datapoints before t-192 ?

• Jason Brownlee August 8, 2018 at 6:16 am #

Perhaps fit a regularized linear regression model directly on your chosen lags?

• Tim Boons August 14, 2018 at 10:14 pm #

31. Kamal Pokharel August 27, 2018 at 4:54 pm #

Thanks a lot for this lovely article

• Jason Brownlee August 28, 2018 at 5:57 am #

Thanks, I’m happy it helped.

32. Allison August 28, 2018 at 1:00 pm #

Hi Dr. Brownlee,

Fantastic article — I’m following along step by step and it’s helping.

at the step:
model.fit()

I get this error:
TypeError: Cannot find a common data type.

Where could this come from?

• Jason Brownlee August 29, 2018 at 7:59 am #

That is odd. Are you using the code and data from the tutorial?

Did you replace the “?” chars in the data file?

33. Phil September 29, 2018 at 4:49 am #

X(t) = b0 + b1*X(t-1) + b2*X(t-2)

X(t+1) = b0 + b1*X(t-1) + b2*X(t-2)

• Jason Brownlee September 29, 2018 at 6:38 am #

Then where is “t”?

• asieh August 28, 2019 at 6:30 am #

I have the same question as Phil, and I’m not sure what you mean by the answer. When you wrote: “we can predict the value for the next time step (t+1) given the observations at the last two time steps (t-1 and t-2)”, the last two time steps for (t+1) are t, t-1 not t-1 and t-2.
Why did you jump over t?

• asieh August 28, 2019 at 6:35 am #

also, in the following sentence you said it the right way:

“We can plot the observation at the previous time step (t-1) with the observation at the next time step (t+1) as a scatter plot.”

So you are talking of current time (t) vs previous step (t-1) vs next step (t+1).

That’s why I don’t think the formula should be as Phil mentioned.

34. ML Uros October 12, 2018 at 2:12 am #

I was wondering if you could help me with the following question: in your example, you choose 7 points as your test set and the AR model has a lower MSE for these points than the persistence model. However, if I make the test set larger, say a couple hundred points, and make AR predictions, I get a higher MSE with the AR model. I know a couple hundred points means like a year of data points and the AR model could be updated in between to obtain better results, but I was just wondering what is your view on this matter. Does it mean that the AR model is not suitable for predictions too far in the future?
Also, if I use the AR model for predicting about 180 points, AR’s MSE value rises quite significantly, to roughly 9. Interestingly, if the test set is enlarged even more to about 350 points, MSE value falls to about 7. Persistence model’s MSE has lower variability. What does this changing MSE say about the data and applying AR to it?

• Jason Brownlee October 12, 2018 at 6:42 am #

The further you predict into the future, the worse the performance.

35. Dieu Do October 13, 2018 at 1:54 pm #

Hello Dr. Jason Brownlee,

Thanks a lot for your excellent article.
I have a question related to predicted results.
Why the predicted solution at i-th point is very close the expected solution at (i-1)-th point ?
In all most your article, I have seen this.
I don’t understand why? Can you help me answer this question, please?

Thank you so much Dr Jason Brownlee.

Best,
Dieu Do

36. Yingfei October 22, 2018 at 1:21 am #

Hi Jason, thanks for your sharing.

I am trying to use AR model to predict a complex-valued time series. I used a series as below and replace the tempreature data in your example code:

series = Series([1, 1+1j, 2, 3, 4, 5, 8, 1+2j, 3, 5])

However, it reports an error message like this:

/anaconda/lib/python3.6/site-packages/statsmodels/tsa/tsatools.py in lagmat(x, maxlag, trim, original, use_pandas)
377 dropidx = nvar
378 if maxlag >= nobs:
–> 379 raise ValueError(“maxlag should be < nobs")
380 lm = np.zeros((nobs + maxlag, nvar * (maxlag + 1)))
381 for k in range(0, int(maxlag + 1)):

ValueError: maxlag should be < nobs

Please shed a light on how to correct it.

Thanks.

• Jason Brownlee October 22, 2018 at 6:20 am #

The error suggests you may need to change the configuration of the model to suite your data.

37. NikosLamp November 22, 2018 at 9:38 am #

Hello,

In the section “Quick Check for Autocorrelation”, you shifted the data by one position back and you named the columns ‘t-1’ and ‘t+1’. In the article ‘https://machinelearningmastery.com/convert-time-series-supervised-learning-problem-python/’ in the section ‘Pandas shift() Function’ you have the code line: ‘df[‘t-1’] = df[‘t’].shift(1)’ that is shifted by one means 1 time difference(t-1, t). Can you explain which one is correct? What point have I missed?

Thanks

• Jason Brownlee November 22, 2018 at 2:10 pm #

Really it should be ‘t’ and ‘t-1.

38. Yannick December 13, 2018 at 9:24 pm #

Hi Dr. Brownlee,

I have used your tutorial to make some predictions on a dataset which records every minute the number of used parking spaces for 2017. For the Persistence model I get a test MSE score of 12.7 and for the Autoregression model a test MSE score of 74. Could you tell me if it is good or bad? In the meantime, could you give me more details on how the MSE results work on your code does it run on the entire dataset?

Regards.

• Jason Brownlee December 14, 2018 at 5:30 am #

Good or bad is only knowable in comparison to the persistence model.

You can answer this question yourself.

39. Awibawa December 20, 2018 at 1:16 pm #

Hi Dr. Brownlee,
Is there any references and example code of NARX (Non-Linear AutoRegressive with eXogenous inputs) ? I apologize if this is out of topic probably you have experience about this

regards,

40. Markus December 23, 2018 at 10:07 pm #

When we say that a given model makes use of lag value of 3, which one of the followings is the given model equation:

X(t) = b0 + b1*X(t-1) + b2*X(t-2)+ b3*X(t-3)
X(t) = b0 + b3*X(t-3)

I assume it’s the first one, but I’m not sure.

• Jason Brownlee December 24, 2018 at 5:29 am #

It will be a linear function of three prior time steps to the step being predictions.

The specifics of the linear function will vary across algorithms.

41. Victor February 25, 2019 at 1:45 am #

Hi Jason,

How can I change the order for the AR model using this code.

E.g. AR(1), AR(2) etc?

• Jason Brownlee February 25, 2019 at 6:46 am #

Create the AR model and provide an integer for the order.

• Victor February 25, 2019 at 9:12 am #

I don’t really understand what you mean?

I’d just like to know how to do it based on the example you gave. Wouldn’t it just be a single line where I add the extra parameter as ‘order number’?

42. WiseNetAI March 6, 2019 at 6:02 am #

Could you please explain why it’s not “cheating”, so to speak, to append the observation at a time step to the history list. Isn’t this equivalent to feeding the model part of the data it is trying to predict? Is this possible in real life situations where we may not know the actual value of the thing we are trying to predict? I am referring to line 26 in the code which generates the “Predictions From Rolling AR Model.” I will appreciate it you could enlighten me about this please. Thank you.

• Jason Brownlee March 6, 2019 at 8:00 am #

No.

It is called walk-forward validation and under this model we are assuming that the true observation becomes available after the prediction was made, and in turn can be used by the model.

If this assumption does not hold for your data, you can design a walk forward validation strategy that captures the assumptions for your specific forecast problem.

More on walk forward validation here:
https://machinelearningmastery.com/backtest-machine-learning-models-time-series-forecasting/

• WiseNetAI March 7, 2019 at 11:32 am #

Thank you. That helped resolve my uncertainty.

43. Nihar Dharia March 16, 2019 at 1:17 am #

Hi,

model.predict(start , end) always gives me the same value and I end up getting a straight line prediction. So I used the history method shown above and kept adding yhat to test for the number of out of sample predictions I wanted (code below). Is it correct?

for t in range(len(test)+number_of_values_to_predict):
length = len(history)
lag = [history[i] for i in range(length-window,length)]
yhat = coef[0]
for d in range(window):
yhat += coef[d+1] * lag[window-d-1]
obs = test[t]
predictions.append(yhat)
history.append(obs)
test = np.append(test,yhat)

• Jason Brownlee March 16, 2019 at 7:55 am #

I’m not really sure what you’re trying to achieve?

• Nihar Dharia March 16, 2019 at 11:01 am #

I am trying to achieve out of sample forecasting like forecasting the value for the next 7 days. Using predict() gives me the same predicted value and gives me a straight line prediction. Hence I tried to use the code above. Do you think it is correct to do that?

• Jason Brownlee March 17, 2019 at 6:15 am #

Perhaps try a different model or use different data?

44. Maysaa April 4, 2019 at 6:38 pm #

Hey Jason,
You are only providing an autoregression model in a case that you only have one label or one input which is previous records of the same output. What if for example we are concerned about the prediction of energy consumption of a house and we have different input labels like inddor temp, outdoor temp and taking into considertion the home architecture and previous records.
How can we develop such a model?
do you have an example for that?
Thank you

45. Haiyang Duan April 17, 2019 at 10:54 am #

Hey Jason, I’m following your tutorial using my own dataset. But I have some questions about the results. (1)what does the Lag , that is the value of model_fit.k_ar, mean for your dataset?(2)what is the meaning of the period of Figure “Pandas Autocorrelation Plot” ? Could you take a moment to tell me something about them? Thank you very much.

46. Dav April 24, 2019 at 8:39 pm #

Hi

Thanks for this useful article. Where can I get the data file “daily-minimum-temperatures.csv”? I can’t reach the target site when I click on “Learn more about the dataset here”.

Thanks

47. Thibault de Wit June 6, 2019 at 5:10 am #

Hi Jason,

Quick question here. Why do we start the train dataset at 1 and not 0?

In your code we have: train, test = X[1:len(X)-7], X[len(X)-7:]
But having this instead: train, test = X[:len(X)-7], X[len(X)-7:] would allow the model to train on 1 more value (starting at index 0 instead of starting at index 1).

• Jason Brownlee June 6, 2019 at 6:39 am #

Thanks. Not sure what I was thinking there…

48. Martin Muñoz Mandujano July 2, 2019 at 12:41 pm #

Hi Jason,
what order is the AR model in the code?
Or in what way is the order defined?

• Jason Brownlee July 2, 2019 at 2:10 pm #

Order is the number of lag observations considered by the model.

You can grid search different values or use an ACF/PACF plot to choose the value.

49. Ank July 28, 2019 at 7:07 pm #

Hi Jason,
I am new to python, is it the update issue of pandas?

I cannot run the code for “Quick Check for Autocorrelation :”

unless adding the below 2 line of code

Data[‘Date’] = pd.to_datetime(Data[‘Date’])
series=pd.Series(Data[‘Temp’])

lag_plot(series)
pyplot.show()

thank you

50. Leo Barbosa July 29, 2019 at 10:11 am #

Hi Jason,

Thank you for the tutorial, very helpful. I have a question though

How is your last example (rolling forecast) different to what statsmodels.tsa.ar_model.AR.predict() would do when dynamic=False?
The way I understand it, it would do what you did in your last example, but you are getting different results, so not sure what I am missing.

From statsmodels website:
“The dynamic keyword affects in-sample prediction. If dynamic is False, then the in-sample lagged values are used for prediction. If dynamic is True, then in-sample forecasts are used in place of lagged dependent variables. The first forecasted value is start.”

• Jason Brownlee July 29, 2019 at 2:21 pm #

Thanks.

I believe dynamic only effects “in sample” data, e.g. data within the scope of the training data.

51. Manpreet Singh September 9, 2019 at 8:49 am #

Hi Jason,

Thank you for the tutorial, very helpful. I have a question though

You wrote that “the statsmodels library provides an autoregression model that automatically selects an appropriate lag value using statistical tests and trains a linear regression model.”

Is it using any model selection criteria like AIC, BIC to select the appropriate lag value or is it approximating all the models and picking the one with the least MSE?

52. George September 11, 2019 at 4:43 am #

Hey Jason,
Do you know what the methods are for validating AR model?
Specific tests that could be done to show the model developed in valid

53. Leo September 24, 2019 at 2:56 pm #

Hi Jason,

Thank you for the tutorial, very helpful. I have a question that your ACF lag is big in this case and so as I. How can we decide to choose the ARIMA parameters if the lag of ACF or PACF is very big ? In my case , my ACF decay towards to zero at lag 1000, and PACF at lag 30.

54. Laguna October 16, 2019 at 4:31 am #

Thank you for the article.
Isn’t partial autocorrelation (PACF) plot supposed to be used to determine the statistically significant lag cofficients for an AR model. Isn’t ACF used for the Moving average part, not the AR part of an ARMA model? I was confused by the choice of using the ACF plot for the AR part of the model

55. karan February 10, 2020 at 6:11 pm #

Hi Jason. Very nice and clear explanation.

56. Evan Berry March 2, 2020 at 8:59 am #

Hi Jason, I was wondering if this technique of lag variables could be used for other features. So it seems like above you are predicting weather so you are using lag variables of weather data. What if we wanted to use lag variables of another variable such as total sunlight per day. Even though this is a different variable can you use the say technique in terms of lagging data to predict weather?

• Jason Brownlee March 2, 2020 at 10:07 am #

Yes, it can be useful for any time series data.

57. Mark March 28, 2020 at 1:18 am #

Hi Jason

I am trying to do a dynamic forecast using OLS. The model has an AR(1) variable and n exdog variables.

I use a for loop to run the regression many times for different combinations of the exdog variables and store the results. I split into test/train sets and run a prediction on the test set and store the MAE and RMSE.

The problem I have is the out of sample test is using the actual lagged AR(1) variable rather than dynamically generating it. I want to use the realized values of the exdog variables but the dynamically estimated AR(1) variable.

Do you have any thoughts on the best way of doing this please? I have looked at SARIMAX as an alternative but wanted to use OLS ideally.

Also which of your books would you recommend for statsmodels/OLS/time series forecasting?

Do you also have anything on monte carlo in python?

Cheers

Mark

• Jason Brownlee March 28, 2020 at 6:24 am #

I don’t follow the specific problem you’re having sorry. “generated” do you mean predicted as in a recursive model?

I cover the basics of time series forecasting with ARIMA in this book (no exog or sarima though):
https://machinelearningmastery.com/introduction-to-time-series-forecasting-with-python/

Not much on MC in python:
https://machinelearningmastery.com/monte-carlo-sampling-for-probability/

• Mark March 28, 2020 at 9:16 am #

I mean the standard way OLS works with statsmodels.predict() is to do a fixed forecast using the actual lagged dependent variables in the test data if there is a AR term in the equation. For a true out of sample forecast, for a given assumed exog data set or scenario, I want the forecast to be dynamic with regard to the AR term in the equation. Otherwise it looks better at out of sample forecasting than it really is.

• Jason Brownlee March 29, 2020 at 5:48 am #

I don’t follow, sorry. Perhaps you can rephrase your question/issue?

58. Vasiliki Voukelatou April 1, 2020 at 12:32 am #

Hi Mark amazing tutorial many thanks,

When using plot_acf(series, lags=30), I don’t see why the autocorrelation plot appears 2 times.
Is there a bug?

59. Arabzai April 17, 2020 at 7:29 pm #

Hi,
Can we fit Support vector regression instead of linear regression ?
If possible , then what is the procedure and how we can get the residuals from SVR-AR(1) model ?
Looking forward to hearing from you soon.
Best,
Arabzai

60. Olly Price April 19, 2020 at 7:49 am #

Hi, do you know what statistical method statsmodels.tsa.ar_model.AR( ) uses under the hood to determine the optimal order for the AR? (Can’t seem to find anything on the documentation.)

If not, are there any models you recommend me reading up on? I appreciate that you can observe an ACF and qualitatively decide a rough number.

61. Robert Gadzai May 17, 2020 at 2:25 am #

Thank you so much Jason, you saved my day

62. Lucas June 7, 2020 at 7:26 pm #

Hello,

I am trying to follow the precedure, but I am having problems with statsmodels. All my dependencies are upto date, but I cannot import statsmodels, raising the error:
ImportError: cannot import name ‘assert_equal’ from ‘statsmodels.compat.pandas’

Do you know what am I supposed to do?

Thanks

• Jason Brownlee June 8, 2020 at 6:09 am #

Perhaps confirm that pandas and statsmodels are up to date again?

My versions are:

63. Ashwin June 25, 2020 at 11:28 pm #

I wanted to ask is AR(lags=10) model is equal to an ARMA(10,0)

64. Emy June 30, 2020 at 12:46 am #

hi how can i know numbre off the correct lag?

• Jason Brownlee June 30, 2020 at 6:28 am #

If you’re unsure test a suite of values and use a number of lag obs that results in a model with the best performance.

65. krishnadas July 5, 2020 at 3:16 am #

Nice example. This one helped me to start my VAR model project. Here, I’m using multivariate time series and statsmodel’s VAR model. As you mentioned the API won’t update the coefficients for new observations. Since it’s a multivariate time series, what can I do get the prediction for a long time (say 6 months)?

• Jason Brownlee July 5, 2020 at 7:07 am #

I believe you can call forecast() and predict as long as you like.

Then re-fit the model as you get new data.

66. Dilip Thosar July 27, 2020 at 10:35 pm #

In most practical cases, we have some “regressable” variables in addition to the time series.
Variables that are not uniformly periodic (seasonal) and generally in our control. eg. In a retail store sales forecasting application, “gift promotion scheme on (Y/N)” or “scheme discount percentage offered (% or \$)” may be significantly affecting the output variable, sales. Just running a timeseries model will ignore the effects of the schemes. How are these situations to be handled?

• Jason Brownlee July 28, 2020 at 6:42 am #

They can be included as exogenous variables to a linear model.

67. Sada Narayanappa August 21, 2020 at 8:22 am #

When I run

from matplotlib import pyplot
from statsmodels.tsa.ar_model import AutoReg
from sklearn.metrics import mean_squared_error
from math import sqrt
# split dataset
X = series.values
train, test = X[1:len(X)-7], X[len(X)-7:]
# train autoregression
model = AutoReg(train, lags=29)

I get the following error

—————————————————————————
TypeError Traceback (most recent call last)
in
11 train, test = X[1:len(X)-7], X[len(X)-7:]
12 # train autoregression
—> 13 model = AutoReg(train, lags=29)
14 model_fit = model.fit()
15 print(‘Coefficients: %s’ % model_fit.params)

~/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ar_model.py in __init__(self, endog, lags, trend, seasonal, exog, hold_back, period, missing)
163 hold_back=None, period=None, missing=’none’):
164 super(AutoReg, self).__init__(endog, exog, None, None,
–> 165 missing=missing)
166 self._trend = string_like(trend, ‘trend’,
167 options=(‘n’, ‘c’, ‘t’, ‘ct’))

~/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/base/tsa_model.py in __init__(self, endog, exog, dates, freq, missing, **kwargs)
44 def __init__(self, endog, exog=None, dates=None, freq=None,
45 missing=’none’, **kwargs):
—> 46 super(TimeSeriesModel, self).__init__(endog, exog, missing=missing,
47 **kwargs)
48

TypeError: super(type, obj): obj must be an instance or subtype of type

68. Giuseppe October 14, 2020 at 1:24 am #

Hi Jason, thanks for the article.

I have a question, in the istruction AutoReg(train, lags=29) the lags parameter is equal to 29 because in the ACF graph we notice that there is a strong correlation up to 29 time?
Wouldn’t it be better to consider the PACF graph?

69. Muhammad Zubair December 6, 2020 at 3:53 am #

Hi,

Nice explanation but I want to clarify that the time lags t-1 refers to one lag of time and the current time you are referring to is t+1. Then why don’t you take it as t and t+1 or t-1 and t.

I know this is a very basic question but Appreciate your answer on it.

• Jason Brownlee December 6, 2020 at 7:10 am #

It is a good idea, I should do that.

70. Girish March 21, 2021 at 1:16 am #

Hi Jason,

Thanks for very nice article. I am trying to perform the following , could you please suggest/guide me.

My original data is like below

Month Price
2011-01-01 1405.07
2011-02-01 408.58
2011-03-01 277.75
2011-04-01 294.81
2011-05-01 511.77
2011-06-01 515.90

I have to predict the price (by regression) considering lag of 5, with the rebuilt the data set as below I need to predict the value for 5th element (X)

Dataset = [1405,408,277,294,X]
[408,277,294,511,X]
[277,294,511,515,X]

How I can achieve the same?

-Girish

71. Adarsh Krishna S May 6, 2021 at 11:09 pm #

hi,

I want a code for EEG signal prediction with autoregressive model , so what should be the changes in this temperature prediction .I am doing in jupyter notebook with MNE and Pandas.

• Jason Brownlee May 7, 2021 at 6:27 am #

Perhaps try developing an ARIMA or SARIMA model for your dataset and compare the results to other methods.

This might be a good place to start:
https://machinelearningmastery.com/start-here/#timeseries

• Adarsh May 7, 2021 at 3:56 pm #

Sir it’s my project. I want code for EAR model to do my project. So I was searching for AR model related to EEG and to modify AR to EAR but I am not getting code related to EEG .can u help me.

• Jason Brownlee May 8, 2021 at 6:32 am #

Sorry, I cannot write code for you.

Perhaps you can adapt an example on the blog for your specific dataset.

72. Adarsh Krishna S May 10, 2021 at 4:47 pm #

sir,
I tried with EEG dataset with your autoregressive code and the output is fully different from expected and predicted values . Any solution?

• Jason Brownlee May 11, 2021 at 6:39 am #

Perhaps try some data preparation?
Perhaps try an alternate configuration?
Perhaps try an alternate model?

73. Will Zhao May 23, 2021 at 7:52 am #

Hello Jason,

Do you know how to modify the model set up so that we can put constraints on the coefficient matrix A? For example, in VAR(1), Yt = (A1) (Yt-1) + E, A1= [a11, a12; a21, a22], how to impose a21 = 0 since I already know one granger cause the other, but not the other way around before running the model? I also want granger causality test to reflect that too. Do you know the solution?

Thanks

• Jason Brownlee May 24, 2021 at 5:39 am #

Not off hand, sorry. It may require custom code.

74. Shushman June 26, 2021 at 4:53 am #

Thanks so much for the useful primer! I found lines 18-30 of the final code chunk a bit more verbose than necessary, in part because history ends up being an array of 1-length arrays rather than a flat array.
I rewrote that chunk as follows to get the same output:

 history = train[len(train)-window:].flatten() predictions = list() for (t, obs) in enumerate(test.flatten()): hist_len = len(history) lag = np.concatenate(([1.0], np.flip(history[hist_len-window:]))) prediction = np.dot(coef, lag) predictions.append(prediction) history = np.append(history, obs) print('predicted=%f, expected=%f' % (prediction, obs)) 

• Jason Brownlee June 26, 2021 at 5:00 am #

You’re welcome.

Thanks for sharing!

75. Naser August 9, 2021 at 6:56 am #

Hi Jason,

Can we have scenario generations with this method? I mean several stochastic scenarios.

Thanks

• Jason Brownlee August 10, 2021 at 5:25 am #

Yes, there are many examples on the blog, you can use the search box.

76. Jason August 22, 2021 at 3:07 am #

Thank you for this. Really good explanation of the AR model given that the article is 4yrs old. I do have a question tho, I have beed using the ARIMA function on statsmodel, the lags and order the same? Meaning you are using the autocorrelations of the past 29 time series to predict the next value? So this comes out as an AR(29) model?

• Adrian Tam August 23, 2021 at 5:10 am #

If you’re referring to one of the example there, you’re correct.

77. Alfred Newerla September 7, 2021 at 6:01 am #

Thank you for this great tutorial.
Please either correct the filename in the code pieces to read the CSV file