| Title: | Trend-Cycle Extraction with Linear Filters with 'JDemetra+ 3.x' |
|---|---|
| Description: | Build and apply symmetric and asymmetric moving averages (= linear filters) for trend-cycle extraction. In particular, it implements several modern approaches for real-time estimates from the viewpoint of revisions and time delay in detecting turning points. It includes the local polynomial approach of Proietti and Luati (2008), the Reproducing Kernel Hilbert Space (RKHS) of Dagum and Bianconcini (2008) and the Fidelity-Smoothness-Timeliness approach of Grun-Rehomme, Guggemos, and Ladiray (2018). It is based on 'Java' libraries developed in 'JDemetra+' (<https://github.com/jdemetra>), time series analysis software. |
| Authors: | Alain Quartier-la-Tente [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-7890-3857>), Jean Palate [aut], Tanguy Barthelemy [ctb], Anna Smyk [ctb] |
| Maintainer: | Alain Quartier-la-Tente <[email protected]> |
| License: | EUPL |
| Version: | 2.4.0.9000 |
| Built: | 2026-10-10 18:48:06 UTC |
| Source: | https://github.com/rjdverse/rjd3filters |
These functions are used in all JDemetra+ 3.0 packages to easily interact between R and Java objects.
.jd2r_finitefilters(jf).jd2r_finitefilters(jf)
jf |
Java linear filter object. |
.jd2r_finitefilters() returns a finite_filters object.
Confidence intervals
confint_filter( x, coef, coef_var = coef, level = 0.95, asymmetric_var = TRUE, gaussian_distribution = FALSE, exact_df = TRUE, ... )confint_filter( x, coef, coef_var = coef, level = 0.95, asymmetric_var = TRUE, gaussian_distribution = FALSE, exact_df = TRUE, ... )
x |
input time series. |
coef |
moving-average ( |
coef_var |
moving-average ( |
level |
confidence level. |
asymmetric_var |
if |
gaussian_distribution |
if |
exact_df |
if |
... |
other arguments passed to the function |
Let be a moving average of length
used to filter a time series .
Let denote the filtered series computed at time as:
If is unbiased, a approximate confidence for the true mean
is:
where is the quantile of the standard
normal distribution.
The estimate of the variance is obtained using
var_estimator() with the parameter coef_var.
The assumption that is unbiased is rarely exactly true, so
variance estimates and confidence intervals are usually computed at small
bandwidths where bias is small.
When coef (or coef_var) is a finite filter, the last points of the
confidence interval are computed using the corresponding asymmetric filters
A matrix or mts object with the filtered series and the lower and upper bounds of the confidence interval.
Loader, Clive. 1999. Local regression and likelihood. New York: Springer-Verlag.
x <- retailsa$DrinkingPlaces coef <- lp_filter(6) confint <- confint_filter(x, coef) graphics::plot(confint, plot.type = "single", col = c("red", "black", "black"), lty = c(1, 2, 2))x <- retailsa$DrinkingPlaces coef <- lp_filter(6) confint <- confint_filter(x, coef) graphics::plot(confint, plot.type = "single", col = c("red", "black", "black"), lty = c(1, 2, 2))
Compute the degrees of freedom for the variance estimator
df_var(n, coef, exact_df = FALSE)df_var(n, coef, exact_df = FALSE)
n |
number of observations |
coef |
moving average ( |
exact_df |
if |
Numeric value with the statistic (the degree of freedom is not necessary an integer).
df_var(n = 100, coef = lp_filter(6)@sfilter)df_var(n = 100, coef = lp_filter(6)@sfilter)
Direct Filter Approach
dfa_filter( horizon = 6, degree = 0, density = c("uniform", "rw"), targetfilter = lp_filter(horizon = horizon)@sfilter, passband = 2 * pi/12, accuracy.weight = 1/3, smoothness.weight = 1/3, timeliness.weight = 1/3 )dfa_filter( horizon = 6, degree = 0, density = c("uniform", "rw"), targetfilter = lp_filter(horizon = horizon)@sfilter, passband = 2 * pi/12, accuracy.weight = 1/3, smoothness.weight = 1/3, timeliness.weight = 1/3 )
horizon |
horizon (bandwidth) of the symmetric filter. |
degree |
degree of polynomial. |
density |
hypothesis on the spectral density: |
targetfilter |
the weights of the symmetric target filters (by default the Henderson filter). |
passband |
passband threshold. |
accuracy.weight, smoothness.weight, timeliness.weight
|
the weight used for the optimisation. The weight associated to the residual is derived so that the sum of the four weights are equal to 1. |
Moving average computed by a minimisation of a weighted mean of three criteria under polynomials constraints. The criteria come from the decomposition of the mean squared error between th trend-cycle
Let be a moving average where
and are two integers defined by the parameter lags and leads.
The three criteria are:
A finite_filters() object.
dfa_filter(horizon = 6, degree = 0) dfa_filter(horizon = 6, degree = 2)dfa_filter(horizon = 6, degree = 0) dfa_filter(horizon = 6, degree = 2)
Function to compute a diagnostic matrix of quality criteria for asymmetric filters.
diagnostic_matrix(x, lags, passband = pi/6, sweights = NULL, ...)diagnostic_matrix(x, lags, passband = pi/6, sweights = NULL, ...)
x |
|
lags |
Lags of the filter (should be positive), only used if |
passband |
passband threshold. |
sweights |
Weights of the symmetric filter (from 0 to lags or -lags to
lags).
If missing, the criteria from the functions |
... |
optional arguments to |
For a moving average of coefficients
diagnostic_matrix returns a vector with the following ten criteria:
b_c Constant bias (if , preserve constant trends)
b_l Linear bias (if , preserve constant trends)
b_q Quadratic bias (if , preserve quadratic trends)
F_g Fidelity criterium of Grun-Rehomme et al (2018)
S_g Smoothness criterium of Grun-Rehomme et al (2018)
T_g Timeliness criterium of Grun-Rehomme et al (2018)
A_w Accuracy criterium of Wildi and McElroy (2019)
S_w Smoothness criterium of Wildi and McElroy (2019)
T_w Timeliness criterium of Wildi and McElroy (2019)
R_w Residual criterium of Wildi and McElroy (2019)
A_w, S_w, T_w and R_w are computed only if the weights of the symmetric filter are provided (sweights).
A vector of length 10 with the quality criteria.
Grun-Rehomme, Michel, Fabien Guggemos, and Dominique Ladiray (2018). “Asymmetric Moving Averages Minimizing Phase Shift”. In: Handbook on Seasonal Adjustment.
Wildi, Marc and McElroy, Tucker (2019). “The trilemma between accuracy, timeliness and smoothness in real-time signal extraction”. In: International Journal of Forecasting 35.3, pp. 1072–1084.
filter <- lp_filter(horizon = 6, kernel = "Henderson", endpoints = "LC") sweights <- filter[, "q=6"] aweights <- filter[, "q=0"] diagnostic_matrix(aweights, sweights = sweights)filter <- lp_filter(horizon = 6, kernel = "Henderson", endpoints = "LC") sweights <- filter[, "q=6"] aweights <- filter[, "q=0"] diagnostic_matrix(aweights, sweights = sweights)
Set of functions to compute diagnostics and goodness of fit of filtered series:
cross validation (cv()) and cross validate estimate (cve()),
leave-one-out cross validation estimate (loocve),
CP statistic (cp()) and Rice's T statistics (rt()).
cve(x, coef, ...) cv(x, coef, ...) loocve(x, coef, ...) rt(x, coef, ...) cp(x, coef, var, ...)cve(x, coef, ...) cv(x, coef, ...) loocve(x, coef, ...) rt(x, coef, ...) cp(x, coef, var, ...)
x |
input time series. |
coef |
vector of coefficients or a moving-average ( |
... |
other arguments passed to the function |
var |
variance used to compute the CP statistic ( |
Let be a moving average of length used
to filter a time series .
Let denote the filtered series computed at time as:
The cross validation estimate (cve()) is defined as the time series where
is the leave-one-out cross validation estimate (loocve()) defined as the filtered series
computed deleting the observation and remaining all the other points.
The cross validation statistics (cv()) is defined as:
In the case of filtering with a moving average, we can show that:
and
In the case of filtering with a moving average,
the CP estimate of risk (introduced by Mallows (1973); cp()) can be defined as:
The CP method requires an estimate of (var parameter).
The usual use of CP is to compare several different fits (for example different bandwidths):
one should use the same estimate of for all fits (using for example var_estimator()).
The recommendation of Cleveland and Devlin (1988) is to compute
from a fit at the smallest bandwidth under consideration,
at which one should be willing to assume that bias is negligible.
The Rice's T statistic (rt()) is defined as:
Numeric value for the statistic and ts() or vector() for the estimates.
Loader, Clive. 1999. Local regression and likelihood. New York: Springer-Verlag.
Mallows, C. L. (1973). Some comments on Cp. Technometrics 15, 661– 675.
Cleveland, W. S. and S. J. Devlin (1988). Locally weighted regression: An approach to regression analysis by local fitting. Journal of the American Statistical Association 83, 596–610.
x <- retailsa$DrinkingPlaces h13 <- lp_filter(6)@sfilter cv(x, h13) cve(x, h13) loocve(x, h13) rt(x, h13) cp(x, h13, var = var_estimator(x, h13))x <- retailsa$DrinkingPlaces h13 <- lp_filter(6)@sfilter cv(x, h13) cve(x, h13) loocve(x, h13) rt(x, h13) cp(x, h13, var = var_estimator(x, h13))
Applies linear filtering to a univariate time series or to each series separately of a multivariate time series using either a moving average (symmetric or asymmetric) or a combination of symmetric moving average at the center and asymmetric moving averages at the bounds.
filter(x, coefs, remove_missing = TRUE)filter(x, coefs, remove_missing = TRUE)
x |
a univariate or multivariate time series. |
coefs |
a |
remove_missing |
if |
The functions filter extends filter allowing to
apply every kind of moving averages (symmetric and asymmetric filters) or to
apply a set of multiple moving averages to deal with the boundaries.
Let be the input time series to filter.
If coef is an object moving_average(), of length , the result
is equal at time to:
.
It extends the function filter that would add NA
at the end of the time series.
If coef is a matrix, list or finite_filters() object, at the
center, the symmetric moving average is used (first column/element of
coefs).
At the boundaries, the last moving average of coefs is used to
compute the filtered time series (no future point known), the
second to last to compute the filtered time series (one future
point known)...
An object of the same class as x (ts, mts, vector or matrix) with the filtered time series.
x <- retailsa$DrinkingPlaces lags <- 6 leads <- 2 fst_coef <- fst_filter(lags = lags, leads = leads, smoothness.weight = 0.3, timeliness.weight = 0.3) lpp_coef <- lp_filter(horizon = lags, kernel = "Henderson", endpoints = "LC") fst_ma <- filter(x, fst_coef) lpp_ma <- filter(x, lpp_coef[,"q=2"]) graphics::plot(stats::ts.union(x, fst_ma, lpp_ma), plot.type = "single", col = c("black","red","blue")) trend <- filter(x, lpp_coef) # This is equivalent to: trend <- localpolynomials(x, horizon = 6)x <- retailsa$DrinkingPlaces lags <- 6 leads <- 2 fst_coef <- fst_filter(lags = lags, leads = leads, smoothness.weight = 0.3, timeliness.weight = 0.3) lpp_coef <- lp_filter(horizon = lags, kernel = "Henderson", endpoints = "LC") fst_ma <- filter(x, fst_coef) lpp_ma <- filter(x, lpp_coef[,"q=2"]) graphics::plot(stats::ts.union(x, fst_ma, lpp_ma), plot.type = "single", col = c("black","red","blue")) trend <- filter(x, lpp_coef) # This is equivalent to: trend <- localpolynomials(x, horizon = 6)
Manipulation of moving_average() or finite_filters() objects.
## S3 method for class 'moving_average' sum(..., na.rm = FALSE) ## S4 method for signature 'moving_average,numeric' x[i] ## S4 method for signature 'moving_average,logical' x[i] ## S4 replacement method for signature 'moving_average,ANY,missing,numeric' x[i] <- value ## S3 method for class 'moving_average' cbind(..., zero_as_na = FALSE) ## S3 method for class 'moving_average' rbind(...) ## S4 method for signature 'moving_average,moving_average' e1 + e2 ## S4 method for signature 'moving_average,numeric' e1 + e2 ## S4 method for signature 'numeric,moving_average' e1 + e2 ## S4 method for signature 'moving_average,missing' e1 + e2 ## S4 method for signature 'moving_average,missing' e1 - e2 ## S4 method for signature 'moving_average,moving_average' e1 - e2 ## S4 method for signature 'moving_average,numeric' e1 - e2 ## S4 method for signature 'numeric,moving_average' e1 - e2 ## S4 method for signature 'moving_average,moving_average' e1 * e2 ## S4 method for signature 'moving_average,numeric' e1 * e2 ## S4 method for signature 'numeric,moving_average' e1 * e2 ## S4 method for signature 'ANY,moving_average' e1 * e2 ## S4 method for signature 'moving_average,ANY' e1 * e2 ## S4 method for signature 'moving_average,numeric' e1 / e2 ## S4 method for signature 'moving_average,numeric' e1 ^ e2 ## S4 method for signature 'finite_filters,moving_average' e1 * e2 ## S4 method for signature 'moving_average,finite_filters' e1 * e2 ## S4 method for signature 'finite_filters,numeric' e1 * e2 ## S4 method for signature 'ANY,finite_filters' e1 * e2 ## S4 method for signature 'finite_filters,ANY' e1 * e2 ## S4 method for signature 'numeric,finite_filters' e1 + e2 ## S4 method for signature 'finite_filters,moving_average' e1 + e2 ## S4 method for signature 'moving_average,finite_filters' e1 + e2 ## S4 method for signature 'finite_filters,missing' e1 + e2 ## S4 method for signature 'finite_filters,missing' e1 - e2 ## S4 method for signature 'finite_filters,moving_average' e1 - e2 ## S4 method for signature 'moving_average,finite_filters' e1 - e2 ## S4 method for signature 'finite_filters,numeric' e1 - e2 ## S4 method for signature 'numeric,finite_filters' e1 - e2 ## S4 method for signature 'finite_filters,numeric' e1 / e2 ## S4 method for signature 'finite_filters,numeric' e1 ^ e2 ## S4 method for signature 'finite_filters,finite_filters' e1 * e2 ## S4 method for signature 'finite_filters,finite_filters' e1 + e2 ## S4 method for signature 'finite_filters,finite_filters' e1 - e2 ## S4 method for signature 'finite_filters,missing' x[i, j, ..., drop = TRUE] ## S4 method for signature 'finite_filters,ANY' x[i, j, ..., drop = TRUE]## S3 method for class 'moving_average' sum(..., na.rm = FALSE) ## S4 method for signature 'moving_average,numeric' x[i] ## S4 method for signature 'moving_average,logical' x[i] ## S4 replacement method for signature 'moving_average,ANY,missing,numeric' x[i] <- value ## S3 method for class 'moving_average' cbind(..., zero_as_na = FALSE) ## S3 method for class 'moving_average' rbind(...) ## S4 method for signature 'moving_average,moving_average' e1 + e2 ## S4 method for signature 'moving_average,numeric' e1 + e2 ## S4 method for signature 'numeric,moving_average' e1 + e2 ## S4 method for signature 'moving_average,missing' e1 + e2 ## S4 method for signature 'moving_average,missing' e1 - e2 ## S4 method for signature 'moving_average,moving_average' e1 - e2 ## S4 method for signature 'moving_average,numeric' e1 - e2 ## S4 method for signature 'numeric,moving_average' e1 - e2 ## S4 method for signature 'moving_average,moving_average' e1 * e2 ## S4 method for signature 'moving_average,numeric' e1 * e2 ## S4 method for signature 'numeric,moving_average' e1 * e2 ## S4 method for signature 'ANY,moving_average' e1 * e2 ## S4 method for signature 'moving_average,ANY' e1 * e2 ## S4 method for signature 'moving_average,numeric' e1 / e2 ## S4 method for signature 'moving_average,numeric' e1 ^ e2 ## S4 method for signature 'finite_filters,moving_average' e1 * e2 ## S4 method for signature 'moving_average,finite_filters' e1 * e2 ## S4 method for signature 'finite_filters,numeric' e1 * e2 ## S4 method for signature 'ANY,finite_filters' e1 * e2 ## S4 method for signature 'finite_filters,ANY' e1 * e2 ## S4 method for signature 'numeric,finite_filters' e1 + e2 ## S4 method for signature 'finite_filters,moving_average' e1 + e2 ## S4 method for signature 'moving_average,finite_filters' e1 + e2 ## S4 method for signature 'finite_filters,missing' e1 + e2 ## S4 method for signature 'finite_filters,missing' e1 - e2 ## S4 method for signature 'finite_filters,moving_average' e1 - e2 ## S4 method for signature 'moving_average,finite_filters' e1 - e2 ## S4 method for signature 'finite_filters,numeric' e1 - e2 ## S4 method for signature 'numeric,finite_filters' e1 - e2 ## S4 method for signature 'finite_filters,numeric' e1 / e2 ## S4 method for signature 'finite_filters,numeric' e1 ^ e2 ## S4 method for signature 'finite_filters,finite_filters' e1 * e2 ## S4 method for signature 'finite_filters,finite_filters' e1 + e2 ## S4 method for signature 'finite_filters,finite_filters' e1 - e2 ## S4 method for signature 'finite_filters,missing' x[i, j, ..., drop = TRUE] ## S4 method for signature 'finite_filters,ANY' x[i, j, ..., drop = TRUE]
..., drop, na.rm
|
other parameters. |
x, e1, e2
|
object. |
i, j, value
|
indices specifying elements to extract or replace and the new value |
zero_as_na |
boolean indicating if, when merging several moving averages ( |
A moving_average() or finite_filters() object resulting from the operation.
e1 <- moving_average(rep(1,12), lags = -6) e1 <- e1/sum(e1) e2 <- moving_average(rep(1/12, 12), lags = -5) M2X12 <- (e1 + e2)/2 M2X12 h13 <- lp_filter(horizon = 6) h13 h13 * M2X12e1 <- moving_average(rep(1,12), lags = -6) e1 <- e1/sum(e1) e2 <- moving_average(rep(1/12, 12), lags = -5) M2X12 <- (e1 + e2)/2 M2X12 h13 <- lp_filter(horizon = 6) h13 h13 * M2X12
Finite filters are a set of moving averages used to compute the filtered values of a time series. The symmetric filter is used on the central points (i.e. to obtain final estimates), while the right and left filters are used on the boundaries (at the end and beginning of the series) when the symmetric filter cannot be applied due to lack of data.
finite_filters( sfilter, rfilters = NULL, lfilters = NULL, first_to_last = FALSE ) is.finite_filters(x) ## S3 method for class 'finite_filters' to_seasonal(x, s)finite_filters( sfilter, rfilters = NULL, lfilters = NULL, first_to_last = FALSE ) is.finite_filters(x) ## S3 method for class 'finite_filters' to_seasonal(x, s)
sfilter |
the symmetric filter ( |
rfilters |
the right filters (used on the last points). |
lfilters |
the left filters (used on the first points). |
first_to_last |
boolean indicating if the first element of |
x |
object to test the class. |
s |
seasonal period for the |
finite_filters() returns a "finite_filters" object, which is an S4 class with three slots:
sfilter: the symmetric filter (moving_average() object) used on the central points.
rfilters: a list of moving_average() objects used on the boundaries at the end of the series (when the symmetric filter cannot be applied due to missing future data).
lfilters: a list of moving_average() objects used on the boundaries at the beginning of the series (when the symmetric filter cannot be applied due to missing past data).
is.finite_filters() returns TRUE if the object is a "finite_filters" object, FALSE otherwise.
ff_lp <- lp_filter() ff_simple_ma <- finite_filters(moving_average(c(1, 1, 1), lags = -1)/3, rfilters = list(moving_average(c(1, 1), lags = -1)/2)) ff_lp ff_simple_ma ff_lp * ff_simple_ma # To get the symmetric filter and the right filters: ff_lp@sfilter ff_lp@rfiltersff_lp <- lp_filter() ff_simple_ma <- finite_filters(moving_average(c(1, 1, 1), lags = -1)/3, rfilters = list(moving_average(c(1, 1), lags = -1)/2)) ff_lp ff_simple_ma ff_lp * ff_simple_ma # To get the symmetric filter and the right filters: ff_lp@sfilter ff_lp@rfilters
Compute the Fidelity, Smoothness and Timeliness (FST) criteria
fst(weights, lags, passband = pi/6, ...)fst(weights, lags, passband = pi/6, ...)
weights |
either a |
lags |
Lags of the moving average (when |
passband |
Passband threshold for timeliness criterion. |
... |
other unused arguments. |
A named vector with the values of the 3 criteria.
Grun-Rehomme, Michel, Fabien Guggemos, and Dominique Ladiray (2018). “Asymmetric Moving Averages Minimizing Phase Shift”. In: Handbook on Seasonal Adjustment, https://ec.europa.eu/eurostat/web/products-manuals-and-guidelines/-/ks-gq-18-001.
filter <- lp_filter(horizon = 6, kernel = "Henderson", endpoints = "LC") fst(filter[, "q=0"]) # To compute the statistics on all filters: fst(filter)filter <- lp_filter(horizon = 6, kernel = "Henderson", endpoints = "LC") fst(filter[, "q=0"]) # To compute the statistics on all filters: fst(filter)
Estimation of a filter using the Fidelity-Smoothness-Timeliness criteria
fst_filter( lags = 6, leads = 0, pdegree = 2, smoothness.weight = 1, smoothness.degree = 3, timeliness.weight = 0, timeliness.passband = pi/6, timeliness.antiphase = TRUE )fst_filter( lags = 6, leads = 0, pdegree = 2, smoothness.weight = 1, smoothness.degree = 3, timeliness.weight = 0, timeliness.passband = pi/6, timeliness.antiphase = TRUE )
lags |
Lags of the filter (should be positive). |
leads |
Leads of the filter (should be positive or 0). |
pdegree |
Local polynomials preservation: max degree. |
smoothness.weight |
Weight for the smoothness criterion (in |
smoothness.degree |
Degree of the smoothness criterion (3 for Henderson). |
timeliness.weight |
Weight for the Timeliness criterion (in |
timeliness.passband |
Passband for the timeliness criterion (in radians). The phase effect is computed in |
timeliness.antiphase |
boolean indicating if the timeliness should be computed analytically ( |
Moving average computed by a minimisation of a weighted mean of three criteria under polynomials constraints.
Let be a moving average where
and are two integers defined by the parameter lags and leads.
The three criteria are:
Fidelity, : it's the variance reduction ratio.
Smoothness, : it measures the flexibility of the coefficient curve of a filter and the smoothness of the trend.
The integer is defined by parameter smoothness.degree.
By default, the Henderson criteria is used (smoothness.degree = 3).
Timeliness, :
with and the gain and phase shift functions
of , and a penalty function defined as
to have an analytically solvable criterium.
is defined by the parameter timeliness.passband and is it
by default equal to : for monthly time series, we focus on the timeliness associated to
cycles of 12 months or more.
The moving average is then computed solving the problem:
Where represents linear constraints to have a moving average
that preserve polynomials of degree (pdegree):
A finite_filters() object.
Grun-Rehomme, Michel, Fabien Guggemos, and Dominique Ladiray (2018). “Asymmetric Moving Averages Minimizing Phase Shift”. In: Handbook on Seasonal Adjustment, https://ec.europa.eu/eurostat/web/products-manuals-and-guidelines/-/ks-gq-18-001.
filter <- fst_filter(lags = 6, leads = 0) filterfilter <- fst_filter(lags = 6, leads = 0) filter
Function to get the coefficient associated to a kernel. Those coefficients are then used to compute the different filters.
get_kernel( kernel = c("Henderson", "Uniform", "Triangular", "Epanechnikov", "Parabolic", "BiWeight", "TriWeight", "Tricube", "Trapezoidal", "Gaussian"), horizon, sd_gauss = 0.25 )get_kernel( kernel = c("Henderson", "Uniform", "Triangular", "Epanechnikov", "Parabolic", "BiWeight", "TriWeight", "Tricube", "Trapezoidal", "Gaussian"), horizon, sd_gauss = 0.25 )
kernel |
kernel uses. |
horizon |
horizon (bandwidth) of the symmetric filter. |
sd_gauss |
standard deviation for gaussian kernel. By default 0.25. |
tskernel object (see kernel).
get_kernel("Henderson", horizon = 3)get_kernel("Henderson", horizon = 3)
Get Moving Averages from ARIMA model
get_moving_average(x, ...)get_moving_average(x, ...)
x |
the object. |
... |
unused parameters |
A moving_average() object.
fit <- stats::arima(log10(AirPassengers), c(0, 1, 1), seasonal = list(order = c(0, 1, 1), period = 12)) get_moving_average(fit)fit <- stats::arima(log10(AirPassengers), c(0, 1, 1), seasonal = list(order = c(0, 1, 1), period = 12)) get_moving_average(fit)
Get properties of filters
get_properties_function( x, component = c("Symmetric Gain", "Symmetric Phase", "Symmetric transfer", "Asymmetric Gain", "Asymmetric Phase", "Asymmetric transfer"), ... )get_properties_function( x, component = c("Symmetric Gain", "Symmetric Phase", "Symmetric transfer", "Asymmetric Gain", "Asymmetric Phase", "Asymmetric transfer"), ... )
x |
a |
component |
the component to extract. |
... |
unused other arguments. |
A function that takes a frequency as input and returns the value of the selected component.
filter <- lp_filter(3, kernel = "Henderson") sgain <- get_properties_function(filter, "Symmetric Gain") graphics::plot(sgain, xlim= c(0, pi/12))filter <- lp_filter(3, kernel = "Henderson") sgain <- get_properties_function(filter, "Symmetric Gain") graphics::plot(sgain, xlim= c(0, pi/12))
Function to retrieve the implicit forecasts corresponding to the asymmetric filters
implicit_forecasts(x, coefs)implicit_forecasts(x, coefs)
x |
a univariate or multivariate time series. |
coefs |
a |
Let be the bandwidth of the symmetric filter,
the coefficients of the symmetric filter and
the coefficients of the asymmetric filter used to estimate
the trend when future values are known (with the convention ).
Let denote the last available values of the input times series.
The implicit forecast induced by are defined by:
which is equivalent to
Note that this is solved numerically: the solution isn't exact.
An object of the same class as x (ts, mts, vector or matrix) with the implicit forecasts.
x <- retailsa$AllOtherGenMerchandiseStores ql <- lp_filter(horizon = 6, kernel = "Henderson", endpoints = "QL") lc <- lp_filter(horizon = 6, kernel = "Henderson", endpoints = "LC") f_ql <- implicit_forecasts(x, ql) f_lc <- implicit_forecasts(x, lc) graphics::plot(window(x, start = 2007), xlim = c(2007,2012)) graphics::lines( x = stats::ts( c(utils::tail(x,1), f_ql), frequency = stats::frequency(x), start = stats::end(x) ), col = "red", lty = 2 ) graphics::lines( x = stats::ts(c(utils::tail(x,1), f_lc), frequency = stats::frequency(x), start = stats::end(x) ), col = "blue", lty = 2 )x <- retailsa$AllOtherGenMerchandiseStores ql <- lp_filter(horizon = 6, kernel = "Henderson", endpoints = "QL") lc <- lp_filter(horizon = 6, kernel = "Henderson", endpoints = "LC") f_ql <- implicit_forecasts(x, ql) f_lc <- implicit_forecasts(x, lc) graphics::plot(window(x, start = 2007), xlim = c(2007,2012)) graphics::lines( x = stats::ts( c(utils::tail(x,1), f_ql), frequency = stats::frequency(x), start = stats::end(x) ), col = "red", lty = 2 ) graphics::lines( x = stats::ts(c(utils::tail(x,1), f_lc), frequency = stats::frequency(x), start = stats::end(x) ), col = "blue", lty = 2 )
Impute Incomplete Finite Filters
impute_last_obs(x, n = NULL, nperiod = 1, backward = TRUE, forward = TRUE)impute_last_obs(x, n = NULL, nperiod = 1, backward = TRUE, forward = TRUE)
x |
a |
n |
integer specifying the number of imputed periods. By default all the missing moving averages are imputed. |
nperiod |
integer specifying how to impute missing date.
|
backward, forward
|
boolean indicating if the imputation should be done backward (on left filters), forward (on right filters). |
When combining finite filters and a moving average, the first and/or the last points cannot be computed.
For example, using the M2X12 moving average, that is to say the symmetric moving average with coefficients
the first and last 6 points cannot be computed.
impute_last_obs() allows to impute the first/last points using the
nperiod previous filtered data. With nperiod = 1, the last filtered data
is used for the imputation, with nperiod = 12 and monthly data, the last
year filtered data is used for the imputation, etc.
A finite_filters() object.
y <- window(retailsa$AllOtherGenMerchandiseStores, start = 2008) M3 <- moving_average(rep(1/3, 3), lags = -1) M3X3 <- M3 * M3 M2X12 <- (simple_ma(12, -6) + simple_ma(12, -5)) / 2 composite_ma <- M3X3 * M2X12 # The last 6 points cannot be computed composite_ma composite_ma * y # they can be computed using the last filtered data # e.g. to impute the first 3 missing months with last period: impute_last_obs(composite_ma, n = 3, nperiod = 1) * y # or using the filtered data of the same month in previous year impute_last_obs(composite_ma, n = 6, nperiod = 12) * yy <- window(retailsa$AllOtherGenMerchandiseStores, start = 2008) M3 <- moving_average(rep(1/3, 3), lags = -1) M3X3 <- M3 * M3 M2X12 <- (simple_ma(12, -6) + simple_ma(12, -5)) / 2 composite_ma <- M3X3 * M2X12 # The last 6 points cannot be computed composite_ma composite_ma * y # they can be computed using the last filtered data # e.g. to impute the first 3 missing months with last period: impute_last_obs(composite_ma, n = 3, nperiod = 1) * y # or using the filtered data of the same month in previous year impute_last_obs(composite_ma, n = 6, nperiod = 12) * y
Apply Local Polynomials Filters
localpolynomials( x, horizon = 6, degree = 3, kernel = c("Henderson", "Uniform", "Biweight", "Trapezoidal", "Triweight", "Tricube", "Gaussian", "Triangular", "Parabolic"), endpoints = c("LC", "QL", "CQ", "CC", "DAF"), ic = 3.5, tweight = 0, passband = pi/12 )localpolynomials( x, horizon = 6, degree = 3, kernel = c("Henderson", "Uniform", "Biweight", "Trapezoidal", "Triweight", "Tricube", "Gaussian", "Triangular", "Parabolic"), endpoints = c("LC", "QL", "CQ", "CC", "DAF"), ic = 3.5, tweight = 0, passband = pi/12 )
x |
input time-series. |
horizon |
horizon (bandwidth) of the symmetric filter. |
degree |
degree of polynomial. |
kernel |
kernel uses. |
endpoints |
method for endpoints. |
ic |
ic ratio. |
tweight |
timeliness weight. |
passband |
passband threshold. |
A vector or ts object with the filtered time series.
Proietti, Tommaso and Alessandra Luati (2008). “Real time estimation in local polynomial regression, with application to trend-cycle analysis”.
x <- retailsa$AllOtherGenMerchandiseStores trend <- localpolynomials(x, horizon = 6) graphics::plot(x) graphics::lines(trend, col = "red")x <- retailsa$AllOtherGenMerchandiseStores trend <- localpolynomials(x, horizon = 6) graphics::plot(x) graphics::lines(trend, col = "red")
Local Polynomials Filters
lp_filter( horizon = 6, degree = 3, kernel = c("Henderson", "Uniform", "Biweight", "Trapezoidal", "Triweight", "Tricube", "Gaussian", "Triangular", "Parabolic"), endpoints = c("LC", "QL", "CQ", "CC", "DAF", "CN"), ic = 3.5, tweight = 0, passband = pi/12 )lp_filter( horizon = 6, degree = 3, kernel = c("Henderson", "Uniform", "Biweight", "Trapezoidal", "Triweight", "Tricube", "Gaussian", "Triangular", "Parabolic"), endpoints = c("LC", "QL", "CQ", "CC", "DAF", "CN"), ic = 3.5, tweight = 0, passband = pi/12 )
horizon |
horizon (bandwidth) of the symmetric filter. |
degree |
degree of polynomial. |
kernel |
kernel uses. |
endpoints |
method for endpoints. |
ic |
ic ratio. |
tweight |
timeliness weight. |
passband |
passband threshold. |
"LC": Linear-Constant filter
"QL": Quadratic-Linear filter
"CQ": Cubic-Quadratic filter
"CC": Constant-Constant filter
"DAF": Direct Asymmetric filter
"CN": Cut and Normalized Filter
a finite_filters() object.
A finite_filters() object.
Proietti, Tommaso and Alessandra Luati (2008). “Real time estimation in local polynomial regression, with application to trend-cycle analysis”.
mmsre_filter() localpolynomials().
henderson_f <- lp_filter(horizon = 6, kernel = "Henderson") plot_coef(henderson_f)henderson_f <- lp_filter(horizon = 6, kernel = "Henderson") plot_coef(henderson_f)
Provides an asymmetric filter based on the given reference filter (usually symmetric) minimizing the mean square revision error.
mmsre_filter( ref_filter, q, U, Z = NULL, delta = NULL, kernel = NULL, tweight = 0, passband = pi/12 )mmsre_filter( ref_filter, q, U, Z = NULL, delta = NULL, kernel = NULL, tweight = 0, passband = pi/12 )
ref_filter |
The reference filter (a |
q |
The horizon of the asymmetric filter. |
U |
Matrix of the constraints. |
Z |
Matrix of the bias (can be |
delta |
Coefficients of the linear model. |
kernel |
The kernel used for weighting factors, by default, no weight is used.
See |
tweight |
timeliness weight. |
passband |
passband threshold. |
The asymmetric filter minimizes the mean square revision error
(mmsre) relative to the reference filter .
The series follows the model
With a set of weights (kernel), by default (kernel = NULL) no weight is used.
The matrix represents the constraints of the symmetric filter (usually polynomials preservations), ,
imposed to the asymmetric filter, .
Partitionning the matrix
with the first rows and the remaining rows, the constraints are
.
The matrix represents the bias of the asymmetric filter: usually constraints imposed to the symmetric filter but not to the asymmetric filter.
A moving_average() object.
Proietti, Tommaso and Alessandra Luati (2008). “Real time estimation in local polynomial regression, with application to trend-cycle analysis”.
QL <- lp_filter(endpoints = "QL", ic = 3.5) LC <- lp_filter(endpoints = "LC", ic = 3.5) DAF <- lp_filter(endpoints = "DAF") h6 <- QL[, "q=6"] # To reproduce DAF filter mmsre_filter( ref_filter = h6, q = 0, U = polynomial_matrix(l = - 6, d0 = 0, d1 = 3), kernel = "Henderson" ) DAF[, "q=0"] # To reproduce QL filter mmsre_filter( ref_filter = h6, q = 1, delta = 2 / (sqrt(pi) * 3.5), U = polynomial_matrix(l = -6, d0 = 0, d1 = 1), Z = polynomial_matrix(l = -6, d0 = 2, d1 = 2) ) QL[, "q=1"] # Or using the Uniform kernel mmsre_filter( ref_filter = h6, q = 2, # we multiply by the square root of the inverse of weights (1/13) # to get the same result as the QL filter delta = 2 / (sqrt(pi) * 3.5) * (sqrt(13)), U = polynomial_matrix(l = -6, d0 = 0, d1 = 0), Z = polynomial_matrix(l = -6, d0 = 1, d1 = 1), kernel = "Uniform" ) LC[, "q=2"]QL <- lp_filter(endpoints = "QL", ic = 3.5) LC <- lp_filter(endpoints = "LC", ic = 3.5) DAF <- lp_filter(endpoints = "DAF") h6 <- QL[, "q=6"] # To reproduce DAF filter mmsre_filter( ref_filter = h6, q = 0, U = polynomial_matrix(l = - 6, d0 = 0, d1 = 3), kernel = "Henderson" ) DAF[, "q=0"] # To reproduce QL filter mmsre_filter( ref_filter = h6, q = 1, delta = 2 / (sqrt(pi) * 3.5), U = polynomial_matrix(l = -6, d0 = 0, d1 = 1), Z = polynomial_matrix(l = -6, d0 = 2, d1 = 2) ) QL[, "q=1"] # Or using the Uniform kernel mmsre_filter( ref_filter = h6, q = 2, # we multiply by the square root of the inverse of weights (1/13) # to get the same result as the QL filter delta = 2 / (sqrt(pi) * 3.5) * (sqrt(13)), U = polynomial_matrix(l = -6, d0 = 0, d1 = 0), Z = polynomial_matrix(l = -6, d0 = 1, d1 = 1), kernel = "Uniform" ) LC[, "q=2"]
Manipulation of moving averages
moving_average( x, lags = -length(x), trailing_zero = FALSE, leading_zero = FALSE ) is.moving_average(x) ## S3 method for class 'moving_average' coef(object, ...) is_symmetric(x) upper_bound(x) lower_bound(x) mirror(x) ## S3 method for class 'moving_average' rev(x) ## S3 method for class 'moving_average' length(x) to_seasonal(x, s) ## S4 method for signature 'moving_average' show(object)moving_average( x, lags = -length(x), trailing_zero = FALSE, leading_zero = FALSE ) is.moving_average(x) ## S3 method for class 'moving_average' coef(object, ...) is_symmetric(x) upper_bound(x) lower_bound(x) mirror(x) ## S3 method for class 'moving_average' rev(x) ## S3 method for class 'moving_average' length(x) to_seasonal(x, s) ## S4 method for signature 'moving_average' show(object)
x |
vector of coefficients. |
lags |
integer indicating the number of lags of the moving average. |
trailing_zero, leading_zero
|
boolean indicating whether to remove leading/trailing zero and NA. |
object |
|
... |
other unused parameters. |
s |
seasonal period for the |
A moving average is defined by a set of coefficient
such all time series are transformed as:
The integer is defined by the parameter lags and can be retrieved by
the function lower_bound(). The integer is defined by the length of the coefficients
and can be retrieved by the function upper_bound().
The length of the moving average is defined as and can be retrieved by the function length().
A moving average is symmetric (is_symmetric()) if and for all .
The function to_seasonal() transforms the moving average to:
The functions mirror() and rev() transforms the moving average to:
moving_average() returns a "moving_average" object which is an S4 class with three slots:
coefficients: a numeric vector of coefficients.
lower_bound: an integer indicating the number of lags.
upper_bound: an integer indicating the number of leads (automatically computed).
is.moving_average() and is_symmetric() return a boolean.
length(), numeric(), lower_bound() and upper_bound() return an integer.
mirror(), rev() and to_seasonal() return a "moving_average" object.
y <- retailsa$AllOtherGenMerchandiseStores e1 <- moving_average(rep(1,12), lags = -6) e1 <- e1/sum(e1) e2 <- moving_average(rep(1/12, 12), lags = -5) M2X12 <- (e1 + e2)/2 stats::coef(M2X12) M3 <- moving_average(rep(1/3, 3), lags = -1) M3X3 <- M3 * M3 # M3X3 moving average applied to each month M3X3 M3X3_seasonal <- to_seasonal(M3X3, 12) # M3X3_seasonal moving average applied to the global series M3X3_seasonal def.par <- par(no.readonly = TRUE) par(mai = c(0.5, 0.8, 0.3, 0)) layout(matrix(c(1,2), nrow = 1)) plot_gain(M3X3, main = "M3X3 applied to each month") plot_gain(M3X3_seasonal, main = "M3X3 applied to the global series") par(def.par) # To apply the moving average t <- y * M2X12 # Or use the filter() function: t <- filter(y, M2X12) si <- y - t s <- si * M3X3_seasonal # or equivalently: s_mm <- M3X3_seasonal * (1 - M2X12) s <- y * s_mm graphics::plot(s)y <- retailsa$AllOtherGenMerchandiseStores e1 <- moving_average(rep(1,12), lags = -6) e1 <- e1/sum(e1) e2 <- moving_average(rep(1/12, 12), lags = -5) M2X12 <- (e1 + e2)/2 stats::coef(M2X12) M3 <- moving_average(rep(1/3, 3), lags = -1) M3X3 <- M3 * M3 # M3X3 moving average applied to each month M3X3 M3X3_seasonal <- to_seasonal(M3X3, 12) # M3X3_seasonal moving average applied to the global series M3X3_seasonal def.par <- par(no.readonly = TRUE) par(mai = c(0.5, 0.8, 0.3, 0)) layout(matrix(c(1,2), nrow = 1)) plot_gain(M3X3, main = "M3X3 applied to each month") plot_gain(M3X3_seasonal, main = "M3X3 applied to the global series") par(def.par) # To apply the moving average t <- y * M2X12 # Or use the filter() function: t <- filter(y, M2X12) si <- y - t s <- si * M3X3_seasonal # or equivalently: s_mm <- M3X3_seasonal * (1 - M2X12) s <- y * s_mm graphics::plot(s)
Accuracy/smoothness/timeliness criteria through spectral decomposition
mse(aweights, sweights, density = c("uniform", "rw"), passband = pi/6, ...) ## S3 method for class 'finite_filters' mse( aweights, sweights = aweights@sfilter, density = c("uniform", "rw"), passband = pi/6, sfilter = TRUE, rfilters = TRUE, lfilters = FALSE, ... )mse(aweights, sweights, density = c("uniform", "rw"), passband = pi/6, ...) ## S3 method for class 'finite_filters' mse( aweights, sweights = aweights@sfilter, density = c("uniform", "rw"), passband = pi/6, sfilter = TRUE, rfilters = TRUE, lfilters = FALSE, ... )
aweights |
|
sweights |
|
density |
hypothesis on the spectral density: |
passband |
passband threshold. |
... |
other unused arguments. |
sfilter, rfilters, lfilters
|
boolean indicating if the criteria should be computed for the symmetric filter
( |
If sweights is a finite_filters() object, a matrix with the value of the three criteria for asymmetric and
symmetric filters (depending on the value of sfilter, rfilters and lfilters).
Otherwise, a named vector with the value of the three criteria is returned.
Wildi, Marc and McElroy, Tucker (2019). “The trilemma between accuracy, timeliness and smoothness in real-time signal extraction”. In: International Journal of Forecasting 35.3, pp. 1072–1084.
filter <- lp_filter(horizon = 6, kernel = "Henderson", endpoints = "LC") sweights <- filter[, "q=6"] aweights <- filter[, "q=0"] mse(aweights, sweights) # Or to compute directly the criteria on all asymmetric filters: mse(filter)filter <- lp_filter(horizon = 6, kernel = "Henderson", endpoints = "LC") sweights <- filter[, "q=6"] aweights <- filter[, "q=0"] mse(aweights, sweights) # Or to compute directly the criteria on all asymmetric filters: mse(filter)
Functions to plot the coefficients, the gain and the phase functions.
plot_coef(x, nxlab = 7, add = FALSE, ..., xlab = "", ylab = "coefficient") ## Default S3 method: plot_coef( x, nxlab = 7, add = FALSE, zero_as_na = TRUE, q = 0, legend = FALSE, legend.pos = "topright", ..., xlab = "", ylab = "coefficient" ) ## S3 method for class 'moving_average' plot_coef(x, nxlab = 7, add = FALSE, ..., xlab = "", ylab = "coefficient") ## S3 method for class 'finite_filters' plot_coef( x, nxlab = 7, add = FALSE, zero_as_na = TRUE, q = 0, legend = length(q) > 1, legend.pos = "topright", ..., xlab = "", ylab = "coefficient" ) plot_gain( x, nxlab = 7, add = FALSE, xlim = c(0, pi), ..., xlab = "", ylab = "gain" ) ## S3 method for class 'moving_average' plot_gain( x, nxlab = 7, add = FALSE, xlim = c(0, pi), ..., xlab = "", ylab = "gain" ) ## S3 method for class 'finite_filters' plot_gain( x, nxlab = 7, add = FALSE, xlim = c(0, pi), q = 0, legend = length(q) > 1, legend.pos = "topright", n = 101, ..., xlab = "", ylab = "gain" ) plot_phase( x, nxlab = 7, add = FALSE, xlim = c(0, pi), normalized = FALSE, ..., xlab = "", ylab = "phase" ) ## S3 method for class 'moving_average' plot_phase( x, nxlab = 7, add = FALSE, xlim = c(0, pi), normalized = FALSE, ..., xlab = "", ylab = "phase" ) ## S3 method for class 'finite_filters' plot_phase( x, nxlab = 7, add = FALSE, xlim = c(0, pi), normalized = FALSE, q = 0, legend = length(q) > 1, legend.pos = "topright", n = 101, ..., xlab = "", ylab = "phase" )plot_coef(x, nxlab = 7, add = FALSE, ..., xlab = "", ylab = "coefficient") ## Default S3 method: plot_coef( x, nxlab = 7, add = FALSE, zero_as_na = TRUE, q = 0, legend = FALSE, legend.pos = "topright", ..., xlab = "", ylab = "coefficient" ) ## S3 method for class 'moving_average' plot_coef(x, nxlab = 7, add = FALSE, ..., xlab = "", ylab = "coefficient") ## S3 method for class 'finite_filters' plot_coef( x, nxlab = 7, add = FALSE, zero_as_na = TRUE, q = 0, legend = length(q) > 1, legend.pos = "topright", ..., xlab = "", ylab = "coefficient" ) plot_gain( x, nxlab = 7, add = FALSE, xlim = c(0, pi), ..., xlab = "", ylab = "gain" ) ## S3 method for class 'moving_average' plot_gain( x, nxlab = 7, add = FALSE, xlim = c(0, pi), ..., xlab = "", ylab = "gain" ) ## S3 method for class 'finite_filters' plot_gain( x, nxlab = 7, add = FALSE, xlim = c(0, pi), q = 0, legend = length(q) > 1, legend.pos = "topright", n = 101, ..., xlab = "", ylab = "gain" ) plot_phase( x, nxlab = 7, add = FALSE, xlim = c(0, pi), normalized = FALSE, ..., xlab = "", ylab = "phase" ) ## S3 method for class 'moving_average' plot_phase( x, nxlab = 7, add = FALSE, xlim = c(0, pi), normalized = FALSE, ..., xlab = "", ylab = "phase" ) ## S3 method for class 'finite_filters' plot_phase( x, nxlab = 7, add = FALSE, xlim = c(0, pi), normalized = FALSE, q = 0, legend = length(q) > 1, legend.pos = "topright", n = 101, ..., xlab = "", ylab = "phase" )
x |
coefficients, gain or phase. |
nxlab |
number of xlab. |
add |
boolean indicating if the new plot is added to the previous one. |
... |
other arguments to |
xlab, ylab
|
labels of axis. |
zero_as_na |
boolean indicating if the trailing zero of the coefficients should be plotted ( |
q |
q. |
legend |
boolean indicating if the legend is printed. |
legend.pos |
position of the legend. |
xlim |
vector containing x limits. |
n |
number of points used to plot the functions. |
normalized |
boolean indicatif if the phase function is normalized by the frequency. |
No return value, called for side effects.
filter <- lp_filter(6, endpoints = "DAF", kernel = "Henderson") plot_coef(filter, q = c(0,3), legend = TRUE) plot_gain(filter, q = c(0,3), legend = TRUE) plot_phase(filter, q = c(0,3), legend = TRUE)filter <- lp_filter(6, endpoints = "DAF", kernel = "Henderson") plot_coef(filter, q = c(0,3), legend = TRUE) plot_gain(filter, q = c(0,3), legend = TRUE) plot_phase(filter, q = c(0,3), legend = TRUE)
Create polynomial matrix used in local polynomial regression (see details).
polynomial_matrix(l, u = abs(l), d0 = 0, d1 = 3)polynomial_matrix(l, u = abs(l), d0 = 0, d1 = 3)
l, u
|
lower bound (usually negative) and upper bound (usually positive) of the polynomial matrix. |
d0, d1
|
lower and upper polynomial degree of the polynomial matrix. |
polynomial_matrix() computes the following matrix:
A "matrix" of size .
# For example to reproduce DAF filters daf <- lp_filter(horizon = 6, endpoints = "DAF") q <- 0 X <- polynomial_matrix(l = -6, u = q, d0 = 0, d1 = 3) K <- diag(sapply(-6:q, function(i) get_kernel(horizon = 6)[i])) e_1 <- c(1, 0, 0, 0) q0 <- K %*% X %*% solve(t(X) %*% K %*% X, e_1) q0 daf[, "q=0"]# For example to reproduce DAF filters daf <- lp_filter(horizon = 6, endpoints = "DAF") q <- 0 X <- polynomial_matrix(l = -6, u = q, d0 = 0, d1 = 3) K <- diag(sapply(-6:q, function(i) get_kernel(horizon = 6)[i])) e_1 <- c(1, 0, 0, 0) q0 <- K %*% X %*% solve(t(X) %*% K %*% X, e_1) q0 daf[, "q=0"]
Printing objects of class "finite_filters".
## S4 method for signature 'finite_filters' print( x, sfilter = TRUE, rfilters = TRUE, lfilters = FALSE, zero_as_na = FALSE, ... ) ## S4 method for signature 'finite_filters' show(object) ## S3 method for class 'finite_filters' as.matrix( x, sfilter = TRUE, rfilters = TRUE, lfilters = FALSE, zero_as_na = FALSE, ... )## S4 method for signature 'finite_filters' print( x, sfilter = TRUE, rfilters = TRUE, lfilters = FALSE, zero_as_na = FALSE, ... ) ## S4 method for signature 'finite_filters' show(object) ## S3 method for class 'finite_filters' as.matrix( x, sfilter = TRUE, rfilters = TRUE, lfilters = FALSE, zero_as_na = FALSE, ... )
x, object
|
An object of class |
sfilter, rfilters, lfilters
|
Logical indicating whether to add the symmetric filter
( |
zero_as_na |
Logical. Asymmetric filters have fewer terms than the
symmetric filter. If |
... |
Further arguments to be passed to or from methods. |
The print method just calls the as.matrix() method and prints the resulting matrix.
The print method returns the argument x, invisibly, as for all base::print() methods.
as.matrix.finite_filters() returns a matrix containing the coefficients of the symmetric and asymmetric filters.
The columns are named according to the number of future points used (for right filters).
For example, column q=0 corresponds to the asymmetric filter using no future points,
while q=1 corresponds to the filter using one future point, and so on.
A similar convention with negative values is used for left filters (when printed).
ff_lp <- lp_filter() as.matrix(ff_lp)ff_lp <- lp_filter() as.matrix(ff_lp)
A dataset containing monthly seasonally adjusted retailed sales
retailsaretailsa
A list of ts objects from january 1992 to december 2010.
Estimation of a filter using Reproducing Kernel Hilbert Space (RKHS)
rkhs_filter( horizon = 6, degree = 2, kernel = c("BiWeight", "Henderson", "Epanechnikov", "Triangular", "Uniform", "TriWeight"), asymmetricCriterion = c("Timeliness", "FrequencyResponse", "Accuracy", "Smoothness", "Undefined"), density = c("uniform", "rw"), passband = 2 * pi/12, optimalbw = TRUE, optimal.minBandwidth = horizon, optimal.maxBandwidth = 3 * horizon, bandwidth = horizon + 1 )rkhs_filter( horizon = 6, degree = 2, kernel = c("BiWeight", "Henderson", "Epanechnikov", "Triangular", "Uniform", "TriWeight"), asymmetricCriterion = c("Timeliness", "FrequencyResponse", "Accuracy", "Smoothness", "Undefined"), density = c("uniform", "rw"), passband = 2 * pi/12, optimalbw = TRUE, optimal.minBandwidth = horizon, optimal.maxBandwidth = 3 * horizon, bandwidth = horizon + 1 )
horizon |
horizon (bandwidth) of the symmetric filter. |
degree |
degree of polynomial. |
kernel |
kernel uses. |
asymmetricCriterion |
the criteria used to compute the optimal
bandwidth. If |
density |
hypothesis on the spectral density: |
passband |
passband threshold. |
optimalbw |
boolean indicating if the bandwidth should be choosen by
optimisation (between |
optimal.minBandwidth, optimal.maxBandwidth
|
the range used for the optimal bandwidth selection. |
bandwidth |
the bandwidth to use if |
A finite_filters() object.
Dagum, Estela Bee and Silvia Bianconcini (2008). “The Henderson Smoother in Reproducing Kernel Hilbert Space”. In: Journal of Business & Economic Statistics 26, pp. 536–545. URL: https://ideas.repec.org/a/bes/jnlbes/v26y2008p536-545.html.
rkhs <- rkhs_filter(horizon = 6, asymmetricCriterion = "Timeliness") plot_coef(rkhs)rkhs <- rkhs_filter(horizon = 6, asymmetricCriterion = "Timeliness") plot_coef(rkhs)
Get RKHS kernel function
rkhs_kernel( kernel = c("Biweight", "Henderson", "Epanechnikov", "Triangular", "Uniform", "Triweight"), degree = 2, horizon = 6 )rkhs_kernel( kernel = c("Biweight", "Henderson", "Epanechnikov", "Triangular", "Uniform", "Triweight"), degree = 2, horizon = 6 )
kernel |
kernel uses. |
degree |
degree of polynomial. |
horizon |
horizon (bandwidth) of the symmetric filter. |
A function that takes a numeric input and returns the value of the RKHS kernel.
biweight <- rkhs_kernel(kernel = "Biweight") triangular <- rkhs_kernel(kernel = "Triangular") graphics::plot(biweight, -1, 1) graphics::plot(triangular, -1, 1, add = TRUE, col = "orange")biweight <- rkhs_kernel(kernel = "Biweight") triangular <- rkhs_kernel(kernel = "Triangular") graphics::plot(biweight, -1, 1) graphics::plot(triangular, -1, 1, add = TRUE, col = "orange")
Function to compute the optimal bandwidths used in Reproducing Kernel Hilbert Space (RKHS) filters
rkhs_optimal_bw( horizon = 6, degree = 2, kernel = c("Biweight", "Henderson", "Epanechnikov", "Triangular", "Uniform", "Triweight"), asymmetricCriterion = c("Timeliness", "FrequencyResponse", "Accuracy", "Smoothness"), density = c("uniform", "rw"), passband = 2 * pi/12, optimal.minBandwidth = horizon, optimal.maxBandwidth = 3 * horizon )rkhs_optimal_bw( horizon = 6, degree = 2, kernel = c("Biweight", "Henderson", "Epanechnikov", "Triangular", "Uniform", "Triweight"), asymmetricCriterion = c("Timeliness", "FrequencyResponse", "Accuracy", "Smoothness"), density = c("uniform", "rw"), passband = 2 * pi/12, optimal.minBandwidth = horizon, optimal.maxBandwidth = 3 * horizon )
horizon |
horizon (bandwidth) of the symmetric filter. |
degree |
degree of polynomial. |
kernel |
kernel uses. |
asymmetricCriterion |
the criteria used to compute the optimal
bandwidth. If |
density |
hypothesis on the spectral density: |
passband |
passband threshold. |
optimal.minBandwidth, optimal.maxBandwidth
|
the range used for the optimal bandwidth selection. |
A vector of optimal bandwidths for each lead time.
rkhs_optimal_bw(asymmetricCriterion = "Timeliness") rkhs_optimal_bw(asymmetricCriterion = "Timeliness", optimal.minBandwidth = 6.2)rkhs_optimal_bw(asymmetricCriterion = "Timeliness") rkhs_optimal_bw(asymmetricCriterion = "Timeliness", optimal.minBandwidth = 6.2)
Export function used to compute the optimal bandwidth of Reproducing Kernel Hilbert Space (RKHS) filters.
rkhs_optimization_fun( horizon = 6, leads = 0, degree = 2, kernel = c("Biweight", "Henderson", "Epanechnikov", "Triangular", "Uniform", "Triweight"), asymmetricCriterion = c("Timeliness", "FrequencyResponse", "Accuracy", "Smoothness"), density = c("uniform", "rw"), passband = 2 * pi/12 )rkhs_optimization_fun( horizon = 6, leads = 0, degree = 2, kernel = c("Biweight", "Henderson", "Epanechnikov", "Triangular", "Uniform", "Triweight"), asymmetricCriterion = c("Timeliness", "FrequencyResponse", "Accuracy", "Smoothness"), density = c("uniform", "rw"), passband = 2 * pi/12 )
horizon |
horizon (bandwidth) of the symmetric filter. |
leads |
Leads of the filter (should be positive or 0). |
degree |
degree of polynomial. |
kernel |
kernel uses. |
asymmetricCriterion |
the criteria used to compute the optimal
bandwidth. If |
density |
hypothesis on the spectral density: |
passband |
passband threshold. |
A function that takes a bandwidth as input and returns the value of the optimization criterion.
graphics::plot( rkhs_optimization_fun( horizon = 6, leads = 0, degree = 3, asymmetricCriterion = "Timeliness" ), 5.5, 6 * 3, ylab = "Timeliness", main = "6X0 filter" ) graphics::plot( rkhs_optimization_fun( horizon = 6, leads = 1, degree = 3, asymmetricCriterion = "Timeliness" ), 5.5, 6 * 3, ylab = "Timeliness", main = "6X1 filter" ) graphics::plot( rkhs_optimization_fun( horizon = 6, leads = 2, degree = 3, asymmetricCriterion = "Timeliness" ), 5.5, 6 * 3, ylab = "Timeliness", main = "6X2 filter" ) graphics::plot( rkhs_optimization_fun( horizon = 6, leads = 3, degree = 3, asymmetricCriterion = "Timeliness" ), 5.5, 6 * 3, ylab = "Timeliness", main = "6X3 filter" ) graphics::plot( rkhs_optimization_fun( horizon = 6, leads = 4, degree = 3, asymmetricCriterion = "Timeliness" ), 5.5, 6 * 3, ylab = "Timeliness", main = "6X4 filter" ) graphics::plot( rkhs_optimization_fun( horizon = 6, leads = 5, degree = 3, asymmetricCriterion = "Timeliness" ), 5.5, 6 * 3, ylab = "Timeliness", main = "6X5 filter" )graphics::plot( rkhs_optimization_fun( horizon = 6, leads = 0, degree = 3, asymmetricCriterion = "Timeliness" ), 5.5, 6 * 3, ylab = "Timeliness", main = "6X0 filter" ) graphics::plot( rkhs_optimization_fun( horizon = 6, leads = 1, degree = 3, asymmetricCriterion = "Timeliness" ), 5.5, 6 * 3, ylab = "Timeliness", main = "6X1 filter" ) graphics::plot( rkhs_optimization_fun( horizon = 6, leads = 2, degree = 3, asymmetricCriterion = "Timeliness" ), 5.5, 6 * 3, ylab = "Timeliness", main = "6X2 filter" ) graphics::plot( rkhs_optimization_fun( horizon = 6, leads = 3, degree = 3, asymmetricCriterion = "Timeliness" ), 5.5, 6 * 3, ylab = "Timeliness", main = "6X3 filter" ) graphics::plot( rkhs_optimization_fun( horizon = 6, leads = 4, degree = 3, asymmetricCriterion = "Timeliness" ), 5.5, 6 * 3, ylab = "Timeliness", main = "6X4 filter" ) graphics::plot( rkhs_optimization_fun( horizon = 6, leads = 5, degree = 3, asymmetricCriterion = "Timeliness" ), 5.5, 6 * 3, ylab = "Timeliness", main = "6X5 filter" )
A simple moving average is a moving average whose coefficients are all equal and whose sum is 1
simple_ma(order, lags = -trunc((order - 1)/2))simple_ma(order, lags = -trunc((order - 1)/2))
order |
number of terms of the moving_average |
lags |
integer indicating the number of lags of the moving average. |
A moving_average() object.
# The M2X12 moving average is computed as (simple_ma(12, -6) + simple_ma(12, -5)) / 2 # The M3X3 moving average is computed as simple_ma(3, -1) ^ 2 # The M3X5 moving average is computed as simple_ma(3, -1) * simple_ma(5, -2)# The M2X12 moving average is computed as (simple_ma(12, -6) + simple_ma(12, -5)) / 2 # The M3X3 moving average is computed as simple_ma(3, -1) ^ 2 # The M3X5 moving average is computed as simple_ma(3, -1) * simple_ma(5, -2)
Function to retrieve the underlying forecasts corresponding to the asymmetric filters: the asymmetric filtering produce the same result than the symmetric filters applied to the extended series.
underlying_forecasts(x, coefs)underlying_forecasts(x, coefs)
x |
a univariate or multivariate time series. |
coefs |
a |
Let be the bandwidth of the symmetric filter,
the coefficients of the symmetric filter and
the coefficients of the asymmetric filter used to estimate
the trend when future values are known (with the convention ).
Let denote the last available values of the input times series.
The underlying forecast induced by are defined by:
Note that this is solved numerically: the solution isn't exact.
An object of the same class as x (ts, mts, vector or matrix) with the underlying forecasts.
x <- retailsa$AllOtherGenMerchandiseStores ql <- lp_filter(horizon = 6, kernel = "Henderson", endpoints = "QL") lc <- lp_filter(horizon = 6, kernel = "Henderson", endpoints = "LC") f_ql <- underlying_forecasts(x, ql) f_lc <- underlying_forecasts(x, lc) graphics::plot(window(x, start = 2007), xlim = c(2007, 2012)) graphics::lines( stats::ts( c(utils::tail(x, 1), f_ql), frequency = stats::frequency(x), start = stats::end(x) ), col = "red", lty = 2 ) graphics::lines( stats::ts( c(utils::tail(x, 1), f_lc), frequency = stats::frequency(x), start = stats::end(x) ), col = "blue", lty = 2 )x <- retailsa$AllOtherGenMerchandiseStores ql <- lp_filter(horizon = 6, kernel = "Henderson", endpoints = "QL") lc <- lp_filter(horizon = 6, kernel = "Henderson", endpoints = "LC") f_ql <- underlying_forecasts(x, ql) f_lc <- underlying_forecasts(x, lc) graphics::plot(window(x, start = 2007), xlim = c(2007, 2012)) graphics::lines( stats::ts( c(utils::tail(x, 1), f_ql), frequency = stats::frequency(x), start = stats::end(x) ), col = "red", lty = 2 ) graphics::lines( stats::ts( c(utils::tail(x, 1), f_lc), frequency = stats::frequency(x), start = stats::end(x) ), col = "blue", lty = 2 )
Variance Estimator
var_estimator(x, coef, ...)var_estimator(x, coef, ...)
x |
input time series. |
coef |
vector of coefficients or a moving-average ( |
... |
other arguments passed to the function |
Let be a moving average of length used
to filter a time series .
It is equivalent to a local regression and the associated error variance
can be estimated using the normalized residual sum of squares, which can be simplified as:
Numeric value with the statistic.
Loader, Clive. 1999. Local regression and likelihood. New York: Springer-Verlag.
x <- retailsa$DrinkingPlaces h13 <- lp_filter(6)@sfilter var_estimator(x, h13)x <- retailsa$DrinkingPlaces h13 <- lp_filter(6)@sfilter var_estimator(x, h13)