Apply a Filter to a Time Series

DESCRIPTION:

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

USAGE:

filter(x, filter, method="convolution", sides=2, circular=F,
       init=<<see below>>)

REQUIRED ARGUMENTS:

x
a univariate or multivariate regular time series, or a vector, or a matrix with each column representing a univariate component of a multivariate series. Missing values are allowed only at the ends of series. Factor data are not allowed.
filter
a vector of filter coefficients -- the first element is for the last timepoint used and the last element of filter is for the earliest timepoint used. The same filter coefficients are applied to each univariate component of the time series, i.e., to each column of x.

OPTIONAL ARGUMENTS:

method
either the string "convolution" or "recursive" (only the start of a 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
for convolution filters only: if TRUE, wrap the filter around the ends of the series. If FALSE, do not wrap and set any undefined elements of the filtered series to NA.
init
for recursive filters only: 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 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 a matrix of zeros. A typical init might be the mean of each input series repeated to the length of the filter, i.e.,

init=matrix(rep(apply(x,2,mean),length(filter)),nrow=length(filter),byrow=T).

VALUE:

a regular time series with the same dimensions and time parameters as x containing the filtered series.

DETAILS:

Calendar time series are coerced to regular ( rts) time series. Filtered vectors and matrices are returned as regular time series with start , delat and frequency of 1.

REFERENCES:

The chapters "Creating and Viewing Time Series" and "Analyzing Time Series" of the S-PLUS Guide to Statistics.

SEE ALSO:

, , .

EXAMPLES:

series <- rts(rt(100, 5), start=c(1974,1), frequency=12)
filt <- exp(-((-10:10)^2)/5)    # gaussian lowpass filter
filt <- filt/sum(filt)
ts.plot(series, filter(series, 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)

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(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))