tk_augment_slidify: Add many rolling window calculations to the data

Description Usage Arguments Details Value See Also Examples

View source: R/augment-tk_augment_slidify.R

Description

Quickly use any function as a rolling function and apply to multiple .periods. Works with dplyr groups too.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
tk_augment_slidify(
  .data,
  .value,
  .period,
  .f,
  ...,
  .align = c("center", "left", "right"),
  .partial = FALSE,
  .names = "auto"
)

Arguments

.data

A tibble.

.value

One or more column(s) to have a transformation applied. Usage of tidyselect functions (e.g. contains()) can be used to select multiple columns.

.period

One or more periods for the rolling window(s)

.f

A summary [function / formula],

...

Optional arguments for the summary function

.align

Rolling functions generate .period - 1 fewer values than the incoming vector. Thus, the vector needs to be aligned. Select one of "center", "left", or "right".

.partial

.partial Should the moving window be allowed to return partial (incomplete) windows instead of NA values. Set to FALSE by default, but can be switched to TRUE to remove NA's.

.names

A vector of names for the new columns. Must be of same length as .period. Default is "auto".

Details

tk_augment_slidify() scales the slidify_vec() function to multiple time series .periods. See slidify_vec() for examples and usage of the core function arguments.

Value

Returns a tibble object describing the timeseries.

See Also

Augment Operations:

Underlying Function:

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
library(tidyverse)
library(tidyquant)
library(timetk)

# Single Column | Multiple Rolling Windows
FANG %>%
    select(symbol, date, adjusted) %>%
    group_by(symbol) %>%
    tk_augment_slidify(
        .value   = contains("adjust"),
        # Multiple rolling windows
        .period  = c(10, 30, 60, 90),
        .f       = AVERAGE,
        .partial = TRUE,
        .names   = str_c("MA_", c(10, 30, 60, 90))
    ) %>%
    ungroup()

# Multiple Columns | Multiple Rolling Windows
FANG %>%
    select(symbol, date, adjusted, volume) %>%
    group_by(symbol) %>%
    tk_augment_slidify(
        .value  = c(adjusted, volume),
        .period  = c(10, 30, 60, 90),
        .f       = AVERAGE,
        .partial = TRUE
    ) %>%
    ungroup()

timetk documentation built on Nov. 16, 2021, 9:26 a.m.