Autoregressive models are time-series models used to predict future incidents. The following formula shows this:
In the preceding formula, c is a constant and the last term is a random component, also known as white noise.
Let's build the autoregression model using the statsmodels.tsa subpackage:
- Import the libraries and read the dataset:
# import needful libraries
from statsmodels.tsa.ar_model import AR
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
import statsmodels.api as sm
from math import sqrt
# Read the dataset
data = sm.datasets.sunspots.load_pandas().data
- Split the Sunspot data into train and test sets:
# Split data into train and test set
train_ratio=0.8
train=data[:int(train_ratio*len(data))]
test=data[int(train_ratio*len(data)):]
- Train and fit the autoregressive model:
# AutoRegression Model training
ar_model = AR(train...