Computes running sample mean of a time-series x in a fixed length window.

RunningMean(x, W, circular = FALSE)

Arguments

x

A numeric vector.

W

A numeric scalar; length of x window over which sample mean is computed.

circular

Logical; whether running sample mean is computed assuming circular nature of x time-series (see Details).

Value

A numeric vector.

Details

The length of output vector equals the length of x vector. Parameter circular determines whether x time-series is assumed to have a circular nature. Assume \(l_x\) is the length of time-series x, W is a fixed length of x time-series window.

If circular equals TRUE then

  • first element of the output time-series corresponds to sample mean of x[1:W],

  • last element of the output time-series corresponds to sample mean of c(x[l_x], x[1:(W - 1)]).

If circular equals FALSE then

  • first element of the output time-series corresponds to sample mean of x[1:W],

  • \(l_x - W + 1\)-th element of the output time-series corresponds to sample mean of x[(l_x - W + 1):l_x],

  • last W-1 elements of the output time-series are filled with NA.

See runstats.demo(func.name = "RunningMean") for a detailed presentation.

Examples

x <- rnorm(10) RunningMean(x, 3, circular = FALSE)
#> [1] -0.1144907 0.3980760 0.8190281 0.3265208 0.1380470 0.7299686 #> [7] 1.4916578 0.7567328 NA NA
RunningMean(x, 3, circular = TRUE)
#> [1] -0.11449070 0.39807597 0.81902807 0.32652080 0.13804704 0.72996859 #> [7] 1.49165778 0.75673284 -0.02002755 -1.00306972