| Type: | Package |
| Title: | Rolling and Expanding Statistics |
| Version: | 1.2.2 |
| 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 because the computation is independent of the window size. 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. |
| License: | GPL-2 | GPL-3 [expanded from: GPL (≥ 2)] |
| URL: | https://github.com/jasonjfoster/roll |
| BugReports: | https://github.com/jasonjfoster/roll/issues |
| Depends: | R (≥ 3.5.0) |
| Imports: | Rcpp, RcppParallel |
| Suggests: | covr, testthat, zoo |
| LinkingTo: | Rcpp, RcppArmadillo, RcppParallel |
| Config/roxygen2/old_usage: | TRUE |
| Config/roxygen2/version: | 8.1.1 |
| Encoding: | UTF-8 |
| SystemRequirements: | GNU make |
| NeedsCompilation: | yes |
| Packaged: | 2026-11-28 14:18:31 UTC; jason |
| Author: | Jason Foster [aut, cre] |
| Maintainer: | Jason Foster <jason.j.foster@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-11-29 00:30:02 UTC |
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 because the computation is independent of the window size. 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.
Details
roll is a package that provides fast and efficient computation of rolling and expanding statistics for time-series data.
Author(s)
Jason Foster [aut, cre]
References
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
Rolling All
Description
A function for computing the rolling and expanding all of time-series data.
Usage
roll_all(x, width, min_obs = width, complete_obs = FALSE,
na_restore = FALSE, online = TRUE)
Arguments
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. |
Value
An object of the same class and dimension as x with the rolling and expanding
all.
Examples
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, min_obs = 1)
# expanding all with partial windows
roll_all(x < 0, width = n, min_obs = 1)
Rolling Any
Description
A function for computing the rolling and expanding any of time-series data.
Usage
roll_any(x, width, min_obs = width, complete_obs = FALSE,
na_restore = FALSE, online = TRUE)
Arguments
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. |
Value
An object of the same class and dimension as x with the rolling and expanding
any.
Examples
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, min_obs = 1)
# expanding any with partial windows
roll_any(x < 0, width = n, min_obs = 1)
Rolling Correlations
Description
A function for computing the rolling and expanding correlations of time-series data.
Usage
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)
Arguments
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. |
Details
The denominator used gives an unbiased estimate of the covariance,
so if the weights are the default then the divisor n - 1 is obtained.
The online algorithm is used for equal or exponential decay weights, otherwise the offline
algorithm is used with a warning if online is TRUE.
Value
A cube with each slice the rolling and expanding correlations.
Examples
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)
Rolling Covariances
Description
A function for computing the rolling and expanding covariances of time-series data.
Usage
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)
Arguments
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. |
Details
The denominator used gives an unbiased estimate of the covariance,
so if the weights are the default then the divisor n - 1 is obtained.
The online algorithm is used for equal or exponential decay weights, otherwise the offline
algorithm is used with a warning if online is TRUE.
Value
A cube with each slice the rolling and expanding covariances.
Examples
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)
Rolling Cross-Products
Description
A function for computing the rolling and expanding cross-products of time-series data.
Usage
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)
Arguments
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. |
Details
The online algorithm is used for equal or exponential decay weights, otherwise the
offline algorithm is used with a warning if online is TRUE.
Value
A cube with each slice the rolling and expanding cross-products.
Examples
n <- 15
x <- rnorm(n)
y <- rnorm(n)
weights <- 0.9 ^ (n:1)
# rolling cross-products with complete windows
roll_crossprod(x, y, width = 5)
# rolling cross-products with partial windows
roll_crossprod(x, y, width = 5, min_obs = 1)
# expanding cross-products with partial windows
roll_crossprod(x, y, width = n, min_obs = 1)
# expanding cross-products with partial windows and weights
roll_crossprod(x, y, width = n, min_obs = 1, weights = weights)
Rolling Index of Maximums
Description
A function for computing the rolling and expanding index of maximums of time-series data.
Usage
roll_idxmax(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 |
complete_obs |
logical. If |
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
index of maximums.
Examples
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)
Rolling Index of Minimums
Description
A function for computing the rolling and expanding index of minimums of time-series data.
Usage
roll_idxmin(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 |
complete_obs |
logical. If |
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
index of minimums.
Examples
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)
Rolling Linear Models
Description
A function for computing the rolling and expanding linear models of time-series data.
Usage
roll_lm(x, y, width, weights = rep(1, width), intercept = TRUE,
min_obs = width, complete_obs = TRUE, na_restore = FALSE,
online = TRUE)
Arguments
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. |
Details
complete_obs = FALSE is not supported, so a warning is given and the argument is
ignored. The online algorithm is used for equal or exponential decay weights, otherwise the
offline algorithm is used with a warning if online is TRUE.
Value
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 |
Examples
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)
Rolling Maximums
Description
A function for computing the rolling and expanding maximums of time-series data.
Usage
roll_max(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 |
complete_obs |
logical. If |
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
maximums.
Examples
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)
Rolling Means
Description
A function for computing the rolling and expanding means of time-series data.
Usage
roll_mean(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 |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
Details
The online algorithm is used for equal or exponential decay weights, otherwise the
offline algorithm is used with a warning if online is TRUE.
Value
An object of the same class and dimension as x with the rolling and expanding
means.
Examples
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)
Rolling Medians
Description
A function for computing the rolling and expanding medians of time-series data.
Usage
roll_median(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 |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
Details
The online algorithm is used for equal weights, otherwise an error is given if
online is TRUE.
Value
An object of the same class and dimension as x with the rolling and expanding
medians.
Examples
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)
# expanding medians with partial windows and weights
roll_median(x, width = n, min_obs = 1, weights = weights, online = FALSE)
Rolling Minimums
Description
A function for computing the rolling and expanding minimums of time-series data.
Usage
roll_min(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 |
complete_obs |
logical. If |
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
minimums.
Examples
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)
Rolling Products
Description
A function for computing the rolling and expanding products of time-series data.
Usage
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 |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
Details
The online algorithm is used for equal or exponential decay weights, otherwise the
offline algorithm is used with a warning if online is TRUE.
Value
An object of the same class and dimension as x with the rolling and expanding
products.
Examples
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)
Rolling Quantiles
Description
A function for computing the rolling and expanding quantiles of time-series data.
Usage
roll_quantile(x, width, weights = rep(1, width), p = 0.5,
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. |
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. |
Details
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).
The online algorithm is used for equal weights, otherwise an error is given if
online is TRUE, unless p is zero or one.
Value
An object of the same class and dimension as x with the rolling and expanding
quantiles.
References
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
Examples
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)
# expanding quantiles with partial windows and weights
roll_quantile(x, width = n, min_obs = 1, weights = weights, online = FALSE)
Rolling Scaling and Centering
Description
A function for computing the rolling and expanding scaling and centering of time-series data.
Usage
roll_scale(x, width, weights = rep(1, width), center = TRUE,
scale = TRUE, 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. |
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. |
Details
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.
The online algorithm is used for equal or exponential decay weights, otherwise the offline
algorithm is used with a warning if online is TRUE.
Value
An object of the same class and dimension as x with the rolling and expanding
scaling and centering.
Examples
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)
Rolling Standard Deviations
Description
A function for computing the rolling and expanding standard deviations of time-series data.
Usage
roll_sd(x, width, weights = rep(1, width), center = TRUE,
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. |
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. |
Details
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.
The online algorithm is used for equal or exponential decay weights, otherwise the offline
algorithm is used with a warning if online is TRUE.
Value
An object of the same class and dimension as x with the rolling and expanding
standard deviations.
Examples
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)
Rolling Sums
Description
A function for computing the rolling and expanding sums of time-series data.
Usage
roll_sum(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 |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
Details
The online algorithm is used for equal or exponential decay weights, otherwise the
offline algorithm is used with a warning if online is TRUE.
Value
An object of the same class and dimension as x with the rolling and expanding
sums.
Examples
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)
Rolling Variances
Description
A function for computing the rolling and expanding variances of time-series data.
Usage
roll_var(x, width, weights = rep(1, width), center = TRUE,
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. |
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. |
Details
The denominator used gives an unbiased estimate of the variance,
so if the weights are the default then the divisor n - 1 is obtained.
The online algorithm is used for equal or exponential decay weights, otherwise the offline
algorithm is used with a warning if online is TRUE.
Value
An object of the same class and dimension as x with the rolling and expanding
variances.
Examples
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)