In R, you can calculate the autocorrelation using the acf()
function from the tseries
package.
Autocorrelation measures the degree of similarity between a time series and a lagged version of itself over successive time intervals.
In this article, we will explore how to calculate autocorrelation using the acf()
function.
Calculate Autocorrelation Using acf() Function in R
To calculate autocorrelation, we need to load the tseries
package and use the acf()
function. Here is the syntax:
library(tseries)
acf(data)
Let’s see how we can calculate autocorrelation for a numeric vector in R:
# Load library
library(tseries)
# Define data
Pressure <- c(12.39, 11.25, 12.15, 13.48, 13.78, 12.89, 12.21, 12.58)
# Find autocorrelation
acf(Pressure, pl=FALSE)
Output: 👇️
Autocorrelations of series ‘Pressure’, by lag
0 1 2 3 4 5 6 7
1.000 0.396 -0.407 -0.505 -0.108 0.102 0.021 0.001
As shown in the output, the autocorrelation at lag 0 is 1, and the autocorrelation at lag 1 is 0.396.