Functions to calculate a moving average. These are useful to get rid of (strong) peakiness.
moving_average(x, w, side = "centre", na.rm = getOption("na.rm", FALSE))
moving_sum(x, w, side = "centre", na.rm = getOption("na.rm", FALSE))
moving_Q1(x, w, side = "centre", na.rm = getOption("na.rm", FALSE))
moving_Q3(x, w, side = "centre", na.rm = getOption("na.rm", FALSE))
moving_fn(x, w, fun, side = "centre", ...)
vector of values
window length; total number of observations to include. This should preferably be an odd number, so that the same number of values to the left and right of x
are included.
default is "centre"
, can also be "left"
or "right"
. This can be used to take a moving average (or sum, or ...) of e.g. the last 7 days.
a logical to indicate whether empty must be removed from x
function to apply
arguments passed on to fun
Each function can be used over a moving period with moving_fn()
. For example, for a moving median: moving_fn(x, 7, fun = median)
. Or a moving maximum: moving_fn(x, 5, fun = max)
.
The moving average is determined by averaging floor(w / 2)
values before and after each element of x
and all elements in between.
na.rm
This 'certestats' package supports a global default setting for na.rm
in many mathematical functions. This can be set with options(na.rm = TRUE)
or options(na.rm = FALSE)
.
For normality()
, quantile()
and IQR()
, this also applies to the type
argument. The default, type = 7
, is the default of base R. Use type = 6
to comply with SPSS.