filter
Apply a Filter to a Time Series

### Description

Returns a time series that is the filtered version of x.

### Usage

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

### Arguments

x | a univariate or multivariate time series, a vector, or a matrix with each column representing a univariate component of a multivariate series.
filter | a vector of filter coefficients. The first element specifies the last timepoint used. The last element specifies the earliest timepoint used. The same filter coefficients are applied to each univariate component of the time series; that is, to each column of x. Missing values (NAs) are not allowed. The length for filter cannot be longer than the length of time series (nrow(x)) for a convolution filter.
method | either the string "convolution" or "recursive". Only the beginning of the string is necessary.
sides | for convolution filters only. If sides=2, the filter coefficients are centered at lag 0 (so there should be an odd number of coefficients). If sides=1, the first coefficient is the lag 0 coefficient, so the filtered series depends only on past and present values of the original series.
circular | a logical value for convolution filters only. If TRUE, wraps the filter around the ends of the series. If FALSE (the default), do not wrap and set any undefined elements of the filtered series to NA.
init | for recursive filters only. A matrix of the values of the filtered series just prior to the beginning of the input time series. Must have NROW(init)=length(filter) and NCOL(init) equal to 1 or the number of univariate time series in x (NCOL(x)). The first row of init refers to the values of the series one time step back from the start of x. The default is NULL. A typical init might be the mean of each input series repeated to the length of the filter. For example: init=matrix(rep(apply(x,2,mean),length(filter)),nrow=length(filter),byrow=TRUE).

### Details

Any object x is coerced to a (as.ts) time series. Filtered vectors and matrices are returned as time series with time parameters.
Value
returns a time series with the same dimensions and time parameters as x containing the filtered series. It could have the class name c("mts", "ts") (if ncol(x) > 1) or "ts".
References
The chapters "Creating and Viewing Time Series" and "Analyzing Time Series" of the S-PLUS Guide to Statistics.
Differences between Spotfire Enterprise Runtime for R and Open-source R
NA handling for recursive filtering differs between Spotfire Enterprise Runtime for R and open-source R. In Spotfire Enterprise Runtime for R, all values after an NA is encountered in x become NA.
See Also
acm.filt, arima.sim, demod
Examples

    series <- ts(rt(100, 5), start=c(1974,1), frequency=12)
    filt <- exp(-((-10:10)^2)/5)	# gaussian lowpass filter
    filt <- filt/sum(filt)
    # f2series[t] = .3*series[t+1] + .6*series[t] + .1*series[t-1]
    f2series <- filter(series, c(.3, .6, .1))
    # f3series[t] = .3*series[t] + .6*series[t-1] + .1*series[t-2]
    f3series <- filter(series, c(.3, .6, .1), sides=1)
    # f4series[t] = .3*series[t] + .6*series[t-1] + .1*series[t-2]
    f4series <- filter(series, c(.3, .6, .1), sides = 1,  circular = TRUE)
    x <- rnorm(200)
    filtx <- c(.4, .1)
    # fx[t] = .4*fx[t-1] + .1*fx[t-2] + x[t]:
    fx <- filter(x, filtx, "rec")
    # A function to do exponential smoothing:
    exponential.smooth <- function(x, lambda)
    {
         if(length(lambda) > 1)
              stop("lambda must be a single number")
         if(lambda > 1 || lambda <= 0)
              stop("lambda must be between zero and one")
         xlam <- x * lambda
         xlam[1] <- x[1]
         filter(xlam, filter = 1 - lambda, method = "rec")
    }
    exponential.smooth(Sdatsets::lynx, .47)
    # Generate the Fibonacci Sequence
    # (30 terms after the initial 1, 1):
    filter(rep(0, 30), c(1, 1), method="rec", init=c(1, 1))
    # NAs and/or factor example for time series.
    x.fac <- factor(c(1:10, NA, 2:5))
    filter(x.fac, c(0.4, 0.1))

Package stats version 6.1.5-18
Package Index