Skip to contents

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

Usage

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".

Value

Returns a tibble object describing the timeseries.

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.

See also

Augment Operations:

Underlying Function:

  • slidify_vec() - The underlying function that powers tk_augment_slidify()

Examples

library(dplyr)
library(stringr)
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()
#> # A tibble: 4,032 × 7
#>    symbol date       adjusted MA_10 MA_30 MA_60 MA_90
#>    <chr>  <date>        <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1 FB     2013-01-02     28    28.9  30.0  29.7  29.1
#>  2 FB     2013-01-03     27.8  29.3  30.1  29.7  29.1
#>  3 FB     2013-01-04     28.8  29.6  30.2  29.7  29.0
#>  4 FB     2013-01-07     29.4  29.7  30.2  29.6  29.0
#>  5 FB     2013-01-08     29.1  29.8  30.3  29.6  29.0
#>  6 FB     2013-01-09     30.6  30.0  30.3  29.5  28.9
#>  7 FB     2013-01-10     31.3  30.2  30.3  29.4  28.9
#>  8 FB     2013-01-11     31.7  30.3  30.2  29.4  28.8
#>  9 FB     2013-01-14     31.0  30.4  30.1  29.3  28.8
#> 10 FB     2013-01-15     30.1  30.6  30.1  29.3  28.7
#> # … with 4,022 more rows

# 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()
#> # A tibble: 4,032 × 12
#>    symbol date       adjusted    volume adjust…¹ volum…² adjus…³ volum…⁴ adjus…⁵
#>    <chr>  <date>        <dbl>     <dbl>    <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
#>  1 FB     2013-01-02     28    69846400     28.9  7.34e7    30.0  7.57e7    29.7
#>  2 FB     2013-01-03     27.8  63140600     29.3  7.65e7    30.1  7.44e7    29.7
#>  3 FB     2013-01-04     28.8  72715400     29.6  7.81e7    30.2  7.36e7    29.7
#>  4 FB     2013-01-07     29.4  83781800     29.7  8.04e7    30.2  7.35e7    29.6
#>  5 FB     2013-01-08     29.1  45871300     29.8  8.97e7    30.3  7.43e7    29.6
#>  6 FB     2013-01-09     30.6 104787700     30.0  9.03e7    30.3  7.98e7    29.5
#>  7 FB     2013-01-10     31.3  95316400     30.2  8.80e7    30.3  8.01e7    29.4
#>  8 FB     2013-01-11     31.7  89598000     30.3  8.57e7    30.2  8.06e7    29.4
#>  9 FB     2013-01-14     31.0  98892800     30.4  8.28e7    30.1  7.93e7    29.3
#> 10 FB     2013-01-15     30.1 173242600     30.6  8.31e7    30.1  7.76e7    29.3
#> # … with 4,022 more rows, 3 more variables: volume_roll_60 <dbl>,
#> #   adjusted_roll_90 <dbl>, volume_roll_90 <dbl>, and abbreviated variable
#> #   names ¹​adjusted_roll_10, ²​volume_roll_10, ³​adjusted_roll_30,
#> #   ⁴​volume_roll_30, ⁵​adjusted_roll_60