How can I calculate autocorrelation in R?

Autocorrelation is a statistical method used to measure the correlation between a time series data and its lagged values. This is a useful tool in analyzing and understanding the patterns and trends in a dataset. In order to calculate autocorrelation in R, there are a few steps that need to be followed. First, the time series data needs to be imported into R. Then, the “acf” function can be used to calculate the autocorrelation values. This function generates a plot and a numerical value known as the autocorrelation coefficient, which indicates the strength and direction of the correlation. Additionally, the “acf” function also allows for the adjustment of parameters such as lag and type of correlation. Overall, calculating autocorrelation in R is a straightforward process that can provide valuable insights into a dataset.

Calculate Autocorrelation in R


Autocorrelation measures the degree of similarity between a time series and a lagged version of itself over successive time intervals.

It’s also sometimes referred to as “serial correlation” or “lagged correlation” since it measures the relationship between a variable’s current values and its historical values.

When the autocorrelation in a time series is high, it becomes easy to predict future values by simply referring to past values.

How to Calculate Autocorrelation in R

Suppose we have the following time series in R  that shows the value of a certain variable during 15 different time periods:

#define data
x <- c(22, 24, 25, 25, 28, 29, 34, 37, 40, 44, 51, 48, 47, 50, 51)

We can calculate the autocorrelation for every lag in the time series by using the acf() function from the tseries library:

library(tseries)#calculate autocorrelations
acf(x, pl=FALSE)

     0      1      2      3      4      5      6      7      8      9     10 
 1.000  0.832  0.656  0.491  0.279  0.031 -0.165 -0.304 -0.401 -0.458 -0.450 
    11 
-0.369 

The way to interpret the output is as follows:

  • The autocorrelation at lag 0 is 1.
  • The autocorrelation at lag 1 is 0.832.
  • The autocorrelation at lag 2 is 0.656.
  • The autocorrelation at lag 3 is 0.491.

And so on.

We can also specify the number of lags to display with the lag argument:

#calculate autocorrelations up to lag=5
acf(x, lag=5, pl=FALSE)

Autocorrelations of series 'x', by lag

    0     1     2     3     4     5 
1.000 0.832 0.656 0.491 0.279 0.031 

How to Plot the Autocorrelation Function in R

We can plot the autocorrelation function for a time series in R by simply not using the pl=FALSE argument:

#plot autocorrelation function
acf(x)

Autocorrelation in R

You can also specify a different title for the plot by using the main argument:

#plot autocorrelation function with custom title
acf(x, main='Autocorrelation by Lag')

Autocorrelation plot in R

Additional Resources

How to Calculate Autocorrelation in Python
How to Calculate Autocorrelation in Excel

x