rolling_apply: Apply Rolling Function

Description Usage Arguments Details Methods (by class) Examples

View source: R/rolling_apply.R

Description

Apply a function to the time series values in a half-open (open on the left, closed on the right) rolling time window of fixed temporal width.

Usage

1
2
3
4
5
rolling_apply(x, ...)

## S3 method for class 'uts'
rolling_apply(x, width, FUN, ..., by = NULL, align = "right",
  interior = FALSE, use_specialized = TRUE)

Arguments

x

a numeric time series object.

...

arguments passed to FUN.

width

a finite, positive duration object, specifying the temporal width of the rolling time window.

FUN

a function to be applied to the vector of observation values inside the half-open rolling time window.

by

a positive duration object. If not NULL, move the rolling time window by steps of this size forward in time, rather than by the observation time differences of x.

align

either "right", "left", or "center". Specifies whether the output times should right- or left-aligned or centered compared to their time window. Using "right" gives a causal (i.e. backward-looking) time series operator, while using "left" gives a purely forward-looking time series operator.

interior

logical. If TRUE, then FUN is only applied if the corresponding time window is in the interior of the temporal support of x, i.e. inside the time interval [start(x), end(x)].

use_specialized

logical. Whether to use a fast optimized implementation, if available. Currently, the following choices for FUN are supported: mean, median, min, max, prod, sd, sum, var

Details

A fast optimized implementation is used automatically for certain choices of FUN. See the use_specialized argument for details.

Methods (by class)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# rolling mean, sum, number of observations
rolling_apply(ex_uts(), width=ddays(1), FUN=mean)
rolling_apply(ex_uts(), width=ddays(1), FUN=sum)
rolling_apply(ex_uts(), width=ddays(1), FUN=length)

# move rolling time window by one observation vs. one day at a time
rolling_apply(ex_uts(), width=ddays(1), FUN="mean")
rolling_apply(ex_uts(), width=ddays(1), FUN="mean", by=ddays(1))

# right-align, left-aligned, and centered rolling time window
rolling_apply(ex_uts(), width=ddays(1), FUN=mean)
rolling_apply(ex_uts(), width=ddays(1), FUN=mean, align="left")
rolling_apply(ex_uts(), width=ddays(1), FUN=mean, align="center")

# restrict rolling time window to temporal support of x
rolling_apply(ex_uts(), width=ddays(1), FUN="mean", interior=TRUE)

# specialized vs. general-purpose implementation
rolling_apply(ex_uts(), width=ddays(1), FUN="mean")
rolling_apply(ex_uts(), width=ddays(1), FUN="mean", use_specialized=FALSE)    # same

andreas50/utsOperators documentation built on May 25, 2019, 7:16 a.m.