The autocorrelation function (ACF) is the main method in time series analysis for quantifying the level of correlation between a series and its lags. This method is fairly similar (both mathematically and logically) to the Pearson correlation coefficient, which we saw earlier, and can be formalized with the following expression:
![](https://static.packt-cdn.com/products/9781788629157/graphics/assets/22868d8b-b944-4e1f-9ecf-ca232a1a275f.png)
Here, represents the ACF correlation coefficient of the series with its k lag; and n,
, and
denote the number of observations of the series, the t observation of the series, and the mean, respectively. The acf function from the stats package is R's built-in ACF, which, by default, visualizes the results using a bar plot. Let's use this function to plot the correlation of the USgas series and its first 60 lags (by setting the lag.max argument to 60):
acf(USgas, lag.max = 60)
We will get the following plot:
![](https://static.packt-cdn.com/products/9781788629157/graphics/assets/6f63967f-94c4-4347-a2ae-1d91c19dbf44.png)
Each...