Estimate and Plot the Autocorrelation Function (ACF) in R

You need to estimate and plot either the autocorrelation function (also known as a correlogram) or else the autocovariance function for time series data.

Step-by-step tutorial

Use the acf function from the stats package to estimate and plot the autocorrelation and autocovariance functions in R.

First, let's load some hygrometer data into a data frame, and plot it.

> hygrometer.df <- read.csv('hygrometer.csv')
Interval Humidity.Average Temperature.Average
1 11/12/20 1:00         55.50117            21.93301
2 11/12/20 1:12         57.07868            19.70803
3 11/12/20 1:24         56.20748            19.85207
4 11/12/20 1:36         56.12080            19.87645
5 11/12/20 1:48         57.92270            19.32857
6 11/12/20 2:00         57.70622            19.19605
> temp <- hygrometer.df[, 'Temperature.Average']
> plot(temp, type='l')

This time series has an obvious anomalous spike around index 100. This happened when I put the hygrometer in sleep mode by mistake. So let's subset the time series before we continue. The anomaly ends at index 109, so we'll remove elements 1-109 from the vector:

> temp.clean <- temp[-(1:109)]
> plot(temp.clean, type='l')

Looks good, so let's estimate and plot both the autocorrelation and autocovariance functions.

Autocorrelation function

First, let's estimate and plot the autocorrelation function. The plot is also known as a correlogram.

> acf(temp.clean)

Use the lag.max parameter to check for autocorrelation at larger lags. Since the data interval is 12 minutes and temperature data has a daily cycle, we expect to see peaks at multiples of 5 · 24 = 120. Set the lag.max parameter to 6 · 24 to check:

> acf(temp.clean, lag.max=6*24)

As expected, there's a peak at 120 lags.

Autocovariance function

You can also estimate and plot the autocovariance function:

> acf(temp.clean, type='covariance', lag.max=6*24)