roll_MAD {MazamaRollUtils}R Documentation

Roll MAD

Description

Apply a moving-window Median Absolute Deviation function to a numeric vector.

Usage

roll_MAD(
  x,
  width = 1L,
  by = 1L,
  align = c("center", "left", "right"),
  na.rm = FALSE
)

Arguments

x

Numeric vector.

width

Integer width of the rolling window.

by

Integer shift to use when sliding the window to the next location

align

Character position of the return value within the window. One of: "left" | "center" | "right".

na.rm

Logical specifying whether NA values should be removed before the calculations within each window.

Details

For every index in the incoming vector x, a value is returned that is the Median Absolute Deviation (MAD) of all values in x that fall within a window of width width.

The align parameter determines the alignment of the return value within the window. Thus:

For large vectors, theby parameter can be used to force the window to jump ahead by indices for the next calculation. Indices that are skipped over will be assigned NA values so that the return vector still has the same length as the incoming vector. This can dramatically speed up calculations for high resolution time series data.

Value

Numeric vector of the same length as x.

Examples

library(MazamaRollUtils)

# Wikipedia example
x <- c(0, 0, 0, 1, 1, 2, 2, 4, 6, 9, 0, 0, 0)
roll_MAD(x, 3)
roll_MAD(x, 5)
roll_MAD(x, 7)

[Package MazamaRollUtils version 0.1.3 Index]