| roll_series | R Documentation |
Compute rolling and year-to-date aggregations of a time series. Unlike
extract_trends(), which estimates a trend in the units of the series, these
are aggregations: a 12-month rolling sum is a 12-month total, not a level
estimate. The two families are kept separate for that reason, so rolling
results are not accepted by detrend_series().
roll_series(
ts_data,
stats = "sum",
window = NULL,
align = "right",
percent = FALSE,
na_rm = FALSE,
.quiet = FALSE
)
ts_data |
A time series object ( |
stats |
Character vector of rolling statistics. Options: |
window |
Window length in periods, or the lag for |
align |
Alignment of the window relative to the output position:
|
percent |
Only used by |
na_rm |
If |
.quiet |
If |
stats = "sum" and stats = "chain" answer the same question for different
kinds of series. For a flow measured in levels (units sold, jobs created),
the 12-month accumulation is the sum. For a series that is already a rate of
change (monthly inflation, monthly returns), summing is only an
approximation; the correct accumulation compounds the rates:
(1 + r_1)(1 + r_2)\cdots(1 + r_k) - 1
stats = "change" goes the other way, from a level (an index, a price, real
income) to its rate of change over window periods. Chaining the
one-period changes over k periods gives back the k-period change. The
lag counts periods on the calendar grid for monthly, quarterly and annual
series, and observations for daily and weekly series.
Note that a rolling sum is proportional to the simple moving average
available through extract_trends(): roll_series(x, "sum", window = k)
equals k times extract_trends(x, "ma", window = k, align = "right"). The
rolling version is the one to reach for when the accumulated quantity is
itself the number of interest. The two part company for an even window
under align = "center", where the moving average is weighted and the sum
is not.
An even window centred on an observation has one more period on one side
than the other. "mean" resolves this the way the ma trend method does,
with the 2xN filter that puts half weight on the two endpoints, so
roll_series(x, "mean", window = k, align = "center") matches
extract_trends(x, "ma", window = k, align = "center"). The other
statistics have no such correction and use a window with one extra period
after the anchor.
If a single statistic and a single window are requested, a ts
object. Otherwise a named list of ts objects with names of the form
{stat}_{window} (e.g. sum_12, chain_ytd, change_12).
augment_rolling() for the data frame interface,
extract_trends() for trend estimation.
# 12-month rolling sum of vehicle production
prod_ts <- df_to_ts(vehicles, value_col = "production", frequency = 12)
roll_series(prod_ts, "sum", window = 12)
# Accumulated growth over 12 months, from monthly rates in percent
ibc_ts <- df_to_ts(ibcbr, value_col = "index", frequency = 12)
rates <- roll_series(ibc_ts, "change", window = 1, percent = TRUE)
roll_series(rates, "chain", window = 12, percent = TRUE)
# Year-to-date accumulation, resetting each January
roll_series(rates, "chain", window = "ytd", percent = TRUE)
# Cumulative growth since the start of the series
roll_series(rates, "chain", window = "all", percent = TRUE)
# 12-month change of the index, in percent
roll_series(ibc_ts, "change", window = 12, percent = TRUE)
# Several statistics and windows at once
roll_series(prod_ts, stats = c("sum", "sd"), window = c(3, 12))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.