roll: Rolling Window function

Description Usage Arguments Examples

Description

A simple roll function, "rolling the window" across a series of data, applying a function, to produce a vector of results

Usage

1
roll(x, .f, ..., width = 1, extend = FALSE)

Arguments

x

a vector passed into .f

.f

a function or formula

If a function, it is used as is.

If a formula, e.g. ~ .x + 2, it is converted to a function with two arguments, .x or . and .y. This allows you to create very compact anonymous functions with up to two inputs.

...

further arguments to pass .f

width

the width of the window

extend

extend the window as it rolls (logical)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Simulate 4 years of daily returns
x <- rnorm(1000)
# compute the historical var across a window of a year
roll(x, var_historical, conf = 0.8, width = 250)

Really the decision lies with:
if kept simple, we map(df, ~ roll(~ .f(.x)))
but this means we cannot use roll to compute
functions that require interactions like cov(x,y)

That seems like the best way to deal with dataframes

dandermotj/mmr documentation built on June 4, 2019, 9:26 p.m.