| Title: | Modified and Fully Modified Hodrick-Prescott Filters |
|---|---|
| Description: | Decomposes macroeconomic time series into trend and cycle with the Hodrick-Prescott (HP) filter and two data-driven versions of it. The modified HP filter of McDermott (1997), assessed by Choudhary, Hanif and Iqbal (2014) <doi:10.1080/00036846.2014.894631>, chooses the smoothing parameter of every series by generalised cross-validation (as described in Coe and McDermott (1997) <doi:10.2307/3867497>). The fully modified HP filter of Hanif, Iqbal and Choudhary (2017, State Bank of Pakistan Working Paper 88) also reduces the end-point bias of the HP filter: it raises the smoothing parameter at the first and last observations, with linear or quadratic weights chosen to minimise the loss of Bloechl (2014, University of Munich Discussion Paper 2014-1). Every filter is a banded linear solve, so the searches are fast; batch processing of many series, comparisons of the filters, the filter weights, gain functions and end-point loss, simulation of trend-plus-cycle series and the power comparison of the paper, 'ggplot2' graphics, a plain-language interpretation of every result and a built-in 'shiny' application. |
| Authors: | Muhammad Yaseen [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-5923-1714>), Javed Iqbal [ctb] (Original methodology author), M. Nadim Hanif [ctb, cph] (Original methodology author; author of the MATLAB code of the fully modified HP filter), M. Ali Choudhary [ctb] (Original methodology author) |
| Maintainer: | Muhammad Yaseen <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.0 |
| Built: | 2026-11-06 07:11:19 UTC |
| Source: | https://github.com/myaseen208/mhpfilter |
autoplot() (and plot()) of a filter result draws the series with its trend and, below, the cycle,
with the time of a ts on the horizontal axis. For the modified HP filter which = "gcv" draws the
criterion against ; for the fully modified HP filter which = "lambda" draws the
smoothing parameters and which = "loss" the end-point loss.
autoplot.mhp(object, which = c("decomposition", "gcv", "lambda", "loss"), ...)autoplot.mhp(object, which = c("decomposition", "gcv", "lambda", "loss"), ...)
object |
a result of |
which |
|
... |
not used. |
A ggplot object.
Muhammad Yaseen ([email protected])
data(us_macro) f <- fmhp_filter( x = us_macro$gdp , max_lambda = 3000 , k_linear = 15 , k_quadratic = NULL , alpha_linear = seq(from = 0, to = 2000, by = 10) , alpha_quadratic = seq(from = 0, to = 2000, by = 5) , schemes = c("linear", "quadratic") , form = "rows" , lambda = NULL , log_scale = TRUE , as_dt = FALSE ) ggplot2::autoplot(object = f) plot(x = f, which = "lambda")data(us_macro) f <- fmhp_filter( x = us_macro$gdp , max_lambda = 3000 , k_linear = 15 , k_quadratic = NULL , alpha_linear = seq(from = 0, to = 2000, by = 10) , alpha_quadratic = seq(from = 0, to = 2000, by = 5) , schemes = c("linear", "quadratic") , form = "rows" , lambda = NULL , log_scale = TRUE , as_dt = FALSE ) ggplot2::autoplot(object = f) plot(x = f, which = "lambda")
fmhp_batch() applies fmhp_filter() to every column of a matrix or data frame, as
mhp_batch() does for the modified HP filter.
fmhp_batch( X, max_lambda = 3000L, k_linear = 15L, k_quadratic = NULL, alpha_linear = seq(0, 2000, 10), alpha_quadratic = seq(0, 2000, 5), schemes = c("linear", "quadratic"), form = c("rows", "differences"), lambda = NULL, log_scale = TRUE ) ## Default S3 method: fmhp_batch( X, max_lambda = 3000L, k_linear = 15L, k_quadratic = NULL, alpha_linear = seq(0, 2000, 10), alpha_quadratic = seq(0, 2000, 5), schemes = c("linear", "quadratic"), form = c("rows", "differences"), lambda = NULL, log_scale = TRUE )fmhp_batch( X, max_lambda = 3000L, k_linear = 15L, k_quadratic = NULL, alpha_linear = seq(0, 2000, 10), alpha_quadratic = seq(0, 2000, 5), schemes = c("linear", "quadratic"), form = c("rows", "differences"), lambda = NULL, log_scale = TRUE ) ## Default S3 method: fmhp_batch( X, max_lambda = 3000L, k_linear = 15L, k_quadratic = NULL, alpha_linear = seq(0, 2000, 10), alpha_quadratic = seq(0, 2000, 5), schemes = c("linear", "quadratic"), form = c("rows", "differences"), lambda = NULL, log_scale = TRUE )
X |
a numeric matrix or data frame, one series per column; missing values only at the start or the end. |
max_lambda |
the largest |
k_linear |
number of end values with linear weights (default 15). |
k_quadratic |
number of end values with quadratic weights (default |
alpha_linear, alpha_quadratic
|
the grids of the weight |
schemes |
the schemes compared: |
form |
|
lambda |
|
log_scale |
filter the logarithm of the series (default |
A data.table with the columns series, t, original, trend,
cycle and the attribute lambdas, a data.table with series, lambda,
gcv, lambda_mhp, scheme, alpha, k and loss.
Muhammad Yaseen ([email protected])
fmhp_filter, mhp_batch, plot_batch
data(us_macro) X <- as.matrix(us_macro[, c("gdp", "consumption", "investment")]) fb <- fmhp_batch( X = X , max_lambda = 3000 , k_linear = 15 , k_quadratic = NULL , alpha_linear = seq(from = 0, to = 2000, by = 10) , alpha_quadratic = seq(from = 0, to = 2000, by = 5) , schemes = c("linear", "quadratic") , form = "rows" , lambda = NULL , log_scale = TRUE ) attr(x = fb, which = "lambdas")data(us_macro) X <- as.matrix(us_macro[, c("gdp", "consumption", "investment")]) fb <- fmhp_batch( X = X , max_lambda = 3000 , k_linear = 15 , k_quadratic = NULL , alpha_linear = seq(from = 0, to = 2000, by = 10) , alpha_quadratic = seq(from = 0, to = 2000, by = 5) , schemes = c("linear", "quadratic") , form = "rows" , lambda = NULL , log_scale = TRUE ) attr(x = fb, which = "lambdas")
fmhp_compare() filters one series with the three filters and compares their cycles and their end-point
loss (Hanif et al. 2017, Tables 3a and 3b): the HP filter at the conventional of the
frequency, the modified HP filter and the fully modified HP filter.
fmhp_compare( x, frequency = c("quarterly", "annual", "monthly"), max_lambda = 3000L, log_scale = TRUE, end_share = 0.1 ) ## Default S3 method: fmhp_compare( x, frequency = c("quarterly", "annual", "monthly"), max_lambda = 3000L, log_scale = TRUE, end_share = 0.1 )fmhp_compare( x, frequency = c("quarterly", "annual", "monthly"), max_lambda = 3000L, log_scale = TRUE, end_share = 0.1 ) ## Default S3 method: fmhp_compare( x, frequency = c("quarterly", "annual", "monthly"), max_lambda = 3000L, log_scale = TRUE, end_share = 0.1 )
x |
a numeric vector or a |
frequency |
|
max_lambda |
the largest |
log_scale |
filter the logarithm of the series (default |
end_share |
share of the observations counted as ends on each side (default 0.1, as in Table 2 of the paper). |
Besides the moments of mhp_compare(), the table gives the total loss of each filter
(hp_loss()), its standard deviation of the cycle at the ends (the first and last
end_share of the observations) and in the middle. A filter without end-point bias has a loss near zero and
about the same cycle variability at the ends as in the middle.
A data.table with one row per filter (HP, Modified HP, Fully modified HP) and
the columns method, lambda, cycle_sd, cycle_mean, ar1, cycle_range,
gcv, loss, sd_ends, sd_middle.
Muhammad Yaseen ([email protected])
fmhp_filter, mhp_compare, hp_loss
data(us_macro) fc <- fmhp_compare( x = us_macro$gdp , frequency = "quarterly" , max_lambda = 3000 , log_scale = TRUE , end_share = 0.1 ) fcdata(us_macro) fc <- fmhp_compare( x = us_macro$gdp , frequency = "quarterly" , max_lambda = 3000 , log_scale = TRUE , end_share = 0.1 ) fc
fmhp_filter() applies the fully modified HP (FMHP) filter of Hanif, Iqbal and Choudhary (2017). It removes
the two weaknesses of the HP filter at once: the smoothing parameter is chosen from the data (as in the modified
HP filter of McDermott 1997), and the end-point bias is reduced by raising the smoothing parameter at the first
and last observations, with weights chosen to minimise the loss of Bloechl (2014), the distance of the
gain of every estimate from the gain at the centre of the series.
fmhp_filter( x, max_lambda = 3000L, k_linear = 15L, k_quadratic = NULL, alpha_linear = seq(0, 2000, 10), alpha_quadratic = seq(0, 2000, 5), schemes = c("linear", "quadratic"), form = c("rows", "differences"), lambda = NULL, log_scale = TRUE, as_dt = TRUE ) ## Default S3 method: fmhp_filter( x, max_lambda = 3000L, k_linear = 15L, k_quadratic = NULL, alpha_linear = seq(0, 2000, 10), alpha_quadratic = seq(0, 2000, 5), schemes = c("linear", "quadratic"), form = c("rows", "differences"), lambda = NULL, log_scale = TRUE, as_dt = TRUE )fmhp_filter( x, max_lambda = 3000L, k_linear = 15L, k_quadratic = NULL, alpha_linear = seq(0, 2000, 10), alpha_quadratic = seq(0, 2000, 5), schemes = c("linear", "quadratic"), form = c("rows", "differences"), lambda = NULL, log_scale = TRUE, as_dt = TRUE ) ## Default S3 method: fmhp_filter( x, max_lambda = 3000L, k_linear = 15L, k_quadratic = NULL, alpha_linear = seq(0, 2000, 10), alpha_quadratic = seq(0, 2000, 5), schemes = c("linear", "quadratic"), form = c("rows", "differences"), lambda = NULL, log_scale = TRUE, as_dt = TRUE )
x |
a numeric vector or a |
max_lambda |
the largest |
k_linear |
number of end values with linear weights (default 15). |
k_quadratic |
number of end values with quadratic weights (default |
alpha_linear, alpha_quadratic
|
the grids of the weight |
schemes |
the schemes compared: |
form |
|
lambda |
|
log_scale |
filter the logarithm of the series (default |
as_dt |
|
Gain and loss. Row of the smoother holds the weights of
; its gain at frequency is
(Hanif et al. 2017, eq. 6), and the loss of the estimate is over with and the centre of the series
(eq. 7); the total loss is (eq. 8). See hp_loss().
End-point weights. The smoothing parameter of observation is in the middle and for the -th observation from
the middle among the last (and the first) , , linear
() or quadratic () (eqs. 9a and 9b). See hp_schedule().
The procedure (Section 5 of the paper):
of the modified HP filter (mhp_filter()), searched
over max_lambda;
for each scheme (linear and quadratic), the weight of the grid that minimises the total
loss at ;
with that weight, minimising McDermott's criterion again;
the scheme whose loss in step 2 is smaller gives the trend and the cycle.
The defaults are those of the published MATLAB code (Hanif 2017): max_lambda = 3000; linear weights on
end values with ;
quadratic weights on end values with
; logarithms of the series. The working paper
states in the text; set k_linear and k_quadratic to use it.
Form of the weights. The published code multiplies row of by
, (form = "rows", the default). The penalised least-squares
problem with a separate for each second difference gives instead , a symmetric smoother (form = "differences", the scheme of eq. 9 written on the
differences). Both are solved in operations; the gains need two solves per
frequency and the smoother itself is never formed.
With lambda given, the smoothing parameter is fixed (steps 1 and 3 are skipped): the HP filter with the
endogenous weights of the paper (Table 3a, column "HP (our scheme)").
With as_dt = TRUE, a data.table with the columns original, trend,
cycle (and time for a ts) and the attributes lambda, gcv, lambda_mhp,
scheme, alpha, k, loss and lambda_t. With as_dt = FALSE, a list of
class c("mhp_fmhp", "mhp", "mhp_result", "list"):
original, trend, cycle
the series, its trend and its cycle (on the scale of the series).
lambda, gcv
and its criterion.
lambda_mhp of step 1.
scheme, alpha, k
the chosen scheme, its weight and its number of end values.
lossthe total loss of the chosen scheme at
(the criterion of step 4).
lambda_tthe smoothing parameters of the trend.
schemesdata.table, one row per scheme: scheme, k, alpha,
loss, lambda, gcv.
form, log_scale
the settings; then trend_log, cycle_log (with
log_scale = TRUE), time and frequency (for a ts).
Methods: print(), plot(), ggplot2::autoplot(), hp_loss(),
mhp_interpret().
Muhammad Yaseen ([email protected]), from the MATLAB code of Muhammad Nadim Hanif
Hanif, M. N., Iqbal, J. and Choudhary, M. A. (2017). Fully modified HP filter. SBP Working Paper Series, No. 88, State Bank of Pakistan.
Hanif, M. N. (2017). Fully modified HP filter function (MATLAB code). MATLAB Central File Exchange, 63198.
Bloechl, A. (2014). Reducing the excess variability of the Hodrick-Prescott filter by flexible penalization. Discussion Paper 2014-1, University of Munich.
McDermott, C. J. (1997). An automatic method for choosing the smoothing parameter in the HP filter. Unpublished, International Monetary Fund, Washington DC.
mhp_filter, fmhp_batch, fmhp_compare, hp_loss
data(us_macro) gdp <- ts(data = us_macro$gdp, start = c(1959, 1), frequency = 4) fm <- fmhp_filter( x = gdp , max_lambda = 3000 , k_linear = 15 , k_quadratic = NULL , alpha_linear = seq(from = 0, to = 2000, by = 10) , alpha_quadratic = seq(from = 0, to = 2000, by = 5) , schemes = c("linear", "quadratic") , form = "rows" , lambda = NULL , log_scale = TRUE , as_dt = FALSE ) fm fm$schemes plot(x = fm)data(us_macro) gdp <- ts(data = us_macro$gdp, start = c(1959, 1), frequency = 4) fm <- fmhp_filter( x = gdp , max_lambda = 3000 , k_linear = 15 , k_quadratic = NULL , alpha_linear = seq(from = 0, to = 2000, by = 10) , alpha_quadratic = seq(from = 0, to = 2000, by = 5) , schemes = c("linear", "quadratic") , form = "rows" , lambda = NULL , log_scale = TRUE , as_dt = FALSE ) fm fm$schemes plot(x = fm)
fmhp_simulation() repeats the simulation study of Hanif, Iqbal and Choudhary (2017, Tables 1 and 2): for
each design it simulates series with mhp_simulate(), filters them with the HP, modified HP and fully
modified HP filters, and compares the root mean square error of the estimated cycles against the true one, over
the whole series, its middle and its ends.
fmhp_simulation( designs = mhp_designs(), n_rep = 100L, T = 200L, trend = c("linear", "nonlinear"), aggregate = c("none", "systematic", "sum", "average"), lambda = NULL, max_lambda = 3000L, seed = NULL, form = c("rows", "differences") )fmhp_simulation( designs = mhp_designs(), n_rep = 100L, T = 200L, trend = c("linear", "nonlinear"), aggregate = c("none", "systematic", "sum", "average"), lambda = NULL, max_lambda = 3000L, seed = NULL, form = c("rows", "differences") )
designs |
designs as in |
n_rep |
replicates per design (default 100; the paper used 1000). |
T |
number of quarterly observations (default 200). |
trend, aggregate
|
as in |
lambda |
the HP smoothing parameter (default |
max_lambda |
the largest |
seed |
seed of the random numbers ( |
form |
the form of the end-point weights of the fully modified HP filter (default |
The RMSE of a filter is ; the middle is
the central 80% of the observations and the ends the first and last 10%. fmhp_better is the share of
replicates in which the FMHP filter has a smaller RMSE than the HP filter (Table 1 of the paper reports it in
percent). The series are filtered on their own scale (log_scale = FALSE).
An object of class c("mhp_simulation", "mhp_result", "list"): rmse (a data.table
with one row per design and filter: model, sd_ratio, ar1, ar2, method,
rmse, rmse_middle, rmse_ends), power (one row per design: the share of replicates in
which FMHP beats HP and MHP) and settings. plot() draws the RMSE by design.
Muhammad Yaseen ([email protected])
Hanif, M. N., Iqbal, J. and Choudhary, M. A. (2017). Fully modified HP filter. SBP Working Paper Series, No. 88, State Bank of Pakistan.
sim <- fmhp_simulation( designs = mhp_designs()[c(3, 13, 23)] , n_rep = 2 , T = 120 , trend = "linear" , aggregate = "none" , lambda = NULL , max_lambda = 3000 , seed = 1 , form = "rows" ) sim$powersim <- fmhp_simulation( designs = mhp_designs()[c(3, 13, 23)] , n_rep = 2 , T = 120 , trend = "linear" , aggregate = "none" , lambda = NULL , max_lambda = 3000 , seed = 1 , form = "rows" ) sim$power
get_lambda() returns the smoothing parameter and get_gcv() the value of McDermott's criterion of a
result of hp_filter(), mhp_filter() or fmhp_filter(), whether it is a
data.table (as_dt = TRUE) or a list (as_dt = FALSE).
get_lambda(x) ## Default S3 method: get_lambda(x) get_gcv(x) ## Default S3 method: get_gcv(x)get_lambda(x) ## Default S3 method: get_lambda(x) get_gcv(x) ## Default S3 method: get_gcv(x)
x |
a filter result. |
get_lambda(): the smoothing parameter; get_gcv(): the criterion at that value.
Muhammad Yaseen ([email protected])
data(us_macro) m <- mhp_filter( x = log(us_macro$gdp) , max_lambda = 100000 , as_dt = TRUE , log_scale = FALSE , criterion = c("approximate", "exact") ) get_lambda(x = m) get_gcv(x = m)data(us_macro) m <- mhp_filter( x = log(us_macro$gdp) , max_lambda = 100000 , as_dt = TRUE , log_scale = FALSE , criterion = c("approximate", "exact") ) get_lambda(x = m) get_gcv(x = m)
hp_filter() splits a time series into a smooth trend and a cycle with the filter of Hodrick and Prescott
(1997) and a smoothing parameter chosen by the user: 1600 for quarterly data, 100 for annual
data and 129600 for monthly data are the conventional values (Ravn and Uhlig 2002).
hp_filter(x, lambda = 1600, as_dt = TRUE, log_scale = FALSE) ## Default S3 method: hp_filter(x, lambda = 1600, as_dt = TRUE, log_scale = FALSE)hp_filter(x, lambda = 1600, as_dt = TRUE, log_scale = FALSE) ## Default S3 method: hp_filter(x, lambda = 1600, as_dt = TRUE, log_scale = FALSE)
x |
a numeric vector or a |
lambda |
the smoothing parameter |
as_dt |
|
log_scale |
filter the logarithm of the series (default |
The trend of the series minimises
so that with and the
matrix of second differences (rows ). The cycle is
. The system is pentadiagonal, symmetric and positive
definite and is solved in operations.
With log_scale = TRUE the filter is applied to ; the trend is returned as
and the cycle as , and the components
on the log scale are added to the list result.
With as_dt = TRUE, a data.table with the columns original, trend,
cycle (and time for a ts) and the attribute lambda. With as_dt = FALSE, a list
of class c("mhp_hp", "mhp", "mhp_result", "list") with original, trend, cycle,
lambda, then log_scale, trend_log and cycle_log (with log_scale = TRUE) and
time and frequency (for a ts). Methods: print(), plot(),
ggplot2::autoplot(), mhp_interpret().
Muhammad Yaseen ([email protected])
Hodrick, R. J. and Prescott, E. C. (1997). Postwar U.S. business cycles: an empirical investigation. Journal of Money, Credit and Banking, 29(1), 1–16. doi:10.2307/2953682
Ravn, M. O. and Uhlig, H. (2002). On adjusting the Hodrick-Prescott filter for the frequency of observations. The Review of Economics and Statistics, 84(2), 371–376. doi:10.1162/003465302317411604
mhp_filter, fmhp_filter, mhp_compare
data(us_macro) gdp <- ts(data = us_macro$gdp, start = c(1959, 1), frequency = 4) hp <- hp_filter( x = log(gdp) , lambda = 1600 , as_dt = TRUE , log_scale = FALSE ) hp attr(x = hp, which = "lambda")data(us_macro) gdp <- ts(data = us_macro$gdp, start = c(1959, 1), frequency = 4) hp <- hp_filter( x = log(gdp) , lambda = 1600 , as_dt = TRUE , log_scale = FALSE ) hp attr(x = hp, which = "lambda")
hp_gain() gives the gain of the estimate at every time as a function of the frequency
(Hanif et al. 2017, eq. 6, Figures 2 and 2b): the factor by which the filter scales an oscillation of that
frequency. In the middle of a series the gain of the trend falls to zero at high frequencies; at the ends it does
not, so the trend there keeps part of the cycle.
hp_gain( x, lambda = 1600, form = c("rows", "differences"), theta = seq(0, pi, length.out = 101), rows = NULL ) ## Default S3 method: hp_gain( x, lambda = 1600, form = c("rows", "differences"), theta = seq(0, pi, length.out = 101), rows = NULL )hp_gain( x, lambda = 1600, form = c("rows", "differences"), theta = seq(0, pi, length.out = 101), rows = NULL ) ## Default S3 method: hp_gain( x, lambda = 1600, form = c("rows", "differences"), theta = seq(0, pi, length.out = 101), rows = NULL )
x |
the length |
lambda |
a smoothing parameter, or one per observation ( |
form |
|
theta |
the frequencies (default 101 values from 0 to |
rows |
the rows drawn by |
With the weights of hp_weights(), . It is computed from two solves of the
smoothing system per frequency.
An object of class c("mhp_gain", "mhp_result", "list"): gain (a data.table with
t, theta, gain for every time and frequency), lambda_t, form, rows.
plot() draws the gains of the chosen rows.
Muhammad Yaseen ([email protected])
g <- hp_gain( x = 50 , lambda = 100 , form = "rows" , theta = seq(from = 0, to = pi, length.out = 101) , rows = NULL ) plot(x = g)g <- hp_gain( x = 50 , lambda = 100 , form = "rows" , theta = seq(from = 0, to = pi, length.out = 101) , rows = NULL ) plot(x = g)
hp_loss() measures the end-point bias of a filter by the loss of Bloechl (2014) used by Hanif, Iqbal and
Choudhary (2017): for every time , the squared distance of the gain of its estimate from the gain at
the centre of the series. The loss is zero in the middle and grows towards the ends; the fully modified HP filter
chooses its end-point weights to make the total small.
hp_loss( x, lambda = 1600, form = c("rows", "differences"), theta = seq(0, 3.1, 0.1), weight = 0.1 ) ## Default S3 method: hp_loss( x, lambda = 1600, form = c("rows", "differences"), theta = seq(0, 3.1, 0.1), weight = 0.1 )hp_loss( x, lambda = 1600, form = c("rows", "differences"), theta = seq(0, 3.1, 0.1), weight = 0.1 ) ## Default S3 method: hp_loss( x, lambda = 1600, form = c("rows", "differences"), theta = seq(0, 3.1, 0.1), weight = 0.1 )
x |
the length |
lambda |
a smoothing parameter, or one per observation ( |
form |
|
theta |
the frequencies (default |
weight |
the factor |
with the gains of hp_gain(), , (their spacing) and
(Hanif et al. 2017, eqs. 7 and 8, and their MATLAB code). The loss depends on and the smoothing
parameters only, not on the data: for the HP filter with it is 1.731 to 1.735
for 41 to 55 observations, the values of Table 3a of the paper.
An object of class c("mhp_loss", "mhp_result", "list"): loss (a data.table with
t and loss), total (), lambda_t, form. plot() draws
against (Figures 3 and 3b of the paper).
Muhammad Yaseen ([email protected])
Bloechl, A. (2014). Reducing the excess variability of the Hodrick-Prescott filter by flexible penalization. Discussion Paper 2014-1, University of Munich.
Hanif, M. N., Iqbal, J. and Choudhary, M. A. (2017). Fully modified HP filter. SBP Working Paper Series, No. 88, State Bank of Pakistan.
fmhp_filter, hp_gain, fmhp_compare
hl <- hp_loss( x = 55 , lambda = 100 , form = "rows" , theta = seq(from = 0, to = 3.1, by = 0.1) , weight = 0.1 ) hl$total plot(x = hl)hl <- hp_loss( x = 55 , lambda = 100 , form = "rows" , theta = seq(from = 0, to = 3.1, by = 0.1) , weight = 0.1 ) hl$total plot(x = hl)
hp_schedule() gives the smoothing parameter of every observation (or second difference) under the
end-point schemes of Hanif, Iqbal and Choudhary (2017, eqs. 9a and 9b): in the middle and
for the -th of the values at each end,
counted from the middle ( at the first and last observation).
hp_schedule(T, lambda, alpha, k, power = 1, form = c("rows", "differences"))hp_schedule(T, lambda, alpha, k, power = 1, form = c("rows", "differences"))
T |
length of the series. |
lambda |
the base smoothing parameter. |
alpha |
the weight |
k |
number of raised values at each end. |
power |
|
form |
|
A numeric vector of smoothing parameters.
Muhammad Yaseen ([email protected])
hp_schedule( T = 50 , lambda = 100 , alpha = 10 , k = 20 , power = 1 , form = "rows" )hp_schedule( T = 50 , lambda = 100 , alpha = 10 , k = 20 , power = 1 , form = "rows" )
hp_weights() gives the matrix of the trend : row holds the weights of the estimate at time . Symmetric in
the middle of the series, the rows become asymmetric near the ends, the source of the end-point bias (Hanif et
al. 2017, Figures 1 and 1a).
hp_weights(x, lambda = 1600, form = c("rows", "differences"), rows = NULL) ## Default S3 method: hp_weights(x, lambda = 1600, form = c("rows", "differences"), rows = NULL)hp_weights(x, lambda = 1600, form = c("rows", "differences"), rows = NULL) ## Default S3 method: hp_weights(x, lambda = 1600, form = c("rows", "differences"), rows = NULL)
x |
the length |
lambda |
a smoothing parameter, or one per observation ( |
form |
|
rows |
the rows drawn by |
An object of class c("mhp_weights", "mhp_result", "list"): weights (the
matrix), lambda_t, form, rows. plot() draws the chosen rows.
Muhammad Yaseen ([email protected])
w <- hp_weights( x = 50 , lambda = 100 , form = "rows" , rows = NULL ) round(x = w$weights[1:3, 1:6], digits = 3) plot(x = w)w <- hp_weights( x = 50 , lambda = 100 , form = "rows" , rows = NULL ) round(x = w$weights[1:3, 1:6], digits = 3) plot(x = w)
mhp_app() starts the application of mhpfilter: load a series (the example us_macro or
a CSV file), filter it with the HP, modified HP or fully modified HP filter, compare the three filters, inspect
their weights, gains and end-point loss, filter all the series of a file at once and run the power comparison of
Hanif, Iqbal and Choudhary (2017). Every result page shows an interpretation computed from the results
(mhp_interpret()), the output as R prints it, formatted tables, plots and the R code that repeats the
analysis, each with a Copy button.
mhp_app(launch.browser = interactive(), ...) mhp_app_object()mhp_app(launch.browser = interactive(), ...) mhp_app_object()
launch.browser |
passed to |
... |
further arguments to |
The application needs the suggested packages shiny, bslib and DT. Plots are sent to the browser as WebP images when magick is installed (smaller than PNG; PNG otherwise) and can be downloaded as PNG, PDF or WebP. Text is set in IBM Plex Sans and output in IBM Plex Mono when the browser can reach Google Fonts, in the system fonts otherwise.
mhp_app() is called for its side effect and returns the value of shiny::runApp() when the
application is closed; mhp_app_object() returns the application object (for shiny::runApp(),
shiny::testServer() or deployment on a server).
Muhammad Yaseen ([email protected])
mhp_filter, fmhp_filter, fmhp_compare, mhp_interpret
if (interactive()) { mhp_app( launch.browser = TRUE ) }if (interactive()) { mhp_app( launch.browser = TRUE ) }
mhp_batch() applies mhp_filter() to every column of a matrix or data frame and returns one
long table, with the smoothing parameter of each series in an attribute. Series of different lengths can share
the matrix: leading and trailing missing values are dropped series by series.
mhp_batch(X, max_lambda = 100000L) ## Default S3 method: mhp_batch(X, max_lambda = 100000L)mhp_batch(X, max_lambda = 100000L) ## Default S3 method: mhp_batch(X, max_lambda = 100000L)
X |
a numeric matrix or data frame, one series per column; missing values only at the start or the end. |
max_lambda |
the largest |
A data.table with the columns series, t, original, trend,
cycle and the attribute lambdas, a data.table with series, lambda and
gcv (as in version 0.1.0).
Muhammad Yaseen ([email protected])
mhp_filter, fmhp_batch, batch_compare, plot_batch
data(us_macro) X <- log(as.matrix(us_macro[, c("gdp", "consumption", "investment")])) mb <- mhp_batch( X = X , max_lambda = 100000 ) attr(x = mb, which = "lambdas")data(us_macro) X <- log(as.matrix(us_macro[, c("gdp", "consumption", "investment")])) mb <- mhp_batch( X = X , max_lambda = 100000 ) attr(x = mb, which = "lambdas")
mhp_cite() gives the references of the methods behind a result of the package, or the references of every
method in the package, as text or BibTeX. citation("mhpfilter") cites the package and its main methods.
mhp_cite(x = NULL, style = c("text", "bibtex")) ## Default S3 method: mhp_cite(x = NULL, style = c("text", "bibtex"))mhp_cite(x = NULL, style = c("text", "bibtex")) ## Default S3 method: mhp_cite(x = NULL, style = c("text", "bibtex"))
x |
a result of a function of the package (with |
style |
|
An object of class bibentry (printed in the chosen style).
Muhammad Yaseen ([email protected])
mhp_cite( x = NULL , style = "text" ) data(us_macro) f <- mhp_filter( x = log(us_macro$gdp) , max_lambda = 100000 , as_dt = FALSE , log_scale = FALSE , criterion = "approximate" ) mhp_cite( x = f , style = "bibtex" )mhp_cite( x = NULL , style = "text" ) data(us_macro) f <- mhp_filter( x = log(us_macro$gdp) , max_lambda = 100000 , as_dt = FALSE , log_scale = FALSE , criterion = "approximate" ) mhp_cite( x = f , style = "bibtex" )
mhp_compare() filters one series with the HP filter at the conventional of its
frequency and with the modified HP filter, and compares the cycles; batch_compare() does the same for
every column of a matrix.
mhp_compare( x, frequency = c("quarterly", "annual", "monthly"), max_lambda = 100000L ) ## Default S3 method: mhp_compare( x, frequency = c("quarterly", "annual", "monthly"), max_lambda = 100000L ) batch_compare( X, frequency = c("quarterly", "annual", "monthly"), max_lambda = 100000L ) ## Default S3 method: batch_compare( X, frequency = c("quarterly", "annual", "monthly"), max_lambda = 100000L )mhp_compare( x, frequency = c("quarterly", "annual", "monthly"), max_lambda = 100000L ) ## Default S3 method: mhp_compare( x, frequency = c("quarterly", "annual", "monthly"), max_lambda = 100000L ) batch_compare( X, frequency = c("quarterly", "annual", "monthly"), max_lambda = 100000L ) ## Default S3 method: batch_compare( X, frequency = c("quarterly", "annual", "monthly"), max_lambda = 100000L )
x |
a numeric vector or a |
frequency |
|
max_lambda |
the largest |
X |
a numeric matrix or data frame, one series per column. |
The conventional values are (quarterly), 100 (annual) and 129600 (monthly,
Ravn and Uhlig 2002). ar1 is the first-order autocorrelation of the cycle, .
mhp_compare(): a data.table with one row per method (HP, Modified HP) and
the columns method, lambda, cycle_sd, cycle_mean, ar1, cycle_range,
gcv. batch_compare(): a data.table with one row per series and the columns
series, hp_lambda, mhp_lambda, hp_cycle_sd, mhp_cycle_sd, sd_diff,
hp_ar1, mhp_ar1, ar1_diff, relative_sd.
Muhammad Yaseen ([email protected])
data(us_macro) cmp <- mhp_compare( x = log(us_macro$gdp) , frequency = "quarterly" , max_lambda = 100000 ) cmp bc <- batch_compare( X = log(as.matrix(us_macro[, c("gdp", "consumption", "investment")])) , frequency = "quarterly" , max_lambda = 100000 ) bcdata(us_macro) cmp <- mhp_compare( x = log(us_macro$gdp) , frequency = "quarterly" , max_lambda = 100000 ) cmp bc <- batch_compare( X = log(as.matrix(us_macro[, c("gdp", "consumption", "investment")])) , frequency = "quarterly" , max_lambda = 100000 ) bc
mhp_designs() lists the 30 combinations of Table 1 of the paper: five ratios
of the standard deviations of the trend and cycle
shocks (10, 5, 2, 1, 0.5) crossed with six AR(2) cycles : (0.9, 0.01),
(1.2, -0.25), (1.2, -0.4), (1.2, -0.55), (1.2, -0.75) and (0.8, 0). The designs of Choudhary, Hanif and Iqbal
(2014, Table 1) have the same cycles and the ratios 10, 5, 1, 0.5 and 0.01.
mhp_designs(paper = c("2017", "2014"))mhp_designs(paper = c("2017", "2014"))
paper |
|
A data.table with the columns model (1 to 30, as in the paper), sd_ratio,
ar1, ar2.
Muhammad Yaseen ([email protected])
Choudhary, M. A., Hanif, M. N. and Iqbal, J. (2014). On smoothing macroeconomic time series using the modified HP filter. Applied Economics, 46(19), 2205–2214. doi:10.1080/00036846.2014.894631
Hanif, M. N., Iqbal, J. and Choudhary, M. A. (2017). Fully modified HP filter. SBP Working Paper Series, No. 88, State Bank of Pakistan.
mhp_designs( paper = "2017" )mhp_designs( paper = "2017" )
mhp_filter() applies the modified HP filter of McDermott (1997), studied by Choudhary, Hanif and Iqbal
(2014): the smoothing parameter is the one of
max_lambda that minimises a generalised cross-validation criterion, so that every series gets its own
instead of the conventional 1600 or 100.
mhp_filter( x, max_lambda = 100000L, as_dt = TRUE, log_scale = FALSE, criterion = c("approximate", "exact") ) ## Default S3 method: mhp_filter( x, max_lambda = 100000L, as_dt = TRUE, log_scale = FALSE, criterion = c("approximate", "exact") )mhp_filter( x, max_lambda = 100000L, as_dt = TRUE, log_scale = FALSE, criterion = c("approximate", "exact") ) ## Default S3 method: mhp_filter( x, max_lambda = 100000L, as_dt = TRUE, log_scale = FALSE, criterion = c("approximate", "exact") )
x |
a numeric vector or a |
max_lambda |
the largest |
as_dt |
|
log_scale |
filter the logarithm of the series (default |
criterion |
|
For each candidate the trend of
hp_filter() is computed and
(Choudhary et al. 2014, eq. 6; Hanif et al. 2017, eq. 4); minimises it. This is
Silverman's (1984) approximation of generalised cross-validation (Craven and Wahba 1979),
(Choudhary et al. 2014, eq. 5), which criterion = "exact" uses: the eigenvalues
of , computed once, give and the residual sum of squares for
every in operations (the decomposition costs , a
fraction of a second for a few hundred observations). Each trend costs
operations, so the search over 100000 values takes about a second for 200 quarterly observations.
The search is that of the authors' MATLAB code (modifiedhpfilternew.m, Choudhary et al. 2014), which
omits the factor (it does not change the minimum) and works on the series as given; filter the
logarithm of a series (as in the papers) with log_scale = TRUE or by passing log(x).
Changes from version 0.1.0. The criterion is unchanged (the help page of 0.1.0 printed the factor as
, but the code used ); every trend is now
a banded solve instead of a dense one, so results agree with 0.1.0 to rounding error at a fraction of the time.
With as_dt = TRUE, a data.table with the columns original, trend,
cycle (and time for a ts) and the attributes lambda and gcv. With
as_dt = FALSE, a list of class c("mhp_mhp", "mhp", "mhp_result", "list") with original,
trend, cycle, lambda, gcv as in version 0.1.0, then gcv_path (the criterion
for every of the search), log_scale, criterion, trend_log and
cycle_log (with
log_scale = TRUE), time and frequency (for a ts). Methods: print(),
plot(),
ggplot2::autoplot(), mhp_interpret().
Muhammad Yaseen ([email protected])
Choudhary, M. A., Hanif, M. N. and Iqbal, J. (2014). On smoothing macroeconomic time series using the modified HP filter. Applied Economics, 46(19), 2205–2214. doi:10.1080/00036846.2014.894631
Craven, P. and Wahba, G. (1979). Smoothing noisy data with spline functions. Numerische Mathematik, 31, 377–403. doi:10.1007/BF01404567
Silverman, B. W. (1984). A fast and efficient cross-validation method for smoothing parameter choice in spline regression. Journal of the American Statistical Association, 79(387), 584–589. doi:10.1080/01621459.1984.10478084
McDermott, C. J. (1997). An automatic method for choosing the smoothing parameter in the HP filter. Unpublished, International Monetary Fund, Washington DC; described in Coe, D. T. and McDermott, C. J. (1997). Does the gap model work in Asia? IMF Staff Papers, 44(1), 59–80. doi:10.2307/3867497
hp_filter, fmhp_filter, mhp_batch, get_lambda
data(us_macro) gdp <- ts(data = us_macro$gdp, start = c(1959, 1), frequency = 4) mhp <- mhp_filter( x = log(gdp) , max_lambda = 100000 , as_dt = FALSE , log_scale = FALSE , criterion = "approximate" ) mhp plot(x = mhp)data(us_macro) gdp <- ts(data = us_macro$gdp, start = c(1959, 1), frequency = 4) mhp <- mhp_filter( x = log(gdp) , max_lambda = 100000 , as_dt = FALSE , log_scale = FALSE , criterion = "approximate" ) mhp plot(x = mhp)
mhp_interpret() turns a result of the package into a short report: a headline, findings, recommendations
and cautions, computed from its numbers, with the sources of the method. It covers the filters
(hp_filter(), mhp_filter(), fmhp_filter(), as lists or as
data.tables), the end-point loss (hp_loss()) and the power comparison
(fmhp_simulation()).
mhp_interpret(x, top = 3L, ...) ## Default S3 method: mhp_interpret(x, top = 3L, ...) ## S3 method for class 'mhp_interpretation' print(x, ...) ## S3 method for class 'mhp_interpretation' format(x, style = c("text", "markdown"), width = getOption("width", 80L), ...)mhp_interpret(x, top = 3L, ...) ## Default S3 method: mhp_interpret(x, top = 3L, ...) ## S3 method for class 'mhp_interpretation' print(x, ...) ## S3 method for class 'mhp_interpretation' format(x, style = c("text", "markdown"), width = getOption("width", 80L), ...)
x |
a result of a function of the package. |
top |
number of largest positive and negative cycle values named (default 3). |
... |
unused. |
style |
|
width |
line width of the text (default |
For a filter the report compares the chosen smoothing parameter with the conventional one of the frequency (1600
quarterly, 100 annual, 129600 monthly; known when the series is a ts), names the largest booms and
recessions of the cycle, and warns when a search ended at its limit or the series is short. For the fully
modified HP filter it also gives the reduction of the end-point loss against the HP filter with the same
.
An object of class "mhp_interpretation": a list with analysis, title,
headline, findings, recommendations, cautions, facts (the numbers behind the
sentences), guide (how to read the method) and sources. format() gives it as text or
Markdown.
Muhammad Yaseen ([email protected])
data(us_macro) gdp <- ts(data = us_macro$gdp, start = c(1959, 1), frequency = 4) f <- fmhp_filter( x = gdp , max_lambda = 3000 , k_linear = 15 , k_quadratic = NULL , alpha_linear = seq(from = 0, to = 2000, by = 10) , alpha_quadratic = seq(from = 0, to = 2000, by = 5) , schemes = c("linear", "quadratic") , form = "rows" , lambda = NULL , log_scale = TRUE , as_dt = FALSE ) mhp_interpret( x = f , top = 3 )data(us_macro) gdp <- ts(data = us_macro$gdp, start = c(1959, 1), frequency = 4) f <- fmhp_filter( x = gdp , max_lambda = 3000 , k_linear = 15 , k_quadratic = NULL , alpha_linear = seq(from = 0, to = 2000, by = 10) , alpha_quadratic = seq(from = 0, to = 2000, by = 5) , schemes = c("linear", "quadratic") , form = "rows" , lambda = NULL , log_scale = TRUE , as_dt = FALSE ) mhp_interpret( x = f , top = 3 )
The symbols used in the help pages and vignettes of mhpfilter, with their meaning and the argument or result
component that holds them. The notation follows one rule throughout: scalars are upper-case letters in normal
face (, ), indices lower-case italic (), vectors lower-case bold
(, ) and matrices upper-case bold (,
).
mhp_notation(format = c("table", "path", "tex"))mhp_notation(format = c("table", "path", "tex"))
format |
|
See format.
Muhammad Yaseen ([email protected])
Other mhpfilter utilities:
mhp_reference(),
mhp_save(),
mhp_theme()
mhp_notation( format = c("table", "path", "tex")[1] )mhp_notation( format = c("table", "path", "tex")[1] )
Reads inst/extdata/reference.csv: one row per published number, with its source, the R call
that recomputes it with this package (call), the expression that extracts the number from the result
(extract, in terms of x), the printed value and the digits to compare. The same file drives the test
of published values and the vignette that reproduces the published examples. Each distinct call is evaluated once.
mhp_reference(source = NULL, evaluate = FALSE, level = 1)mhp_reference(source = NULL, evaluate = FALSE, level = 1)
source |
optional regular expression; keep rows whose |
evaluate |
logical; evaluate the rows and add columns |
level |
largest |
A data.table with columns id, source, quantity, call, extract,
value, digits, level, note (and computed, agrees).
Muhammad Yaseen ([email protected])
Other mhpfilter utilities:
mhp_notation(),
mhp_save(),
mhp_theme()
ref <- mhp_reference( source = NULL , evaluate = FALSE , level = 1 ) ref mhp_reference( source = "." , evaluate = TRUE , level = 1 )ref <- mhp_reference( source = NULL , evaluate = FALSE , level = 1 ) ref mhp_reference( source = "." , evaluate = TRUE , level = 1 )
A wrapper of ggsave with the defaults of the package: a vector PDF through
cairo_pdf (embedded fonts, correct glyphs), A4 landscape (11.69 x 8.27 inches) and 600
dpi for raster formats. The device follows the file extension for other formats (.png, .svg,
.tiff, ...).
mhp_save( plot = ggplot2::last_plot(), filename, path = NULL, width = 11.69, height = 8.27, units = c("in", "cm", "mm"), dpi = 600, scale = 1, device = NULL, limitsize = TRUE, ... ) ## Default S3 method: mhp_save( plot = ggplot2::last_plot(), filename, path = NULL, width = 11.69, height = 8.27, units = c("in", "cm", "mm"), dpi = 600, scale = 1, device = NULL, limitsize = TRUE, ... )mhp_save( plot = ggplot2::last_plot(), filename, path = NULL, width = 11.69, height = 8.27, units = c("in", "cm", "mm"), dpi = 600, scale = 1, device = NULL, limitsize = TRUE, ... ) ## Default S3 method: mhp_save( plot = ggplot2::last_plot(), filename, path = NULL, width = 11.69, height = 8.27, units = c("in", "cm", "mm"), dpi = 600, scale = 1, device = NULL, limitsize = TRUE, ... )
plot |
a |
filename |
file name; its extension chooses the format. |
path |
directory to save in (default |
width, height
|
size (default 11.69 x 8.27, A4 landscape, in |
units |
|
dpi |
resolution for raster formats (default 600). |
scale |
multiplicative scaling factor (default 1). |
device |
|
limitsize |
logical; refuse images larger than 50 x 50 inches (default |
... |
passed to |
The file name, invisibly.
Muhammad Yaseen ([email protected])
Other mhpfilter utilities:
mhp_notation(),
mhp_reference(),
mhp_theme()
library(ggplot2) p <- ggplot(data = data.frame(x = 1:10, y = (1:10)^2), mapping = aes(x = x, y = y)) + geom_line() + mhp_theme( base_size = 11 , base_family = "serif" , ticks_inward = TRUE , legend_position = "bottom" ) f <- tempfile(fileext = ".pdf") mhp_save( plot = p , filename = f , path = NULL , width = 11.69 , height = 8.27 , units = c("in", "cm", "mm")[1] , dpi = 600 , scale = 1 , device = NULL , limitsize = TRUE ) mhp_save( plot = p , filename = basename(f) , path = tempdir() , width = 11.69 , height = 8.27 , units = c("in", "cm", "mm")[1] , dpi = 600 , scale = 1 , device = grDevices::cairo_pdf , limitsize = TRUE ) mhp_save( plot = p , filename = tempfile(fileext = ".png") , path = NULL , width = 6 , height = 4 , units = c("in", "cm", "mm")[1] , dpi = 300 , scale = 1 , device = NULL , limitsize = TRUE )library(ggplot2) p <- ggplot(data = data.frame(x = 1:10, y = (1:10)^2), mapping = aes(x = x, y = y)) + geom_line() + mhp_theme( base_size = 11 , base_family = "serif" , ticks_inward = TRUE , legend_position = "bottom" ) f <- tempfile(fileext = ".pdf") mhp_save( plot = p , filename = f , path = NULL , width = 11.69 , height = 8.27 , units = c("in", "cm", "mm")[1] , dpi = 600 , scale = 1 , device = NULL , limitsize = TRUE ) mhp_save( plot = p , filename = basename(f) , path = tempdir() , width = 11.69 , height = 8.27 , units = c("in", "cm", "mm")[1] , dpi = 600 , scale = 1 , device = grDevices::cairo_pdf , limitsize = TRUE ) mhp_save( plot = p , filename = tempfile(fileext = ".png") , path = NULL , width = 6 , height = 4 , units = c("in", "cm", "mm")[1] , dpi = 300 , scale = 1 , device = NULL , limitsize = TRUE )
mhp_simulate() generates a series from the data generating process
of Hanif, Iqbal and Choudhary (2017, eqs. 10a and 10b), after Harvey and Jaeger (1993) and Guay and St-Amant
(2005): a random walk trend with drift and an AR(2) cycle,
with and . Quarterly series can be aggregated to annual ones as in the paper.
mhp_simulate( T = 200L, sd_ratio = 1, ar = c(1.2, -0.4), sd_cycle = 1, drift = 0.5, trend = c("linear", "nonlinear"), aggregate = c("none", "systematic", "sum", "average"), seed = NULL )mhp_simulate( T = 200L, sd_ratio = 1, ar = c(1.2, -0.4), sd_cycle = 1, drift = 0.5, trend = c("linear", "nonlinear"), aggregate = c("none", "systematic", "sum", "average"), seed = NULL )
T |
number of (quarterly) observations (default 200, as in the paper). |
sd_ratio |
|
ar |
|
sd_cycle |
|
drift |
|
trend |
|
aggregate |
|
seed |
seed of the random numbers ( |
The paper does not give the drift or the form of the nonlinear trend term . Here
for trend = "linear", and for trend = "nonlinear", a drift that rises and falls once over the sample. The cycle starts from a
burn-in of 100 values. Aggregation to annual data takes every fourth value ("systematic"), the sum of four
consecutive values ("sum") or their average ("average"), for the series and both components.
A data.table with the columns t, x, trend, cycle.
Muhammad Yaseen ([email protected])
Guay, A. and St-Amant, P. (2005). Do the Hodrick-Prescott and Baxter-King filters provide a good approximation of business cycles? Annales d'Economie et de Statistique, 77, 133–155. doi:10.2307/20079119
Harvey, A. C. and Jaeger, A. (1993). Detrending, stylized facts and the business cycle. Journal of Applied Econometrics, 8(3), 231–247. doi:10.1002/jae.3950080302
s <- mhp_simulate( T = 200 , sd_ratio = 1 , ar = c(1.2, -0.4) , sd_cycle = 1 , drift = 0.5 , trend = "linear" , aggregate = "none" , seed = 1 ) ss <- mhp_simulate( T = 200 , sd_ratio = 1 , ar = c(1.2, -0.4) , sd_cycle = 1 , drift = 0.5 , trend = "linear" , aggregate = "none" , seed = 1 ) s
The theme used by every plot() method of mhpfilter (identical in all packages of the
author): theme_classic with a serif font, black axis lines, ticks pointing inwards and no
grid, close to the look of TikZ/pgfplots figures in LaTeX documents. The plot() methods also use
scales::pretty_breaks(n = 10) on every continuous axis. Save figures with mhp_save.
mhp_theme( base_size = 11, base_family = "serif", ticks_inward = TRUE, legend_position = "bottom" )mhp_theme( base_size = 11, base_family = "serif", ticks_inward = TRUE, legend_position = "bottom" )
base_size |
base font size in points (default 11). |
base_family |
font family (default |
ticks_inward |
logical; ticks point into the panel, as in pgfplots (default |
legend_position |
position of the legend (default |
A ggplot2 theme.
Muhammad Yaseen ([email protected])
Other mhpfilter utilities:
mhp_notation(),
mhp_reference(),
mhp_save()
library(ggplot2) p <- ggplot(data = data.frame(x = 1:20, y = cumsum(sin(1:20))), mapping = aes(x = x, y = y)) + geom_line() p + mhp_theme( base_size = 11 , base_family = "serif" , ticks_inward = TRUE , legend_position = "bottom" ) p + mhp_theme( base_size = 12 , base_family = "serif" , ticks_inward = FALSE , legend_position = "right" ) + scale_x_continuous(breaks = scales::pretty_breaks(n = 10))library(ggplot2) p <- ggplot(data = data.frame(x = 1:20, y = cumsum(sin(1:20))), mapping = aes(x = x, y = y)) + geom_line() p + mhp_theme( base_size = 11 , base_family = "serif" , ticks_inward = TRUE , legend_position = "bottom" ) p + mhp_theme( base_size = 12 , base_family = "serif" , ticks_inward = FALSE , legend_position = "right" ) + scale_x_continuous(breaks = scales::pretty_breaks(n = 10))
The analysis functions of mhpfilter return the named list they have always returned, now with the class
c("mhp_<analysis>", "mhp_result", "list"). Components are read as before (x$ANOVA,
x[["Means"]]); the methods below print, summarise and plot the result and turn any component into a
data.table.
## S3 method for class 'mhp_result' print(x, digits = 4L, ...) ## S3 method for class 'mhp_result' summary(object, ...) ## S3 method for class 'summary.mhp_result' print(x, ...) ## S3 method for class 'mhp_result' format(x, ...) ## S3 method for class 'mhp_result' as.data.table(x, keep.rownames = FALSE, ..., component = 1L) ## S3 method for class 'mhp_result' as.data.frame(x, row.names = NULL, optional = FALSE, ..., component = 1L) ## S3 method for class 'mhp_result' plot(x, ...)## S3 method for class 'mhp_result' print(x, digits = 4L, ...) ## S3 method for class 'mhp_result' summary(object, ...) ## S3 method for class 'summary.mhp_result' print(x, ...) ## S3 method for class 'mhp_result' format(x, ...) ## S3 method for class 'mhp_result' as.data.table(x, keep.rownames = FALSE, ..., component = 1L) ## S3 method for class 'mhp_result' as.data.frame(x, row.names = NULL, optional = FALSE, ..., component = 1L) ## S3 method for class 'mhp_result' plot(x, ...)
x, object
|
a mhpfilter result. |
digits |
significant digits of printed numbers (default 4). |
... |
passed on (to |
keep.rownames, row.names, optional
|
as in |
component |
name or number of the component to convert (default: the first one). |
print() returns x invisibly; summary() an object of class
summary.mhp_result; format() a character string; as.data.table() and
as.data.frame() the chosen component as a table; plot() a ggplot object.
Muhammad Yaseen ([email protected])
plot_batch() draws the cycles (or trends) of a result of mhp_batch() or
fmhp_batch(), one panel per series or all in one panel, with the smoothing parameter of each series
in its label.
plot_batch(x, show = c("cycle", "trend"), facet = TRUE, highlight = NULL)plot_batch(x, show = c("cycle", "trend"), facet = TRUE, highlight = NULL)
x |
a result of |
show |
|
facet |
one panel per series (default |
highlight |
names of series drawn in full colour, the others faded (default |
Change from version 0.1.0. The faceted plot passed linewidth twice (a warning) and ignored
highlight; both are corrected.
A ggplot object.
Muhammad Yaseen ([email protected])
data(us_macro) mb <- mhp_batch( X = log(as.matrix(us_macro[, c("gdp", "consumption", "investment")])) , max_lambda = 100000 ) plot_batch( x = mb , show = "cycle" , facet = TRUE , highlight = NULL )data(us_macro) mb <- mhp_batch( X = log(as.matrix(us_macro[, c("gdp", "consumption", "investment")])) , max_lambda = 100000 ) plot_batch( x = mb , show = "cycle" , facet = TRUE , highlight = NULL )
plot_comparison() draws the series with the trends of the HP filter (at the conventional
of its frequency) and of the modified HP filter, and optionally the cycles; with
fmhp = TRUE the fully modified HP filter is added.
plot_comparison( x, frequency = c("quarterly", "annual", "monthly"), max_lambda = 100000L, show_cycle = FALSE, fmhp = FALSE )plot_comparison( x, frequency = c("quarterly", "annual", "monthly"), max_lambda = 100000L, show_cycle = FALSE, fmhp = FALSE )
x |
a numeric vector or a |
frequency |
|
max_lambda |
the largest |
show_cycle |
draw the cycles too (default |
fmhp |
add the fully modified HP filter (default |
A ggplot object.
Muhammad Yaseen ([email protected])
data(us_macro) plot_comparison( x = log(us_macro$gdp) , frequency = "quarterly" , max_lambda = 100000 , show_cycle = TRUE , fmhp = TRUE )data(us_macro) plot_comparison( x = log(us_macro$gdp) , frequency = "quarterly" , max_lambda = 100000 , show_cycle = TRUE , fmhp = TRUE )
print() of a result of hp_filter(), mhp_filter() or
fmhp_filter() (with as_dt = FALSE) shows the smoothing parameter, the criterion and summaries
of the cycle, as in version 0.1.0, and the sources of the method.
## S3 method for class 'mhp' print(x, ...)## S3 method for class 'mhp' print(x, ...)
x |
a result of |
... |
further arguments ( |
x, invisibly.
Muhammad Yaseen ([email protected])
data(us_macro) m <- mhp_filter( x = log(us_macro$gdp) , max_lambda = 100000 , as_dt = FALSE , log_scale = FALSE , criterion = c("approximate", "exact") ) print(x = m)data(us_macro) m <- mhp_filter( x = log(us_macro$gdp) , max_lambda = 100000 , as_dt = FALSE , log_scale = FALSE , criterion = c("approximate", "exact") ) print(x = m)
Quarterly real gross domestic product, real personal consumption expenditures and real gross private domestic investment of the United States from the first quarter of 1959 to the third quarter of 2009: the three series (income, consumption and investment) of the empirical analysis of Hanif, Iqbal and Choudhary (2017), for one country.
us_macrous_macro
A data frame with 203 rows and 5 columns:
yearyear, 1959 to 2009.
quarterquarter, 1 to 4.
gdpreal gross domestic product (billions of chained 2005 US dollars, seasonally adjusted annual rate).
consumptionreal personal consumption expenditures (same units).
investmentreal gross private domestic investment (same units).
Federal Reserve Economic Data (FRED), Federal Reserve Bank of St. Louis, accessed 15 December 2009, as
compiled in the data set macrodata of the Python library statsmodels (public domain).
Hanif, M. N., Iqbal, J. and Choudhary, M. A. (2017). Fully modified HP filter. SBP Working Paper Series, No. 88, State Bank of Pakistan.
data(us_macro) head(us_macro) gdp <- ts(data = us_macro$gdp, start = c(1959, 1), frequency = 4) plot(log(gdp))data(us_macro) head(us_macro) gdp <- ts(data = us_macro$gdp, start = c(1959, 1), frequency = 4) plot(log(gdp))