| Title: | Rolling and Expanding Statistics |
|---|---|
| Description: | Fast and efficient computation of rolling and expanding statistics for time-series data. The default algorithm in the 'roll' package is an online algorithm that, as observations are added to and removed from a window, updates statistics and discards observations from memory (Welford, 1962, <doi:10.1080/00401706.1962.10490022>; West, 1979, <doi:10.1145/359146.359153>); as a result, the amount of time to evaluate each function is significantly shorter as the computation is independent of the window. In contrast, an offline algorithm requires all observations in memory to calculate the statistic for each window, so users can trade speed for accuracy and select the offline algorithm by setting the online argument to FALSE. Quantiles are computed from the inverse of the empirical distribution function with averaging at discontinuities (Hyndman and Fan, 1996, <doi:10.1080/00031305.1996.10473566>). Use cases include rolling summary statistics, time-varying relationships between variables, and feature engineering for forecasting and signal construction. The package supports rolling and expanding windows, weights, and handling of missing values via the min_obs, complete_obs, and na_restore arguments. The implementation uses 'RcppParallel' to parallelize the online algorithms across columns and the offline algorithms across windows. |
| Authors: | Jason Foster [aut, cre] |
| Maintainer: | Jason Foster <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 1.2.2 |
| Built: | 2026-06-03 03:06:09 UTC |
| Source: | https://github.com/jasonjfoster/roll |
Fast and efficient computation of rolling and expanding statistics for time-series data. The default algorithm in the 'roll' package is an online algorithm that, as observations are added to and removed from a window, updates statistics and discards observations from memory (Welford, 1962, <doi:10.1080/00401706.1962.10490022>; West, 1979, <doi:10.1145/359146.359153>); as a result, the amount of time to evaluate each function is significantly shorter as the computation is independent of the window. In contrast, an offline algorithm requires all observations in memory to calculate the statistic for each window, so users can trade speed for accuracy and select the offline algorithm by setting the online argument to FALSE. Quantiles are computed from the inverse of the empirical distribution function with averaging at discontinuities (Hyndman and Fan, 1996, <doi:10.1080/00031305.1996.10473566>). Use cases include rolling summary statistics, time-varying relationships between variables, and feature engineering for forecasting and signal construction. The package supports rolling and expanding windows, weights, and handling of missing values via the min_obs, complete_obs, and na_restore arguments. The implementation uses 'RcppParallel' to parallelize the online algorithms across columns and the offline algorithms across windows.
roll is a package that provides fast and efficient computation of rolling and expanding statistics for time-series data.
Jason Foster [aut, cre]
Hyndman, R.J. and Fan, Y. (1996). "Sample Quantiles in Statistical Packages." The American Statistician, 50(4), 361-365. doi:10.1080/00031305.1996.10473566
Welford, B.P. (1962). "Note on a Method for Calculating Corrected Sums of Squares and Products." Technometrics, 4(3), 419-420. doi:10.1080/00401706.1962.10490022
West, D.H.D. (1979). "Updating Mean and Variance Estimates: An Improved Method." Communications of the ACM, 22(9), 532-535. doi:10.1145/359146.359153
A function for computing the rolling and expanding all of time-series data.
roll_all(x, width, min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)roll_all(x, width, min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)
x |
logical vector or matrix. Rows are observations and columns are variables. |
width |
integer. Window size. |
min_obs |
integer. Minimum number of observations required to have a value within a window,
otherwise result is |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
An object of the same class and dimension as x with the rolling and expanding
all.
n <- 15 x <- rnorm(n) # rolling all with complete windows roll_all(x < 0, width = 5) # rolling all with partial windows roll_all(x < 0, width = 5) # expanding all with partial windows roll_all(x < 0, width = n)n <- 15 x <- rnorm(n) # rolling all with complete windows roll_all(x < 0, width = 5) # rolling all with partial windows roll_all(x < 0, width = 5) # expanding all with partial windows roll_all(x < 0, width = n)
A function for computing the rolling and expanding any of time-series data.
roll_any(x, width, min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)roll_any(x, width, min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)
x |
logical vector or matrix. Rows are observations and columns are variables. |
width |
integer. Window size. |
min_obs |
integer. Minimum number of observations required to have a value within a window,
otherwise result is |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
An object of the same class and dimension as x with the rolling and expanding
any.
n <- 15 x <- rnorm(n) # rolling any with complete windows roll_any(x < 0, width = 5) # rolling any with partial windows roll_any(x < 0, width = 5) # expanding any with partial windows roll_any(x < 0, width = n)n <- 15 x <- rnorm(n) # rolling any with complete windows roll_any(x < 0, width = 5) # rolling any with partial windows roll_any(x < 0, width = 5) # expanding any with partial windows roll_any(x < 0, width = n)
A function for computing the rolling and expanding correlations of time-series data.
roll_cor(x, y = NULL, width, weights = rep(1, width), center = TRUE, scale = TRUE, min_obs = width, complete_obs = TRUE, na_restore = FALSE, online = TRUE)roll_cor(x, y = NULL, width, weights = rep(1, width), center = TRUE, scale = TRUE, min_obs = width, complete_obs = TRUE, na_restore = FALSE, online = TRUE)
x |
vector or matrix. Rows are observations and columns are variables. |
y |
vector or matrix. Rows are observations and columns are variables. |
width |
integer. Window size. |
weights |
vector. Weights for each observation within a window. |
center |
logical. If |
scale |
logical. If |
min_obs |
integer. Minimum number of observations required to have a value within a window,
otherwise result is |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
The denominator used gives an unbiased estimate of the covariance,
so if the weights are the default then the divisor n - 1 is obtained.
A cube with each slice the rolling and expanding correlations.
n <- 15 x <- rnorm(n) y <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling correlations with complete windows roll_cor(x, y, width = 5) # rolling correlations with partial windows roll_cor(x, y, width = 5, min_obs = 1) # expanding correlations with partial windows roll_cor(x, y, width = n, min_obs = 1) # expanding correlations with partial windows and weights roll_cor(x, y, width = n, min_obs = 1, weights = weights)n <- 15 x <- rnorm(n) y <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling correlations with complete windows roll_cor(x, y, width = 5) # rolling correlations with partial windows roll_cor(x, y, width = 5, min_obs = 1) # expanding correlations with partial windows roll_cor(x, y, width = n, min_obs = 1) # expanding correlations with partial windows and weights roll_cor(x, y, width = n, min_obs = 1, weights = weights)
A function for computing the rolling and expanding covariances of time-series data.
roll_cov(x, y = NULL, width, weights = rep(1, width), center = TRUE, scale = FALSE, min_obs = width, complete_obs = TRUE, na_restore = FALSE, online = TRUE)roll_cov(x, y = NULL, width, weights = rep(1, width), center = TRUE, scale = FALSE, min_obs = width, complete_obs = TRUE, na_restore = FALSE, online = TRUE)
x |
vector or matrix. Rows are observations and columns are variables. |
y |
vector or matrix. Rows are observations and columns are variables. |
width |
integer. Window size. |
weights |
vector. Weights for each observation within a window. |
center |
logical. If |
scale |
logical. If |
min_obs |
integer. Minimum number of observations required to have a value within a window,
otherwise result is |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
The denominator used gives an unbiased estimate of the covariance,
so if the weights are the default then the divisor n - 1 is obtained.
A cube with each slice the rolling and expanding covariances.
n <- 15 x <- rnorm(n) y <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling covariances with complete windows roll_cov(x, y, width = 5) # rolling covariances with partial windows roll_cov(x, y, width = 5, min_obs = 1) # expanding covariances with partial windows roll_cov(x, y, width = n, min_obs = 1) # expanding covariances with partial windows and weights roll_cov(x, y, width = n, min_obs = 1, weights = weights)n <- 15 x <- rnorm(n) y <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling covariances with complete windows roll_cov(x, y, width = 5) # rolling covariances with partial windows roll_cov(x, y, width = 5, min_obs = 1) # expanding covariances with partial windows roll_cov(x, y, width = n, min_obs = 1) # expanding covariances with partial windows and weights roll_cov(x, y, width = n, min_obs = 1, weights = weights)
A function for computing the rolling and expanding crossproducts of time-series data.
roll_crossprod(x, y = NULL, width, weights = rep(1, width), center = FALSE, scale = FALSE, min_obs = width, complete_obs = TRUE, na_restore = FALSE, online = TRUE)roll_crossprod(x, y = NULL, width, weights = rep(1, width), center = FALSE, scale = FALSE, min_obs = width, complete_obs = TRUE, na_restore = FALSE, online = TRUE)
x |
vector or matrix. Rows are observations and columns are variables. |
y |
vector or matrix. Rows are observations and columns are variables. |
width |
integer. Window size. |
weights |
vector. Weights for each observation within a window. |
center |
logical. If |
scale |
logical. If |
min_obs |
integer. Minimum number of observations required to have a value within a window,
otherwise result is |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
A cube with each slice the rolling and expanding crossproducts.
n <- 15 x <- rnorm(n) y <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling crossproducts with complete windows roll_crossprod(x, y, width = 5) # rolling crossproducts with partial windows roll_crossprod(x, y, width = 5, min_obs = 1) # expanding crossproducts with partial windows roll_crossprod(x, y, width = n, min_obs = 1) # expanding crossproducts with partial windows and weights roll_crossprod(x, y, width = n, min_obs = 1, weights = weights)n <- 15 x <- rnorm(n) y <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling crossproducts with complete windows roll_crossprod(x, y, width = 5) # rolling crossproducts with partial windows roll_crossprod(x, y, width = 5, min_obs = 1) # expanding crossproducts with partial windows roll_crossprod(x, y, width = n, min_obs = 1) # expanding crossproducts with partial windows and weights roll_crossprod(x, y, width = n, min_obs = 1, weights = weights)
A function for computing the rolling and expanding index of maximums of time-series data.
roll_idxmax(x, width, weights = rep(1, width), min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)roll_idxmax(x, width, weights = rep(1, width), min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)
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 |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
An object of the same class and dimension as x with the rolling and expanding
index of maximums.
n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling index of maximums with complete windows roll_idxmax(x, width = 5) # rolling index of maximums with partial windows roll_idxmax(x, width = 5, min_obs = 1) # expanding index of maximums with partial windows roll_idxmax(x, width = n, min_obs = 1) # expanding index of maximums with partial windows and weights roll_idxmax(x, width = n, min_obs = 1, weights = weights)n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling index of maximums with complete windows roll_idxmax(x, width = 5) # rolling index of maximums with partial windows roll_idxmax(x, width = 5, min_obs = 1) # expanding index of maximums with partial windows roll_idxmax(x, width = n, min_obs = 1) # expanding index of maximums with partial windows and weights roll_idxmax(x, width = n, min_obs = 1, weights = weights)
A function for computing the rolling and expanding index of minimums of time-series data.
roll_idxmin(x, width, weights = rep(1, width), min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)roll_idxmin(x, width, weights = rep(1, width), min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)
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 |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
An object of the same class and dimension as x with the rolling and expanding
index of minimums.
n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling index of minimums with complete windows roll_idxmin(x, width = 5) # rolling index of minimums with partial windows roll_idxmin(x, width = 5, min_obs = 1) # expanding index of minimums with partial windows roll_idxmin(x, width = n, min_obs = 1) # expanding index of minimums with partial windows and weights roll_idxmin(x, width = n, min_obs = 1, weights = weights)n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling index of minimums with complete windows roll_idxmin(x, width = 5) # rolling index of minimums with partial windows roll_idxmin(x, width = 5, min_obs = 1) # expanding index of minimums with partial windows roll_idxmin(x, width = n, min_obs = 1) # expanding index of minimums with partial windows and weights roll_idxmin(x, width = n, min_obs = 1, weights = weights)
A function for computing the rolling and expanding linear models of time-series data.
roll_lm(x, y, width, weights = rep(1, width), intercept = TRUE, min_obs = width, complete_obs = TRUE, na_restore = FALSE, online = TRUE)roll_lm(x, y, width, weights = rep(1, width), intercept = TRUE, min_obs = width, complete_obs = TRUE, na_restore = FALSE, online = TRUE)
x |
vector or matrix. Rows are observations and columns are the independent variables. |
y |
vector or matrix. Rows are observations and columns are the dependent variables. |
width |
integer. Window size. |
weights |
vector. Weights for each observation within a window. |
intercept |
logical. Either |
min_obs |
integer. Minimum number of observations required to have a value within a window,
otherwise result is |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
A list containing the following components:
coefficients |
A list of objects with the rolling and expanding coefficients for each |
r.squared |
A list of objects with the rolling and expanding r-squareds for each |
std.error |
A list of objects with the rolling and expanding standard errors for each |
n <- 15 x <- rnorm(n) y <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling regressions with complete windows roll_lm(x, y, width = 5) # rolling regressions with partial windows roll_lm(x, y, width = 5, min_obs = 1) # expanding regressions with partial windows roll_lm(x, y, width = n, min_obs = 1) # expanding regressions with partial windows and weights roll_lm(x, y, width = n, min_obs = 1, weights = weights)n <- 15 x <- rnorm(n) y <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling regressions with complete windows roll_lm(x, y, width = 5) # rolling regressions with partial windows roll_lm(x, y, width = 5, min_obs = 1) # expanding regressions with partial windows roll_lm(x, y, width = n, min_obs = 1) # expanding regressions with partial windows and weights roll_lm(x, y, width = n, min_obs = 1, weights = weights)
A function for computing the rolling and expanding maximums of time-series data.
roll_max(x, width, weights = rep(1, width), min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)roll_max(x, width, weights = rep(1, width), min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)
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 |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
An object of the same class and dimension as x with the rolling and expanding
maximums.
n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling maximums with complete windows roll_max(x, width = 5) # rolling maximums with partial windows roll_max(x, width = 5, min_obs = 1) # expanding maximums with partial windows roll_max(x, width = n, min_obs = 1) # expanding maximums with partial windows and weights roll_max(x, width = n, min_obs = 1, weights = weights)n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling maximums with complete windows roll_max(x, width = 5) # rolling maximums with partial windows roll_max(x, width = 5, min_obs = 1) # expanding maximums with partial windows roll_max(x, width = n, min_obs = 1) # expanding maximums with partial windows and weights roll_max(x, width = n, min_obs = 1, weights = weights)
A function for computing the rolling and expanding means of time-series data.
roll_mean(x, width, weights = rep(1, width), min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)roll_mean(x, width, weights = rep(1, width), min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)
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 |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
An object of the same class and dimension as x with the rolling and expanding
means.
n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling means with complete windows roll_mean(x, width = 5) # rolling means with partial windows roll_mean(x, width = 5, min_obs = 1) # expanding means with partial windows roll_mean(x, width = n, min_obs = 1) # expanding means with partial windows and weights roll_mean(x, width = n, min_obs = 1, weights = weights)n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling means with complete windows roll_mean(x, width = 5) # rolling means with partial windows roll_mean(x, width = 5, min_obs = 1) # expanding means with partial windows roll_mean(x, width = n, min_obs = 1) # expanding means with partial windows and weights roll_mean(x, width = n, min_obs = 1, weights = weights)
A function for computing the rolling and expanding medians of time-series data.
roll_median(x, width, weights = rep(1, width), min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)roll_median(x, width, weights = rep(1, width), min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)
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 |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
An object of the same class and dimension as x with the rolling and expanding
medians.
n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling medians with complete windows roll_median(x, width = 5) # rolling medians with partial windows roll_median(x, width = 5, min_obs = 1) # expanding medians with partial windows roll_median(x, width = n, min_obs = 1) ## Not run: # expanding medians with partial windows and weights roll_median(x, width = n, min_obs = 1, weights = weights) ## End(Not run)n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling medians with complete windows roll_median(x, width = 5) # rolling medians with partial windows roll_median(x, width = 5, min_obs = 1) # expanding medians with partial windows roll_median(x, width = n, min_obs = 1) ## Not run: # expanding medians with partial windows and weights roll_median(x, width = n, min_obs = 1, weights = weights) ## End(Not run)
A function for computing the rolling and expanding minimums of time-series data.
roll_min(x, width, weights = rep(1, width), min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)roll_min(x, width, weights = rep(1, width), min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)
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 |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
An object of the same class and dimension as x with the rolling and expanding
minimums.
n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling minimums with complete windows roll_min(x, width = 5) # rolling minimums with partial windows roll_min(x, width = 5, min_obs = 1) # expanding minimums with partial windows roll_min(x, width = n, min_obs = 1) # expanding minimums with partial windows and weights roll_min(x, width = n, min_obs = 1, weights = weights)n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling minimums with complete windows roll_min(x, width = 5) # rolling minimums with partial windows roll_min(x, width = 5, min_obs = 1) # expanding minimums with partial windows roll_min(x, width = n, min_obs = 1) # expanding minimums with partial windows and weights roll_min(x, width = n, min_obs = 1, weights = weights)
A function for computing the rolling and expanding products of time-series data.
roll_prod(x, width, weights = rep(1, width), min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)roll_prod(x, width, weights = rep(1, width), min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)
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 |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
An object of the same class and dimension as x with the rolling and expanding
products.
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)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)
A function for computing the rolling and expanding quantiles of time-series data.
roll_quantile(x, width, weights = rep(1, width), p = 0.5, min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)roll_quantile(x, width, weights = rep(1, width), p = 0.5, min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)
x |
vector or matrix. Rows are observations and columns are variables. |
width |
integer. Window size. |
weights |
vector. Weights for each observation within a window. |
p |
numeric. Probability between zero and one. |
min_obs |
integer. Minimum number of observations required to have a value within a window,
otherwise result is |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
The methodology for computing the quantiles is based on the inverse of the empirical distribution function with averaging at discontinuities (see "Definition 2" in Hyndman and Fan, 1996).
An object of the same class and dimension as x with the rolling and expanding
quantiles.
Hyndman, R.J. and Fan, Y. (1996). "Sample quantiles in statistical packages." American Statistician, 50(4), 361-365.
n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling quantiles with complete windows roll_quantile(x, width = 5) # rolling quantiles with partial windows roll_quantile(x, width = 5, min_obs = 1) # expanding quantiles with partial windows roll_quantile(x, width = n, min_obs = 1) ## Not run: # expanding quantiles with partial windows and weights roll_quantile(x, width = n, min_obs = 1, weights = weights) ## End(Not run)n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling quantiles with complete windows roll_quantile(x, width = 5) # rolling quantiles with partial windows roll_quantile(x, width = 5, min_obs = 1) # expanding quantiles with partial windows roll_quantile(x, width = n, min_obs = 1) ## Not run: # expanding quantiles with partial windows and weights roll_quantile(x, width = n, min_obs = 1, weights = weights) ## End(Not run)
A function for computing the rolling and expanding scaling and centering of time-series data.
roll_scale(x, width, weights = rep(1, width), center = TRUE, scale = TRUE, min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)roll_scale(x, width, weights = rep(1, width), center = TRUE, scale = TRUE, min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)
x |
vector or matrix. Rows are observations and columns are variables. |
width |
integer. Window size. |
weights |
vector. Weights for each observation within a window. |
center |
logical. If |
scale |
logical. If |
min_obs |
integer. Minimum number of observations required to have a value within a window,
otherwise result is |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
If center is TRUE then centering is done by subtracting the weighted mean from
each variable, if FALSE then zero is used. After centering, if scale is TRUE then
scaling is done by dividing by the weighted standard deviation for each variable if center is
TRUE, and the root mean square otherwise. If scale is FALSE then no scaling is
done.
The denominator used gives an unbiased estimate of the standard deviation,
so if the weights are the default then the divisor n - 1 is obtained.
An object of the same class and dimension as x with the rolling and expanding
scaling and centering.
n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling z-scores with complete windows roll_scale(x, width = 5) # rolling z-scores with partial windows roll_scale(x, width = 5, min_obs = 1) # expanding z-scores with partial windows roll_scale(x, width = n, min_obs = 1) # expanding z-scores with partial windows and weights roll_scale(x, width = n, min_obs = 1, weights = weights)n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling z-scores with complete windows roll_scale(x, width = 5) # rolling z-scores with partial windows roll_scale(x, width = 5, min_obs = 1) # expanding z-scores with partial windows roll_scale(x, width = n, min_obs = 1) # expanding z-scores with partial windows and weights roll_scale(x, width = n, min_obs = 1, weights = weights)
A function for computing the rolling and expanding standard deviations of time-series data.
roll_sd(x, width, weights = rep(1, width), center = TRUE, min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)roll_sd(x, width, weights = rep(1, width), center = TRUE, min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)
x |
vector or matrix. Rows are observations and columns are variables. |
width |
integer. Window size. |
weights |
vector. Weights for each observation within a window. |
center |
logical. If |
min_obs |
integer. Minimum number of observations required to have a value within a window,
otherwise result is |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
The denominator used gives an unbiased estimate of the standard deviation,
so if the weights are the default then the divisor n - 1 is obtained.
An object of the same class and dimension as x with the rolling and expanding
standard deviations.
n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling standard deviations with complete windows roll_sd(x, width = 5) # rolling standard deviations with partial windows roll_sd(x, width = 5, min_obs = 1) # expanding standard deviations with partial windows roll_sd(x, width = n, min_obs = 1) # expanding standard deviations with partial windows and weights roll_sd(x, width = n, min_obs = 1, weights = weights)n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling standard deviations with complete windows roll_sd(x, width = 5) # rolling standard deviations with partial windows roll_sd(x, width = 5, min_obs = 1) # expanding standard deviations with partial windows roll_sd(x, width = n, min_obs = 1) # expanding standard deviations with partial windows and weights roll_sd(x, width = n, min_obs = 1, weights = weights)
A function for computing the rolling and expanding sums of time-series data.
roll_sum(x, width, weights = rep(1, width), min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)roll_sum(x, width, weights = rep(1, width), min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)
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 |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
An object of the same class and dimension as x with the rolling and expanding
sums.
n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling sums with complete windows roll_sum(x, width = 5) # rolling sums with partial windows roll_sum(x, width = 5, min_obs = 1) # expanding sums with partial windows roll_sum(x, width = n, min_obs = 1) # expanding sums with partial windows and weights roll_sum(x, width = n, min_obs = 1, weights = weights)n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling sums with complete windows roll_sum(x, width = 5) # rolling sums with partial windows roll_sum(x, width = 5, min_obs = 1) # expanding sums with partial windows roll_sum(x, width = n, min_obs = 1) # expanding sums with partial windows and weights roll_sum(x, width = n, min_obs = 1, weights = weights)
A function for computing the rolling and expanding variances of time-series data.
roll_var(x, width, weights = rep(1, width), center = TRUE, min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)roll_var(x, width, weights = rep(1, width), center = TRUE, min_obs = width, complete_obs = FALSE, na_restore = FALSE, online = TRUE)
x |
vector or matrix. Rows are observations and columns are variables. |
width |
integer. Window size. |
weights |
vector. Weights for each observation within a window. |
center |
logical. If |
min_obs |
integer. Minimum number of observations required to have a value within a window,
otherwise result is |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
The denominator used gives an unbiased estimate of the variance,
so if the weights are the default then the divisor n - 1 is obtained.
An object of the same class and dimension as x with the rolling and expanding
variances.
n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling variances with complete windows roll_var(x, width = 5) # rolling variances with partial windows roll_var(x, width = 5, min_obs = 1) # expanding variances with partial windows roll_var(x, width = n, min_obs = 1) # expanding variances with partial windows and weights roll_var(x, width = n, min_obs = 1, weights = weights)n <- 15 x <- rnorm(n) weights <- 0.9 ^ (n:1) # rolling variances with complete windows roll_var(x, width = 5) # rolling variances with partial windows roll_var(x, width = 5, min_obs = 1) # expanding variances with partial windows roll_var(x, width = n, min_obs = 1) # expanding variances with partial windows and weights roll_var(x, width = n, min_obs = 1, weights = weights)