'roll' provides fast and efficient computation of rolling and expanding statistics for time-series data.
The default algorithm in the 'roll' package, and suitable for most applications, is an online algorithm. Based on the speed requirements and sequential nature of many problems in practice, online algorithms are a natural fit for computing rolling and expanding statistics of time-series data. That is, as observations are added and removed from a window, online algorithms update statistics and discard 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 faster 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. Note that online algorithms are prone to loss of precision due to round-off error; hence, users can trade speed for accuracy and select the offline algorithm by setting the online argument to FALSE. Also, 'RcppParallel' is used to parallelize the online algorithms across columns and the offline algorithms across windows.
As mentioned above, the numerical calculations use 'RcppParallel' to parallelize rolling and expanding statistics of time-series data. 'RcppParallel' provides a complete toolkit for creating safe, portable, high-performance parallel algorithms, built on top of the Intel Threading Building Blocks (TBB) and TinyThread libraries. By default, all the available cores on a machine are used for parallel algorithms. If users are either already taking advantage of parallelism or instead want to use a fixed number or proportion of threads, then set the number of threads with the RcppParallel::setThreadOptions function.
Install the released version from CRAN:
install.packages("roll")Or the development version from GitHub:
# install.packages("pak")
pak::pak("jasonjfoster/roll")Load the package and supply a dataset:
library(roll)
n <- 15
x <- rnorm(n)
y <- rnorm(n)
weights <- 0.9 ^ (n:1)Then, to compute rolling and expanding means, use the roll_mean function:
# 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 weights and partial windows
roll_mean(x, width = n, min_obs = 1, weights = weights)Or use the roll_lm function to compute rolling and expanding regressions:
# 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 weights and partial windows
roll_lm(x, y, width = n, min_obs = 1, weights = weights)Note that handling of missing values is supported as well (see the min_obs, complete_obs, and na_restore arguments).
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
