Autoregression modeling is a modeling technique used for time series data that assumes linear continuation of the series so that previous values in the time series can be used to predict futures values. It includes the idea of 'lag variables'.
So this recipe is a short example on what is AR modelling of time series. Let's get started.
import numpy as np import pandas as pd from statsmodels.tsa.ar_model import AR
Let's pause and look at these imports. Numpy and pandas are general ones. Here statsmodels.tsa.ar_model is used to import autorregressive library for building of model.
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/a10.csv', parse_dates=['date']) df['value'] = np.log(df['value']) df['value'] = df['value'].diff() df = df.drop(df.index) df.head()
Here, we have used one time series data from github. Now, since this data is progressing and, we have normalized the set and taken difference so as to have a stationary series.
Now our dataset is ready.
train_data = df[1:len(df)-12] test_data = df[len(df)-12:]
Here we have simply split data into size of 12 and rest elements
model = AR(train_data.value) model_fitted = model.fit()
We have simply build an AR model on our dataset and fit it.
print('coefficients',model_fitted.params) predictions = model_fitted.predict(start=len(train_data), end=len(train_data) + len(test_data)-1) print(predictions)
Here, we have printed the coeffiecient of model and the predicted values.
Once we run the above code snippet, we will see:
Scroll down the ipython file to visualize the output.