| filter_ma | R Documentation |
Apply a simple moving average smoothing filter to vector data.
filter_moving_average() is an alias of filter_ma().
filter_ma(
x,
t = seq_along(x),
width = NULL,
span = NULL,
partial = FALSE,
na.rm = FALSE,
verbose = TRUE,
...
)
filter_moving_average(
x,
t = seq_along(x),
width = NULL,
span = NULL,
partial = FALSE,
na.rm = FALSE,
verbose = TRUE,
...
)
x |
A numeric vector of the response variable. |
t |
An optional numeric vector of the predictor variable (e.g. time).
Default is |
width |
An integer defining the local window in number of samples
centred on |
span |
A numeric value defining the local window time span around
|
partial |
Logical; default is |
na.rm |
Logical; default is |
verbose |
Logical. Default is |
... |
Additional arguments. |
Applies a centred (symmetrical) moving average filter in a local
window, defined by either width as the number of samples around
idx between [idx - floor(width/2), idx + floor(width/2)]. Or by
span as the timespan in units of time_channel between
[t - span/2, t + span/2].
The default partial = FALSE requires a complete number of samples
specified by width or span (estimated from the sample rate of t when
span is used). NA is returned if fewer samples are present in the
local window.
Setting partial = TRUE allows computation with only a single valid sample,
such as at edge conditions. But these values will be more sensitive to
noise and should be used with caution.
na.rm controls whether missing values (NAs) within each local window are
either propagated to the returned vector when na.rm = FALSE (the default),
or ignored before processing if na.rm = TRUE.
A numeric vector the same length as x.
x <- c(1, 3, 2, 5, 4, 6, 5, 7)
t <- c(0, 1, 2, 4, 5, 6, 7, 10) ## irregular time with gaps
## width: centred window of 3 samples
filter_ma(x, width = 3)
## partial = TRUE fills edge values with a narrower window
filter_ma(x, width = 3, partial = TRUE)
## span: centred window of 2 time-units (accounts for irregular sampling)
filter_ma(x, t, span = 2)
## na.rm = FALSE (default): any NA in the window propagates to the result
x_na <- c(1, NA, 3, 4, 5, NA, 7, 8)
filter_ma(x_na, width = 3, na.rm = FALSE)
## na.rm = TRUE: skip NAs and return the local mean of local valid values
filter_ma(x_na, width = 3, partial = TRUE, na.rm = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.