Skip to contents

Applies a centered moving average filter to a time series using either a rectangular window (default) or a custom window function. If a user-supplied window function is provided via w.func, it will be called as w.func(order, ...) and must return a numeric vector of length order.

Usage

ma(ts, order, na.rm = T, w.func = NULL, ...)

Arguments

ts

A `ts` object to be smoothed.

order

An integer specifying the moving average window size. Must be ≥ 1.

na.rm

Logical. If `TRUE` (default), removes `NA` values introduced by filtering.

w.func

Optional function to generate a weight vector given `order`. Should return a numeric vector of length `order`.

...

Additional arguments passed to `w.func`, if specified.

Value

A smoothed `ts` object with attributes:

collector

Set to `"single"` for bookkeeping.

q_order

The order of the moving average used.

Examples

if (FALSE) { # \dontrun{
ts_data <- ts(rnorm(100))
ma(ts_data, order = 5)

# Using a Tukey window
if (requireNamespace("bspec", quietly = TRUE)) {
    ma(ts_data, order = 11, w.func = tukey_window, r = 0.3)
}
} # }