Compute the mean or sum over an infinite or finite sliding window, returning a vector the same size as the input.
running_sum(
v,
window = NULL,
wts = NULL,
na_rm = FALSE,
restart_period = 10000L,
check_wts = FALSE
)running_mean(
v,
window = NULL,
wts = NULL,
na_rm = FALSE,
min_df = 0L,
restart_period = 10000L,
check_wts = FALSE
)
A vector the same size as the input.
a vector.
the window size. if given as finite integer or double, passed through.
If NULL
, NA_integer_
, NA_real_
or Inf
are given, equivalent
to an infinite window size. If negative, an error will be thrown.
an optional vector of weights. Weights are ‘replication’
weights, meaning a value of 2 is shorthand for having two observations
with the corresponding v
value. If NULL
, corresponds to
equal unit weights, the default. Note that weights are typically only meaningfully defined
up to a multiplicative constant, meaning the units of weights are
immaterial, with the exception that methods which check for minimum df will,
in the weighted case, check against the sum of weights. For this reason,
weights less than 1 could cause NA
to be returned unexpectedly due
to the minimum condition. When weights are NA
, the same rules for checking v
are applied. That is, the observation will not contribute to the moment
if the weight is NA
when na_rm
is true. When there is no
checking, an NA
value will cause the output to be NA
.
whether to remove NA, false by default.
the recompute period. because subtraction of elements can cause loss of precision, the computation of moments is restarted periodically based on this parameter. Larger values mean fewer restarts and faster, though potentially less accurate results. Unlike in the computation of even order moments, loss of precision is unlikely to be disastrous, so the default value is rather large.
a boolean for whether the code shall check for negative weights, and throw an error when they are found. Default false for speed.
the minimum df to return a value, otherwise NaN
is returned,
only for the means computation.
This can be used to prevent moments from being computed on too few observations.
Defaults to zero, meaning no restriction.
Steven E. Pav shabbychef@gmail.com
Computes the mean or sum of the elements, using a Kahan's Compensated Summation Algorithm, a numerically robust one-pass method.
Given the length \(n\) vector \(x\), we output matrix \(M\) where
\(M_{i,1}\) is the sum or mean
of \(x_{i-window+1},x_{i-window+2},...,x_{i}\).
Barring NA
or NaN
, this is over a window of size window
.
During the 'burn-in' phase, we take fewer elements. If fewer than min_df
for
running_mean
, returns NA
.
Terriberry, T. "Computing Higher-Order Moments Online." https://web.archive.org/web/20140423031833/http://people.xiph.org/~tterribe/notes/homs.html
J. Bennett, et. al., "Numerically Stable, Single-Pass, Parallel Statistics Algorithms," Proceedings of IEEE International Conference on Cluster Computing, 2009. tools:::Rd_expr_doi("10.1109/CLUSTR.2009.5289161")
Cook, J. D. "Accurately computing running variance." https://www.johndcook.com/standard_deviation/
Cook, J. D. "Comparing three methods of computing standard deviation." https://www.johndcook.com/blog/2008/09/26/comparing-three-methods-of-computing-standard-deviation/
Kahan, W. "Further remarks on reducing truncation errors," Communications of the ACM, 8 (1), 1965. tools:::Rd_expr_doi("10.1145/363707.363723")
Wikipedia contributors "Kahan summation algorithm," Wikipedia, The Free Encyclopedia, https://en.wikipedia.org/w/index.php?title=Kahan_summation_algorithm&oldid=777164752 (accessed May 31, 2017).
x <- rnorm(1e5)
xs <- running_sum(x,10)
xm <- running_mean(x,100)
Run the code above in your browser using DataLab