The moving average (MA) method models the next step in the sequence as a linear function of the residual errors from a mean process at prior time steps. A moving average model is different from calculating the moving average of the time series.
So this recipe is a short example on what is MR modelling of time series. Let's get started.
import numpy as np import pandas as pd from statsmodels.tsa.arima_model import ARMA
Let's pause and look at these imports. Numpy and pandas are general ones. Here statsmodels.tsa.arima_model is used to import ARMA library for building of model.
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/a10.csv', parse_dates=['date']) df.head()
Here, we have used one time series data from github.
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 = ARMA(train_data.value, order=(0, 1)) model_fitted = model.fit()
We can use the ARMA class to create an MA model and setting a zeroth-order AR model. We must specify the order of the MA model in the order argument.
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.