Next: autocov() Up: Time Series Macros Help Previous: arspectrum()   Contents


autocor(y [, nlags [, nfreq]] [,full:T] [center:T] [,degree:d]), y a REAL
  vector or matrix, nlags > 0 and nfreq > 0 integers, d an integer
  scalar or vector

Keywords: time domain, autocorrelation
r_y <- autocor(y, nlags) computes sample autocorrelations acf(h,y) (y a
REAL vector) or acf(h,y[,j]) (y a REAL matrix) for lags h = 0, 1, ...,

The columns of y, which may have no MISSING elements, are interpreted as
time series defined at equally spaced time points.

For a vector y of length N, the sample autocorrelations are defined as
   acf(h,y) = acvf(h,y)/acvf(0,y),
   acvf(h,y) = sum((y[i] - ybar)*(y[i+h] - ybar),i=1,...,N-h)/N
is the sample autocovariance.  ybar = sum(y)/N is the sample mean of y.

When ny = ncols(y) = 1, r_y is a vector of length L = nlags + 1 with
r_y[k] = acf(k-1,y).  When ny > 1, r_y is a L by ny matrix with
r_y[k,j] = acf(k-1,y[,j]).

autocor(y) is the same as autocor(y, nrows(y) - 1), computing all
non-degenerate sample autocorrelations.

You can specify that columns of y be detrended by subtracting a
polynomial in time using keyword phrase 'degree:d', where d is an
integer scalar or vector of length ny = ncols(y).  A scalar d is
interpreted as rep(d,ny).

r_y <- autocor(y [,nlags] degree:d) computes autocorrelations of the
residuals from polynomial trends fit by least squares to each column of
y.  The values of the fitted polynomial replace ybar in the definition
of acvf().

d[j] is the degree of the polynomial fit to column j.  When d[j] = 0,
only the sample mean is subtracted (default).  When d[j] < 0, nothing is

You can modify the behavior of autocor() using keyword phrases 'full:T'
(compute acf for negative lags, with acf(-h,y) = acf(h,y) in row
2*nlags+2-h), and 'center:T' (same, but with acf(0,y) in row nlags+1).
See crosscor() for details.

Autocovariances acvf(h,y) are computed using crosscov() which makes use
of discrete Fourier transforms (DFTs) of length goodfactors(N+nlags),
the smallest integer >= N + nlags which has no prime factors > 29.  See
topic 'fourier'.

autocor(y, nlags, M) where M is an integer, does the same, using DFTs of
length M.  It is an error if M < N + nlags or if M has a prime factor >
29.  See topic 'fourier'.

You can use macro tsplot() to plot the computed autocorrelations.  Any
of the following might be appropriate to plot the first 50
autocorrelations of the columns of y.

  Cmd> tsplot(autocor(y, 50), 1) # line plot
  Cmd> tsplot(autocor(y, 50), 1, impulse:T) # impulse plot
  Cmd> tsplot(autocor(y, 50), 1, impulse:T, lines:T) # line and impulse
  Cmd> tsplot(autocor(y, 50), 1, char:charVec)#plot lines & symbols

On each of these it would usually be appropriate to include keyword
phrases ymin:-1 and ymax:1 since autocorrelations are between -1 and 1.

See also autocov(), crosscov(), crosscor(), tsplot().

Gary Oehlert 2003-01-15