The autocorrelation function measures the correlations between an observation and its previous lag in a time series model. These functions are often used to determine which time series model to use. Based on the ACF graph, we usually see familiar patterns that allows us to select models or to rule out other models. In this article, we will learn how to create an ACF plot in R.
Let's load a data set of monthly milk production. We will load it from the url below. The data consists of monthly intervals and kilograms of milk produced.
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/ourcodingclub/CC-time-series/master/monthly_milk.csv') df.head()
df.month = pd.to_datetime(df.month)
df = df.set_index('month')
We can plot the acf using the
plot_acf method from the
statsmodels package. In fact, that package as many different time series tools. Here is an example of plotting our milk with the last 40 lags.
from statsmodels.graphics.tsaplots import plot_acf plot_acf(df.milk_prod_per_cow_kg, lags = 40)