movMean: Weighted Simple Moving Mean

Description Usage Arguments Details Value Functions Examples

View source: R/RcppExports.R

Description

This function returns a simple moving average of the given vector. The weight argument is optional.

Usage

1
2
3
4
movMean(vec, n = 1L, ss = 1L, w = NULL, na_rm = FALSE, sizeD = FALSE,
  align = "left")

movMeanr(vec, n = 1L, ss = 1L, w = NULL, na_rm = FALSE, sizeD = FALSE)

Arguments

vec

A numeric vector.

n

An integer: moving window size, with 1 as default

ss

An integer: step size, only calculating at points with an equal distance ss. Namely, there are ss-1 number between each two 'consecutive' points

w

An optional weight vector of length n. It will be automatically normalized (sum to 1).

na_rm

logical. Should missing values (including NaN) be removed?

sizeD

logical. Only applied when ss > 1, it decides whether to get a result of smaller size. If sizeD = T, align does not affect the output.

align

A string denotes how to align the moving average, three options: "left", "middle", "right"

Details

Despite of Efficient computation, usually 5~6 times faster than the moving average function in package 'RcppRoll', it is able to handle potential missing values (NA or NaN) in the vec.
For instance, the output of the second example is NA, NA, 2.200000 3.714286 4.875000. The last number 5.5 is obtained by using re-normalized weight, namely omitting 0.2. The weight applied would be 0.5/(0.5+0.3) and 0.3/(0.5+0.3). Hence,

4.875 = 3 * 0.5/(0.5+0.3) + 8 * 0.3/(0.5+0.3)

Value

This function returns a vector whose length is the same as that of vec or is ceiling((L - n + 1)/ss), (when sizeD = T), where L is the length of vec.

Functions

Examples

1
2
3
movMean(c(1, 4, 3, NA, 8), 3, align = "right", na_rm = TRUE)
movMean(c(1, 4, 3, NA, 8), 3, w = c(0.5, 0.2, 0.3), na_rm = TRUE, align = "right")
movMean(c(1, 4, 3, NA, 8, 4, 5, 9, 6, 0), n = 3, ss = 4, na_rm = TRUE, align = "right")

RcppMovStat documentation built on Jan. 30, 2018, 5:04 p.m.