\HeaderA{filter}{Linear Filtering on a Time Series}{filter}
\keyword{ts}{filter}
\begin{Description}\relax
Applies linear filtering to a univariate time series or to each series
separately of a multivariate time series.
\end{Description}
\begin{Usage}
\begin{verbatim}
filter(x, filter, method = c("convolution", "recursive"),
       sides = 2, circular = FALSE, init)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] a univariate or multivariate time series.
\item[\code{filter}] a vector of filter coefficients in reverse time order
(as for AR or MA coefficients).
\item[\code{method}] Either \code{"convolution"} or \code{"recursive"} (and
can be abbreviated). If \code{"convolution"} a moving average is
used: if \code{"recursive"} an autoregression is used.
\item[\code{sides}] for convolution filters only. If \code{sides=1} the
filter coefficients are for past values only; if \code{sides=2}
they are centred around lag 0. In this case the length of the
filter should be odd, but if it is even, more of the filter
is forward in time than backward.
\item[\code{circular}] for convolution filters only.  If \code{TRUE}, wrap
the filter around the ends of the series, otherwise assume
external values are missing (\code{NA}).
\item[\code{init}] for recursive filters only. Specifies the initial values
of the time series just prior to the start value, in reverse
time order. The default is a set of zeros.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
Missing values are allowed in \code{x} but not in \code{filter}
(where they would lead to missing values everywhere in the output).

Note that there is an implied coefficient 1 at lag 0 in the
recursive filter, which gives
\deqn{y_i = x_i + f_1y_{i-1} + \cdots + f_py_{i-p}}{\code{y[i] =
      x[i] + f[1]*y[i-1] + ... + f[p]*y[i-p]}}
No check is made to see if recursive filter is invertible:
the output may diverge if it is not.

The convolution filter is
\deqn{y_i = f_1x_{i+o} + \cdots + f_px_{i+o-p-1}}{\code{y[i] = f[1]*x[i+o] + ... + f[p]*x[i+o-p-1]}}

where \code{o} is the offset: see \code{sides} for how it is determined.
\end{Details}
\begin{Value}
A time series object.
\end{Value}
\begin{Note}\relax
\code{\LinkA{convolve}{convolve}(, type="filter")} uses the FFT for computations
and so \emph{may} be faster for long filters on univariate series,
but it does not return a time series (and so the  time alignment is
unclear), nor does it handle missing values.  \code{filter} is
faster for a filter of length 100 on a series of length 1000,
for example.
\end{Note}
\begin{SeeAlso}\relax
\code{\LinkA{convolve}{convolve}}, \code{\LinkA{arima.sim}{arima.sim}}
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
x <- 1:100
filter(x, rep(1, 3))
filter(x, rep(1, 3), sides = 1)
filter(x, rep(1, 3), sides = 1, circular = TRUE)

filter(presidents, rep(1,3))
\end{ExampleCode}
\end{Examples}

