Autoregressive model (AR)
What is an autoregression?
What we need for using autoregressive model?
Main assumption, which autoregressive model uses - that current value of time series depends on(correlates with) it's previous values(this is called autocorrelation).
Another important assumption - is that time series is stationary (simply speaking, it means that the mean value and variance of time series is constant). The only possible exception - having trend from a bias coefficient.
Defining model
Formally, when we are talking about autoregression, we should mention such important parameter of this model as p
- model’s order, or, practically - number of previous values, used for prediction. This class of models is denoted as AR(p)
.
For example, in case of p=1
, we work with model AR(1)
, which is represented by the next expression:
Here y_t
- value of time series at moment of time t
, a_1
- coefficient for a previous value(or, alternatively, the first lagged value), ε_t
- random error (practically - we don’t use it for making predictions), c
- bias coefficient.
Analogically, in the second case, we work with AR(2)
model, which is represented by the expression:
Here notation is pretty much the same to the previous example, except that now we additionally have a_2
coefficient, corresponding to the second lagged value.
And so on.
Note also, that we also could find model without using constant trend (practically - assume that c = 0
). But in this case, our time series should be stationary, so - we can use one of the tests, described into previous chapter, in order to check this.
Code implementation
To build autoregressive model we can use the next code:
# Importing necessary class `AutoReg`
from statsmodels.tsa.ar_model import AutoReg
# Creating instance of AR(7) model called `model` and training it using instance of Series with time series values called `data`
model = AutoReg(data, lags = 7)
res = model.fit()
Here we are setting parameter lags
equal to 7
, meaning, that we want to use previous seven values for prediction.
As alternative, we can set lags
equal to list of integers, where each value is a number of corresponding lag, which will be used for prediction.
For example, here is an alternative for the previous code:
# Importing necessary class `AutoReg`
from statsmodels.tsa.ar_model import AutoReg
# Creating instance of AR(7) model called `model` and training it using instance of Series with time series values called `data`
model = AutoReg(data, lags = [1, 2, 3, 4, 5, 6, 7])
res = model.fit()
Also, we can skip several lags in our model:
For example, in the next code we are using only the first, and the third lagged values:
# Importing necessary class `AutoReg`
from statsmodels.tsa.ar_model import AutoReg
# Creating instance of AR(3) with the second lag skipped using Series with time series values called `data`
model = AutoReg(data, lags = [1, 3])
res = model.fit()
If we then want to make prediction of future values, we can use the following code, additionally to the specified above:
# Making forecast on the next 14 time steps
forecast = res.forecast(steps = 14)
Also, if we want to get the predicted values for training data, we can get them, using fittedvalues
attribute:
# Creating Series of corresponding values called `pred`
pred = res.fittedvalues
If you want to see some practical application of AR model - watch the following video:
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Still meg spørsmål om dette emnet
Oppsummer dette kapittelet
Vis eksempler fra virkeligheten
Awesome!
Completion rate improved to 7.14
Autoregressive model (AR)
Sveip for å vise menyen
What is an autoregression?
What we need for using autoregressive model?
Main assumption, which autoregressive model uses - that current value of time series depends on(correlates with) it's previous values(this is called autocorrelation).
Another important assumption - is that time series is stationary (simply speaking, it means that the mean value and variance of time series is constant). The only possible exception - having trend from a bias coefficient.
Defining model
Formally, when we are talking about autoregression, we should mention such important parameter of this model as p
- model’s order, or, practically - number of previous values, used for prediction. This class of models is denoted as AR(p)
.
For example, in case of p=1
, we work with model AR(1)
, which is represented by the next expression:
Here y_t
- value of time series at moment of time t
, a_1
- coefficient for a previous value(or, alternatively, the first lagged value), ε_t
- random error (practically - we don’t use it for making predictions), c
- bias coefficient.
Analogically, in the second case, we work with AR(2)
model, which is represented by the expression:
Here notation is pretty much the same to the previous example, except that now we additionally have a_2
coefficient, corresponding to the second lagged value.
And so on.
Note also, that we also could find model without using constant trend (practically - assume that c = 0
). But in this case, our time series should be stationary, so - we can use one of the tests, described into previous chapter, in order to check this.
Code implementation
To build autoregressive model we can use the next code:
# Importing necessary class `AutoReg`
from statsmodels.tsa.ar_model import AutoReg
# Creating instance of AR(7) model called `model` and training it using instance of Series with time series values called `data`
model = AutoReg(data, lags = 7)
res = model.fit()
Here we are setting parameter lags
equal to 7
, meaning, that we want to use previous seven values for prediction.
As alternative, we can set lags
equal to list of integers, where each value is a number of corresponding lag, which will be used for prediction.
For example, here is an alternative for the previous code:
# Importing necessary class `AutoReg`
from statsmodels.tsa.ar_model import AutoReg
# Creating instance of AR(7) model called `model` and training it using instance of Series with time series values called `data`
model = AutoReg(data, lags = [1, 2, 3, 4, 5, 6, 7])
res = model.fit()
Also, we can skip several lags in our model:
For example, in the next code we are using only the first, and the third lagged values:
# Importing necessary class `AutoReg`
from statsmodels.tsa.ar_model import AutoReg
# Creating instance of AR(3) with the second lag skipped using Series with time series values called `data`
model = AutoReg(data, lags = [1, 3])
res = model.fit()
If we then want to make prediction of future values, we can use the following code, additionally to the specified above:
# Making forecast on the next 14 time steps
forecast = res.forecast(steps = 14)
Also, if we want to get the predicted values for training data, we can get them, using fittedvalues
attribute:
# Creating Series of corresponding values called `pred`
pred = res.fittedvalues
If you want to see some practical application of AR model - watch the following video:
Takk for tilbakemeldingene dine!