rollapply.DTSg | R Documentation |
Applies an arbitrary function to a rolling window of selected columns of a
DTSg
object with recognised periodicity.
## 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")
)
x |
A |
fun |
A |
... |
Further arguments passed on to |
cols |
A character vector specifying the columns whose rolling window
|
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 the method applied to calculate
the weights handed over to |
parameters |
A |
resultCols |
An optional character vector of the same length as |
suffix |
An optional character string. The return values of |
helpers |
A logical specifying if helper data shall be handed over to
|
memoryOverCPU |
A logical specifying if memory usage shall be preferred over CPU usage for this method call. 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 one which works depending on the available hardware. |
clone |
A logical specifying if the object shall be modified in place or if a deep clone (copy) shall be made beforehand. |
Returns a DTSg
object.
Currently, only one method to calculate weights is supported:
"inverseDistance"
. The distance d
of the “center” is one and
each time step further 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 parameter p
as a named element of a list
provided through
the parameters
argument: \frac{1}{d^p}
.
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
. This list
contains the following
elements:
before: Same as the before
argument.
after: Same as the after
argument.
windowSize: Size of the rolling window (before + 1L + after
).
centerIndex: Index of the “center” of the rolling window
(before + 1L
).
cols
, getOption
# 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
)$print()
## S3 method
print(rollapply(
x = x,
fun = mean,
na.rm = TRUE,
before = 2,
after = 2
))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.