yth_filter: Filtered xts object

View source: R/yth_filter.R

yth_filterR Documentation

Filtered xts object

Description

yth_filter returns an xts object containing user defined combinations of the original, trend, cycle, and random walk series.

Usage

yth_filter(x, h = 8, p = 4, output = c("x", "trend", "cycle", "random"), ...)

Arguments

x

A univariate xts object of any zoo index class, such as Date, yearmon, or yearqtr. For converting objects of type timeSeries, ts, irts, fts, matrix, data.frame or zoo, please read as.xts.

h

An integer, defining the lookahead period. Defaults to h = 8. The default assumes economic data of quarterly periodicity with a lookahead period of 2 years. This function is not limited by the default parameter, and Econometricians may change it as required.

p

An integer, indicating the number of lags. A Default of p = 4, assumes data is of quarterly periodicity. If data is of monthly periodicity, one may choose p = 12 or aggregate the series to quarterly periodicity and maintain the default. Econometricians should use this parameter to accommodate the Seasonality of their data.

output

A character vector. Defaults to output = c("x","trend", "cycle", "random"), which returns the original time series (x), yth_glm fitted.values ("trend"), yth_glm residuals ("cycle"), and a random walk series defined by differencing y_{t+h} and y_t ("random"). Arguments "x", "trend", "cycle", and "random" extract their corresponding univariate series and can be merged in any combination. For example c("x", "trend") returns both the original series "x" and the "trend" components. c("cycle", "random") will return both the "cycle" and "random" components.

...

other arguments passed to the function glm

Details

For time series of quarterly periodicity, Hamilton suggests parameters of h = 8 and p = 4, or an AR(4) process, additionally lagged by 8 lookahead periods. Econometricians may explore variations of h. However, p is designed to correspond with the seasonality of a given periodicity and should be matched accordingly.

y_{t+h} = β_0 + β_1 y_t + β_2 y_{t-1} + β_3 y_{t-2} + β_4 y_{t-3} + v_{t+h}

\hat{v}_{t+h} = y_{t+h} - \hat{β}_0 + \hat{β}_1 y_t + \hat{β}_2 y_{t-1} + \hat{β}_3 y_{t-2} + \hat{β}_4 y_{t-3}

Which can be rewritten as:

y_{t} = β_0 + β_1 y_{t-8} + β_2 y_{t-9} + β_3 y_{t-10} + β_4 y_{t-11} + v_{t}

\hat{v}_{t} = y_{t} - \hat{β}_0 + \hat{β}_1 y_{t-8} + \hat{β}_2 y_{t-9} + \hat{β}_3 y_{t-10} + \hat{β}_4 y_{t-11}

Value

An xts object defined by the output parameter.

References

James D. Hamilton. Why You Should Never Use the Hodrick-Prescott Filter. NBER Working Paper No. 23429, Issued in May 2017.

See Also

yth_glm

Examples

data(GDPC1)

gdp_filter <- yth_filter(100*log(GDPC1), h = 8, p = 4)

head(gdp_filter, 15)

#---------------------------------------------------------------------------#

data(PAYEMS)

log_Employment <- 100*log(xts::to.quarterly(PAYEMS["1947/2016-6"], OHLC = FALSE))

employ_trend <- yth_filter(log_Employment, h = 8, p = 4, output = c("x", "trend"))

plot(employ_trend, grid.col = "white", legend.loc = "topleft", 
         main = "Log of Employment and trend")

#----------------------------------------------------------------------------#

quarterly_data <- 100*log(merge(GDPC1, PCECC96, GPDIC1, EXPGSC1, IMPGSC1, GCEC1, GDPDEF))

cycle <- do.call(merge, lapply(quarterly_data, yth_filter, output = "cycle"))
random <- do.call(merge, lapply(quarterly_data, yth_filter, output = "random"))

cycle.sd <- t(data.frame(lapply(cycle, sd, na.rm = TRUE)))
GDP.cor <- t(data.frame(lapply(cycle, cor, cycle[,1], use = "complete.obs")))
random.sd <- t(data.frame(lapply(random, sd, na.rm = TRUE)))
random.cor <- t(data.frame(lapply(random, cor, random[,1], use = "complete.obs")))

my_table_2 <- round(data.frame(cbind(cycle.sd, GDP.cor, random.sd, random.cor)), 2)
names(my_table_2) <- names(Hamilton_table_2)[1:4]

my_table_2

JustinMShea/neverhpfilter documentation built on Dec. 31, 2022, 1:15 p.m.