View source: R/augment_trends.R
| augment_trends | R Documentation |
Pipe-friendly function that adds trend columns to a tibble or data.frame. Designed for exploratory analysis of monthly and quarterly economic time series. Supports multiple trend extraction methods and handles grouped data.
augment_trends(
data,
date_col = "date",
value_col = "value",
group_cols = NULL,
group_vars = NULL,
methods = "stl",
frequency = NULL,
suffix = NULL,
window = NULL,
smoothing = NULL,
band = NULL,
align = NULL,
params = list(),
.quiet = FALSE
)
data |
A |
date_col |
Name of the date column. Defaults to |
value_col |
Name of the value column(s). Defaults to |
group_cols |
Optional grouping variables for multiple time series. Can be a character vector of column names. For tsibbles, defaults to the key and must match it when supplied. |
group_vars |
Deprecated. Use |
methods |
Character vector of trend methods.
Options: |
frequency |
The frequency of the series.
Supports values from 1 (annual) to 365 (daily). Auto-detected for data
frames; a tsibble's |
suffix |
Optional suffix for trend column names. If NULL, uses method names. |
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: use large values (1600+) or small values (0-1) that get converted.
For EWMA: specifies the alpha parameter (0-1) for traditional exponential smoothing.
Cannot be used simultaneously with |
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 is designed for monthly (frequency = 12) and quarterly (frequency = 4) economic data, and the defaults for each method follow the conventions for those frequencies.
For grouped data, the function applies trend extraction to each group separately,
maintaining the original data structure while adding trend columns.
For tsibbles, only Date, yearmonth, and yearquarter indices are
supported; the existing missing-period rules apply after conversion to
calendar dates.
A tibble with original data plus trend columns named trend_{method} or
trend_{method}_{suffix} if suffix is provided. Rows come back in the
order they were supplied in. A tsibble input returns a tsibble with its
index class and key preserved.
# Simple STL decomposition on quarterly GDP construction data
gdp_construction |> augment_trends(value_col = "index")
# Multiple smoothing methods with unified parameter
gdp_construction |>
augment_trends(
value_col = "index",
methods = c("hp", "loess", "ewma"),
smoothing = 0.3
)
# Moving averages with unified window on monthly data
vehicles |>
tail(60) |>
augment_trends(
value_col = "production",
methods = c("ma", "wma", "triangular"),
window = 8
)
# Economic indicators with different methods
ibcbr |>
tail(48) |>
augment_trends(
value_col = "index",
methods = c("median", "kalman", "kernel"),
window = 9,
smoothing = 0.15
)
# Moving average with right alignment (causal filter)
vehicles |>
tail(60) |>
augment_trends(
value_col = "production",
methods = "ma",
window = 12,
align = "right"
)
# Advanced: fine-tune specific methods
electric |>
tail(72) |>
augment_trends(
value_col = "consumption",
methods = "median",
window = 7
)
# Multiple MA windows in a single call (adds trend_ma_3, trend_ma_6, trend_ma_12)
vehicles |>
tail(60) |>
augment_trends(
value_col = "production",
methods = "ma",
window = c(3, 6, 12)
)
# Preserve a tsibble's index and key (if tsibble is installed)
if (requireNamespace("tsibble", quietly = TRUE)) {
quarterly <- gdp_construction
quarterly$date <- tsibble::yearquarter(quarterly$date)
quarterly <- tsibble::as_tsibble(quarterly, index = date)
augment_trends(quarterly, value_col = "index", methods = "hp")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.