Learn R Programming

stats (version 3.6.2)

filter: Linear Filtering on a Time Series

Description

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

Usage

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

Arguments

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 average 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.

Value

A time series object.

Details

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_1y_{i-1} + \cdots + f_py_{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 $$y_i = f_1x_{i+o} + \cdots + f_px_{i+o-(p-1)}$$

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

See Also

convolve, arima.sim

Examples

Run this code
# NOT RUN {
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))
# }

Run the code above in your browser using DataLab