rollapply.DTSg: Rolling Window Function

Description Usage Arguments Details Value See Also Examples

Description

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

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
## S3 method for class 'DTSg'
rollapply(
  x,
  fun,
  ...,
  cols = self$cols(class = "numeric")[1L],
  before = 1L,
  after = before,
  weights = "inverseDistance",
  parameters = list(power = 1),
  resultCols = NULL,
  suffix = NULL,
  helpers = TRUE,
  memoryOverCPU = TRUE,
  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

An integerish value specifying the size of the window in time steps before the “center” of the rolling window.

after

An integerish value 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.

resultCols

An optional character vector of the same length as cols. Non-existing columns specified in this argument are added and existing columns are overwritten by the return values of fun. Columns are matched element-wise between resultCols and cols.

suffix

An optional character string. The return values of fun are added as new columns with names consisting of the columns specified in cols and this suffix. Existing columns are never overwritten. Only used when resultCols is not specified.

helpers

A logical specifying if weights and helper data shall be handed over to fun. See details for further information.

memoryOverCPU

A logical specifying if memory usage is preferred over CPU usage for this method. The former is generally faster for smaller windows and shorter time series, the latter for bigger windows and longer time series or might even be the only way that works depending on the available hardware.

clone

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

Details

In addition to the ... argument, this method optionally 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:

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: 1 / d^p.

Value

Returns a DTSg object.

See Also

DTSg, function, cols, list

Examples

1
2
3
4
5
6
7
8
9
# new DTSg object
x <- DTSg$new(values = flow)

# calculate a 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)

DTSg documentation built on May 30, 2021, 5:06 p.m.