Learn R Programming

mFilter (version 0.1-5)

mFilter: Decomposition of a time series into trend and cyclical components using various filters

Description

mFilter is a generic function for filtering time series data. The function invokes particular filters which depend on filter type specified via its argument filter. The filters implemented in the package mFilter package are useful for smoothing, and estimating tend and cyclical components. Some of these filters are commonly used in economics and finance for estimating cyclical component of time series.

The mFilter currently applies only to time series objects. However a default method is available and should work for any numeric or vector object.

Usage

mFilter(x, …)
  # S3 method for default
mFilter(x, …)
  # S3 method for ts
mFilter(x, filter=c("HP","BK","CF","BW","TR"), …)

Arguments

x

a regular a time series.

filter

filter type, the filter types are "HP" (Hodrick-Prescott), "BK" (Baxter-King), "CF" (Christiano-Fitzgerald), "BW" (Butterworth), and "TR" (trigonometric regression).

...

Additional arguments to pass to the relevant filter functions. These are passed to hpfilter, bkfilter, cffilter, bwfilter, and trfilter, respectively for the "HP", "BK", "CF", "BW", and "TR" filters.

Value

An object of class "mFilter".

The function summary is used to obtain and print a summary of the results, while the function plot produces a plot of the original series, the trend, and the cyclical components. The function print is also available for displaying estimation results.

The generic accessor functions fitted and residuals extract estimated trend and cyclical componets of an "mFilter" object, respectively.

An object of class "mFilter" is a list containing at least the following elements:

cycle

Estimated cyclical (irregular) component of the series.

trend

Estimated trend (smooth) component of the series.

fmatrix

The filter matrix applied to original series.

method

The method, if available, for the filter type applied.

type

The filter type applied to the series.

call

Call to the function.

title

The title for displaying results.

xname

Name of the series passed to mFilter for filtering.

x

The original or drift adjusted, if drift=TRUE, time series passed to the mFilter.

Following additional elements may exists depending on the type of filter applied:

nfix

Length or order of the fixed length filters.

pl

Minimum period of oscillation of desired component (2<=pl).

pu

Maximum period of oscillation of desired component (2<=pl<pu<infinity).

lambda

Lambda (smoothness) parameter of the HP filter.

root

Whether time series has a unit root, TRUE or FALSE (default).

drift

Whether time series has drift, TRUE or FALSE (default).

theta

MA coefficients for time series model, used in "CF" filter.

Details

The default behaviour is to apply the default filter to ts objects.

See Also

Other functions which return objects of class "mFilter" are bkfilter, bwfilter, cffilter, bkfilter, trfilter. Following functions apply the relevant methods to an object of the "mFilter" class: print.mFilter, summary.mFilter, plot.mFilter, fitted.mFilter, residuals.mFilter.

Examples

Run this code
# NOT RUN {
## library(mFilter)

data(unemp)

opar <- par(no.readonly=TRUE)

unemp.hp <- mFilter(unemp,filter="HP")  # Hodrick-Prescott filter
print(unemp.hp)
summary(unemp.hp)
residuals(unemp.hp)
fitted(unemp.hp)
plot(unemp.hp)

unemp.bk <- mFilter(unemp,filter="BK")  # Baxter-King filter
unemp.cf <- mFilter(unemp,filter="CF")  # Christiano-Fitzgerald filter
unemp.bw <- mFilter(unemp,filter="BW")  # Butterworth filter
unemp.tr <- mFilter(unemp,filter="TR")  # Trigonometric regression filter

par(mfrow=c(2,1),mar=c(3,3,2,1),cex=.8)
plot(unemp,main="Unemployment Series & Estimated Trend", col=1, ylab="")
lines(unemp.hp$trend,col=2)
lines(unemp.bk$trend,col=3)
lines(unemp.cf$trend,col=4)
lines(unemp.bw$trend,col=5)
lines(unemp.tr$trend,col=6)

legend("topleft",legend=c("series", "HP","BK","CF","BW","TR"),
    col=1:6,lty=rep(1,6),ncol=2)

plot(unemp.hp$cycle,main="Estimated Cyclical Component",
     ylim=c(-2,2.5),col=2,ylab="")
lines(unemp.bk$cycle,col=3)
lines(unemp.cf$cycle,col=4)
lines(unemp.bw$cycle,col=5)
lines(unemp.tr$cycle,col=6)
## legend("topleft",legend=c("HP","BK","CF","BW","TR"),
## col=2:6,lty=rep(1,5),ncol=2)

unemp.cf1 <- mFilter(unemp,filter="CF", drift=TRUE, root=TRUE)
unemp.cf2 <- mFilter(unemp,filter="CF", pl=8,pu=40,drift=TRUE, root=TRUE)
unemp.cf3 <- mFilter(unemp,filter="CF", pl=2,pu=60,drift=TRUE, root=TRUE)
unemp.cf4 <- mFilter(unemp,filter="CF", pl=2,pu=40,drift=TRUE,
             root=TRUE,theta=c(.1,.4))

plot(unemp,
main="Christiano-Fitzgerald filter of unemployment: Trend \n root=TRUE,drift=TRUE",
      col=1, ylab="")
lines(unemp.cf1$trend,col=2)
lines(unemp.cf2$trend,col=3)
lines(unemp.cf3$trend,col=4)
lines(unemp.cf4$trend,col=5)
legend("topleft",legend=c("series", "pl=2, pu=32", "pl=8, pu=40",
"pl=2, pu=60", "pl=2, pu=40, theta=.1,.4"), col=1:5, lty=rep(1,5), ncol=1)

plot(unemp.cf1$cycle,
main="Christiano-Fitzgerald filter of unemployment: Cycle \n root=TRUE,drift=TRUE",
     col=2, ylab="", ylim=range(unemp.cf3$cycle))
lines(unemp.cf2$cycle,col=3)
lines(unemp.cf3$cycle,col=4)
lines(unemp.cf4$cycle,col=5)
## legend("topleft",legend=c("pl=2, pu=32", "pl=8, pu=40", "pl=2, pu=60",
## "pl=2, pu=40, theta=.1,.4"), col=2:5, lty=rep(1,4), ncol=2)

par(opar)
# }

Run the code above in your browser using DataLab