Learn R Programming

DTSg (version 0.2.0)

rollapply.DTSg: Rolling Window Function

Description

Applies an arbitrary function to a rolling window of selected columns of a DTSg object with recognised periodicity.

Usage

# S3 method for DTSg
rollapply(x, fun, ..., cols = self$cols(class =
  "numeric")[1L], before = 1L, after = before,
  weights = c("inverseDistance"), parameters = list(power = 1),
  clone = getOption("DTSgClone"))

Arguments

x

A DTSg object (S3 method only).

fun

A function. Its return value must be of length one.

Further arguments passed on to fun.

cols

A character vector specifying the columns whose rolling window fun shall be applied to.

before

A numeric specifying the size of the window in time steps before the “center” of the rolling window.

after

A numeric specifying the size of the window in time steps after the “center” of the rolling window.

weights

A character string specifying a method to calculate weights for fun, for instance, weighted.mean. See details for further information.

parameters

A list specifying parameters for weights. See details for further information.

clone

A logical specifying if the object is modified in place or if a clone (copy) is made beforehand.

Value

Returns a DTSg object.

Details

In addition to the argument, this method hands over the weights as a numeric vector (w argument) and a list argument with helper data called .helpers to fun. .helpers contains the following named elements:

  • before: Same as before argument.

  • after: Same as after argument.

  • windowSize: Size of the rolling window (before + 1L + after).

  • centerIndex: Index of the “center” of the rolling window (before + 1L).

Currently, only one method to calculate weights is supported: "inverseDistance". The distance \(d\) of the “center” is one and each time step away from the “center” adds one to it. So, for example, the distance of a timestamp three steps away from the “center” is four. Additionally, the calculation of the weights accepts a power \(p\) parameter as a named element of a list provided through the parameters argument: \(\frac{1}{d^p}\).

See Also

DTSg, cols, list

Examples

Run this code
# NOT RUN {
# new DTSg object
x <- DTSg$new(values = flow)

# calculate moving average
## R6 method
x$rollapply(fun = mean, na.rm = TRUE, before = 2, after = 2)

## S3 method
rollapply(x = x, fun = mean, na.rm = TRUE, before = 2, after = 2)

# }

Run the code above in your browser using DataLab