Calculating autocorrelations
Autocorrelations or lagged correlations are used to assess whether a time series is dependent on its past. For a time series x
of length n
we consider the n-1
pairs of observations one time unit apart. The first such pair is (x[2],x[1])
, and the next is (x[3],x[2])
. Each such pair is of the form (x[t],x[t-1])
where t is the observation index, which we vary from 2 to n in this case. The lag-1 autocorrelation of x
can be estimated as the sample correlation of these (x[t], x[t-1])
pairs.
In general, we can manually create these pairs of observations. First, create two vectors, x_t0
and x_t1
, each with length n-1
, such that the rows correspond to (x[t], x[t-1])
pairs. Then apply the cor()
function to estimate the lag-1 autocorrelation.
Luckily, the acf()
command provides a shortcut. Applying acf(..., lag.max = 1, plot = FALSE)
to a series x
automatically calculates the lag-1 autocorrelation.
Finally, note that the two estimates differ slightly as they use slightly different scalings in their calculation of sample covariance, 1/(n-1)
versus 1/n
. Although the latter would provide a biased estimate, it is preferred in time series analysis, and the resulting autocorrelation estimates only differ by a factor of (n-1)/n
.
In this exercise, you'll practice both the manual and automatic calculation of a lag-1 autocorrelation. The time series x
and its length n
(150) have already been loaded. The series is shown in the plot on the right.
This exercise is part of the course
Time Series Analysis in R
Exercise instructions
- Create two vectors,
x_t0
andx_t1
, each with lengthn-1
such that the rows correspond to the(x[t], x[t-1])
pairs. - Confirm that
x_t0
andx_t1
are(x[t], x[t-1])
pairs using the pre-written code. - Use
plot()
to view the scatterplot ofx_t0
andx_t1
. - Use
cor()
to view the correlation betweenx_t0
andx_t1
. - Use
acf()
withx
to automatically calculate the lag-1 autocorrelation. Set thelag.max
argument to1
to produce a single lag period and set theplot
argument toFALSE
. - Confirm that the difference factor is
(n-1)/n
using the pre-written code.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define x_t0 as x[-1]
x_t0 <-
# Define x_t1 as x[-n]
x_t1 <-
# Confirm that x_t0 and x_t1 are (x[t], x[t-1]) pairs
head(cbind(x_t0, x_t1))
# Plot x_t0 and x_t1
plot(___, ___)
# View the correlation between x_t0 and x_t1
cor(___, ___)
# Use acf with x
acf(___, lag.max = ___, plot = ___)
# Confirm that difference factor is (n-1)/n
cor(x_t1, x_t0) * (n-1)/n