x
.
filter(x, filter, method="convolution", sides=2, circular=F, init=<<see below>>)
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
.
"convolution"
or
"recursive"
(only the start of a string is necessary).
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.
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
.
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)
.
x
containing the filtered series.
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
.
The chapters "Creating and Viewing Time Series" and "Analyzing Time Series" of the S-PLUS Guide to Statistics.
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))