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

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

Value

An xts object defined by the output parameter.

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} = \beta_0 + \beta_1 y_t + \beta_2 y_{t-1} + \beta_3 y_{t-2} + \beta_4 y_{t-3} + v_{t+h}$$ $$\hat{v}_{t+h} = y_{t+h} - \hat{\beta}_0 + \hat{\beta}_1 y_t + \hat{\beta}_2 y_{t-1} + \hat{\beta}_3 y_{t-2} + \hat{\beta}_4 y_{t-3}$$ Which can be rewritten as: $$y_{t} = \beta_0 + \beta_1 y_{t-8} + \beta_2 y_{t-9} + \beta_3 y_{t-10} + \beta_4 y_{t-11} + v_{t}$$ $$\hat{v}_{t} = y_{t} - \hat{\beta}_0 + \hat{\beta}_1 y_{t-8} + \hat{\beta}_2 y_{t-9} + \hat{\beta}_3 y_{t-10} + \hat{\beta}_4 y_{t-11}$$

References

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

See also

Examples

data(GDPC1) gdp_filter <- yth_filter(100*log(GDPC1), h = 8, p = 4) knitr::kable(head(gdp_filter, 15), align = 'l')
#> #> #> |GDPC1 |GDPC1.trend |GDPC1.cycle |GDPC1.random | #> |:--------|:-----------|:-----------|:------------| #> |761.7298 |NA |NA |NA | #> |761.4627 |NA |NA |NA | #> |761.2560 |NA |NA |NA | #> |762.8081 |NA |NA |NA | #> |764.3012 |NA |NA |NA | #> |765.9384 |NA |NA |NA | #> |766.5096 |NA |NA |NA | #> |766.6213 |NA |NA |NA | #> |765.2338 |NA |NA |3.503988 | #> |764.8921 |NA |NA |3.429356 | #> |765.9192 |NA |NA |4.663188 | #> |765.0764 |772.5565 |-7.4800757 |2.268271 | #> |768.9313 |773.6959 |-4.7646846 |4.630074 | #> |771.9355 |774.7931 |-2.8576273 |5.997144 | #> |775.7271 |775.1513 |0.5758054 |9.217473 |
#---------------------------------------------------------------------------# 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] knitr::kable(my_table_2, align = 'l')
#> #> #> | |cycle.sd |gdp.cor |random.sd |gdp.rand.cor | #> |:-------------|:--------|:-------|:---------|:------------| #> |GDPC1.cycle |3.35 |1.00 |3.69 |1.00 | #> |PCECC96.cycle |2.89 |0.80 |3.13 |0.83 | #> |GPDIC1.cycle |12.76 |0.83 |13.30 |0.78 | #> |EXPGSC1.cycle |10.96 |0.38 |11.50 |0.36 | #> |IMPGSC1.cycle |9.70 |0.77 |9.99 |0.77 | #> |GCEC1.cycle |6.95 |0.30 |8.31 |0.37 | #> |GDPDEF.cycle |2.92 |0.05 |4.05 |-0.09 |