mFilter: Decomposition of a time series into trend and cyclical...

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/mFilter.R

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

1
2
3
4
5
  mFilter(x, ...)
  ## Default S3 method:
mFilter(x, ...)
  ## S3 method for class '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.

Details

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

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

Author(s)

Mehmet Balcilar, mbalcilar@yahoo.com

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
## 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)

muschellij2/mFilter documentation built on May 17, 2019, 7:45 p.m.