

   filter {ts}                                  R Documentation

   LLiinneeaarr FFiilltteerriinngg oonn aa TTiimmee SSeerriieess

   DDeessccrriippttiioonn::

        Applies linear filtering to a univariate time series or
        to each series separately of a multivariate time
        series.

   UUssaaggee::

        filter(x, filter, method="convolution", sides=2,
              circular=FALSE, init)

   AArrgguummeennttss::

          x: a univariate or multivariate time series.

     filter: a vector of filter coefficients in reverse time
             order (as for AR or MA coefficients).

     method: Either `"convolution"' or `"recursive"' (and can
             be abbreviated). If `"convolution"' a moving aver-
             age is used: if `"recursive"' an autoregression is
             used.

      sides: for convolution filters only. If `sides=1' the
             filter coefficients are for past values only; if
             `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.

   circular: for convolution filters only.  If `TRUE', wrap the
             filter around the ends of the series, otherwise
             assume external values are missing (`NA').

       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.

   DDeettaaiillss::

        Missing values are allowed in `x' but not in `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

             `y[i] =
                  x[i] + f[1]*y[i-1] + ... + f[p]*y[i-p]'

        No check is made to see if recursive filter is invert-
        ible: the output may diverge if it is not.

        The convolution filter is

             `y[i] = f[1]*x[i+o] + ... + f[p]*x[i+o-p-1]'

        where `o' is the offset: see `sides' for how it is
        determined.

   VVaalluuee::

        A time series object.

   NNoottee::

        `convolve(, type="filter")' uses the FFT for computa-
        tions and so may be faster for long filters on univari-
        ate series, but it does not return a time series (and
        so the  time alignment is unclear), nor does it handle
        missing values.  `filter' is faster for a filter of
        length 100 on a series of length 1000, for example.

   AAuutthhoorr((ss))::

        B.D. Ripley

   SSeeee AAllssoo::

        `convolve'

   EExxaammpplleess::

        x <- 1:100
        filter(x, rep(1,3))
        filter(x, rep(1,3), sides=1)
        filter(x, rep(1,3), sides=1, circular=T)
        data(presidents)
        filter(presidents, rep(1,3))

        ## A simple simulation function for ARMA processes
        arma.sim <- function(n, ar = NULL, ma = NULL, sigma = 1.0)
        {
            x <- ts(rnorm(n+100, 0, sigma), start = -99)
            if(length(ma)) x <- filter(x, c(1, ma), sides=1)
            if(length(ar)) x <- filter(x, ar, method="recursive")
            as.ts(x[-(1:100)])
        }
        arma.sim(63, c(0.8897,-0.4858), c(-0.2279, 0.2488), sigma=sqrt(0.1796))

