View source: R/augment_rolling.R
| augment_rolling | R Documentation |
Pipe-friendly companion to augment_trends() for rolling and year-to-date
aggregations: 12-month accumulated totals, compounded rates of change,
rolling volatility, and so on. Columns are prefixed roll_ rather than
trend_, because these are aggregations of the series and not estimates of
its trend.
augment_rolling(
data,
date_col = "date",
value_col = "value",
group_cols = NULL,
stats = "sum",
window = NULL,
frequency = NULL,
align = "right",
percent = FALSE,
na_rm = FALSE,
suffix = NULL,
.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. |
stats |
Character vector of rolling statistics. Options: |
window |
Window length in periods, or the lag for |
frequency |
The frequency of the series. Supports values from 1 (annual) to 365 (daily). Auto-detected if not specified. |
align |
Alignment of the window relative to the output position:
|
percent |
Only used by |
na_rm |
If |
suffix |
Optional suffix appended to the generated column names. |
.quiet |
If |
Use "sum" for flows measured in levels and "chain" for series that are
already rates of change. Summing monthly inflation rates approximates the
12-month accumulation but is not equal to it; "chain" compounds them
correctly. "change" turns a level into its rate of change, matching each
date with the one window periods earlier rather than the row window
positions above. See roll_series() for the underlying computation.
"mean" overlaps with the simple moving average available through
augment_trends(methods = "ma"). The two differ in defaults rather than in
substance: rolling aggregations default to right alignment, while the moving
average trend defaults to centred alignment. Given the same window and
alignment they agree, including the 2xN correction for even centred
windows.
Rows whose value is NA are kept in place, so window positions stay aligned
with the calendar; na_rm then decides whether such a window yields NA or
is computed from the observations that are present. Unlike
augment_trends(), which rejects gaps inside the observed range, a rolling
window has well-defined local behaviour for a gap, so these functions accept
one. A period that is absent
from the data altogether cannot be positioned, so it raises an error rather
than shifting later observations — add the missing rows with an NA value
first.
A tibble with the original data plus rolling columns named
roll_{stat}_{window} (e.g. roll_sum_12, roll_chain_ytd,
roll_change_12), with
_{suffix} appended when suffix is supplied. Rows come back in the
order they were supplied in.
roll_series() for the time series interface, augment_trends()
for trend estimation.
# 12-month accumulated vehicle production
vehicles |> augment_rolling(value_col = "production", window = 12)
# Several windows at once
vehicles |>
tail(60) |>
augment_rolling(value_col = "production", window = c(3, 6, 12))
# Rolling mean and volatility side by side
ibcbr |>
augment_rolling(value_col = "index", stats = c("mean", "sd"), window = 12)
# Year-to-date accumulation, resetting each January
vehicles |> augment_rolling(value_col = "production", window = "ytd")
# 12-month change of an index, in percent
ibcbr |>
augment_rolling(value_col = "index", stats = "change", percent = TRUE)
# Grouped series
retail_volume |>
augment_rolling(group_cols = "name_series", window = 12)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.