View source: R/extract_trends.R
| extract_trends | R Documentation |
Extract trend components from time series objects using various econometric methods. Designed for monthly and quarterly economic data analysis. Returns trend components as time series objects or a list of time series.
extract_trends(
ts_data,
methods = "stl",
window = NULL,
smoothing = NULL,
band = NULL,
align = NULL,
params = list(),
.quiet = FALSE
)
ts_data |
A time series object ( |
methods |
Character vector of trend methods.
Options: |
window |
Unified window/period parameter for moving
average methods (ma, wma, triangular, stl, ewma, median, gaussian,
henderson). Must be positive.
If NULL, uses frequency-appropriate defaults. For EWMA, the window is
converted to the smoothing factor via |
smoothing |
Unified smoothing parameter for smoothing
methods (hp, loess, spline, ewma, kernel, kalman).
For hp: a value above 1 is lambda itself; a value of 1 or less is a
fraction of the default lambda for the frequency, |
band |
Unified band parameter for bandpass filters
(bk, cf). Provide as |
align |
Unified alignment parameter for moving average
methods (ma, wma, triangular, gaussian). Valid values: |
params |
Optional list of method-specific parameters for fine control:
|
.quiet |
If |
This function focuses on monthly (frequency = 12) and quarterly (frequency = 4) economic data. It uses established econometric methods with appropriate defaults:
HP Filter: lambda = 1600 (quarterly), 129600 (monthly), 6.25 (annual), following Ravn and Uhlig (2002). Supports both two-sided and one-sided (real-time) variants
Baxter-King: Bandpass filter for business cycles (1.5 to 8 years by default)
Christiano-Fitzgerald: Asymmetric bandpass filter
Moving Average: Centered, frequency-appropriate windows
STL: Seasonal-trend decomposition
Loess: Local polynomial regression
Spline: Smoothing splines
Polynomial: Linear/polynomial trends
Beveridge-Nelson: Permanent/transitory decomposition
UCM: Unobserved Components Model (basic structural model up to monthly data, local level otherwise)
Hamilton: Regression-based alternative to HP filter
Advanced MA: EWMA with various implementations
Kernel Smoother: Non-parametric regression with various kernel functions
Kalman Smoother: Adaptive filtering for noisy time series
Median Filter: Robust filtering using running medians to remove outliers
Gaussian Filter: Weighted average with Gaussian (normal) density weights
Parameter Usage Notes:
HP Filter: Use hp_onesided=TRUE for real-time analysis or when future data should not
influence current estimates. One-sided filter is appropriate for nowcasting, policy analysis,
and avoiding look-ahead bias. Default two-sided filter is optimal for historical analysis.
EWMA: Use either window (converted to alpha = 2 / (window + 1)) OR smoothing (alpha parameter), not both
Kalman: Use smoothing parameter or params list for fine control of noise parameters
Spline: Use spline_cv to control cross-validation (NULL=none, TRUE=LOO-CV, FALSE=GCV)
Polynomial: Use poly_raw=FALSE for orthogonal polynomials (more stable for degree > 2)
or poly_raw=TRUE for raw polynomials. Warning issued for degree > 3 (overfitting risk).
UCM: Choose model type - "level" (simplest), "trend" (time-varying slope), or
"BSM" (with seasonal component, requires seasonal data). Variances are
estimated by maximum likelihood, so smoothing does not apply. The trend
is the smoothed level. On seasonal data, "level" and "trend" can absorb
the seasonality into the level and return the series itself, which is
why the default is "BSM" up to monthly data. "BSM" carries one state per
season and becomes very slow on weekly or daily data.
If single method, returns a ts object. If multiple methods, returns
a named list of ts objects.
# Single method
hp_trend <- extract_trends(AirPassengers, methods = "hp")
# Multiple methods with unified smoothing
smooth_trends <- extract_trends(
AirPassengers,
methods = c("hp", "loess", "ewma"),
smoothing = 0.3
)
# EWMA with window (alpha derived from window size)
ewma_window <- extract_trends(AirPassengers, methods = "ewma", window = 12)
# EWMA with alpha (traditional formula)
ewma_alpha <- extract_trends(AirPassengers, methods = "ewma", smoothing = 0.2)
# Moving averages with unified window
ma_trends <- extract_trends(
AirPassengers,
methods = c("ma", "wma", "triangular"),
window = 8
)
# Bandpass filters with unified band
bp_trends <- extract_trends(
AirPassengers,
methods = c("bk", "cf"),
band = c(18, 96)
)
# Moving average with right alignment (causal filter)
ma_causal <- extract_trends(
AirPassengers,
methods = "ma",
window = 12,
align = "right"
)
# Signal processing methods with specific parameters
finance_trends <- extract_trends(
AirPassengers,
methods = c("kalman", "gaussian"),
window = 9, # For Gaussian filter
params = list(kalman_measurement_noise = 0.1) # Kalman-specific parameter
)
# Spline with cross-validation options
spline_trends <- extract_trends(
AirPassengers,
methods = "spline",
params = list(spline_cv = FALSE) # Use GCV instead of default
)
# Polynomial with orthogonal vs raw polynomials
poly_trends <- extract_trends(
AirPassengers,
methods = "poly",
params = list(poly_degree = 2, poly_raw = FALSE) # Orthogonal (default)
)
# UCM with different model types
ucm_trends <- extract_trends(
AirPassengers,
methods = "ucm",
params = list(ucm_type = "BSM") # Basic Structural Model with seasonality
)
# HP Filter: One-sided (real-time) vs Two-sided (historical)
hp_realtime <- extract_trends(
AirPassengers,
methods = "hp",
params = list(hp_onesided = TRUE) # For nowcasting and real-time analysis
)
# STL with custom parameters via params (both notations work)
stl_custom1 <- extract_trends(
AirPassengers,
methods = "stl",
params = list(s.window = 21, robust = TRUE) # Dot notation
)
stl_custom2 <- extract_trends(
AirPassengers,
methods = "stl",
params = list(stl_s_window = 21, stl_robust = TRUE) # Underscore notation
)
# Advanced: fine-tune specific methods
custom_trends <- extract_trends(
AirPassengers,
methods = c("median", "kalman"),
window = 7,
params = list(median_endrule = "constant")
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.