roll_prod: Rolling Products

Description Usage Arguments Value Examples

View source: R/roll.R

Description

A function for computing the rolling and expanding products of time-series data.

Usage

1
2
roll_prod(x, width, weights = rep(1, width), min_obs = width,
  complete_obs = FALSE, na_restore = FALSE, online = TRUE)

Arguments

x

vector or matrix. Rows are observations and columns are variables.

width

integer. Window size.

weights

vector. Weights for each observation within a window.

min_obs

integer. Minimum number of observations required to have a value within a window, otherwise result is NA.

complete_obs

logical. If TRUE then rows containing any missing values are removed, if FALSE then each value is used.

na_restore

logical. Should missing values be restored?

online

logical. Process observations using an online algorithm.

Value

An object of the same class and dimension as x with the rolling and expanding products.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
n <- 15
x <- rnorm(n)
weights <- 0.9 ^ (n:1)

# rolling products with complete windows
roll_prod(x, width = 5)

# rolling products with partial windows
roll_prod(x, width = 5, min_obs = 1)

# expanding products with partial windows
roll_prod(x, width = n, min_obs = 1)

# expanding products with partial windows and weights
roll_prod(x, width = n, min_obs = 1, weights = weights)

Example output

 [1]           NA           NA           NA           NA -0.021808468
 [6]  0.158026328 -0.048749341 -0.015620696 -0.006469362 -0.187674764
[11] -0.052786934  0.050418075  0.033093285  0.067464126  0.005949360
 [1]  0.139780481 -0.263427397 -0.507157189  0.512125227 -0.021808468
 [6]  0.158026328 -0.048749341 -0.015620696 -0.006469362 -0.187674764
[11] -0.052786934  0.050418075  0.033093285  0.067464126  0.005949360
 [1]  1.397805e-01 -2.634274e-01 -5.071572e-01  5.121252e-01 -2.180847e-02
 [6]  2.208900e-02  1.284191e-02  7.922148e-03 -3.313123e-03  4.092899e-03
[11] -1.166010e-03  6.474645e-04  2.621699e-04 -2.235170e-04  2.435013e-05
 [1]  1.258024e-01 -1.920386e-01 -2.695241e-01  1.785670e-01 -4.490170e-03
 [6]  2.416956e-03  6.720784e-04  1.784733e-04 -2.891682e-05  1.245573e-05
[11] -1.113546e-06  1.746352e-07  1.797427e-08 -3.505694e-09  7.863256e-11

roll documentation built on July 13, 2020, 5:09 p.m.