running_correlation | R Documentation |
Computes 2nd moments and comoments, as well as the means, over an infinite or finite sliding window, returning a matrix with the correlation, covariance, regression coefficient, and so on.
running_correlation(
x,
y,
window = NULL,
wts = NULL,
na_rm = FALSE,
min_df = 0L,
restart_period = 100L,
check_wts = FALSE,
check_negative_moments = TRUE
)
running_covariance(
x,
y,
window = NULL,
wts = NULL,
na_rm = FALSE,
min_df = 0L,
used_df = 1,
restart_period = 100L,
check_wts = FALSE,
normalize_wts = TRUE,
check_negative_moments = TRUE
)
running_covariance_3(
x,
y,
window = NULL,
wts = NULL,
na_rm = FALSE,
min_df = 0L,
used_df = 1,
restart_period = 100L,
check_wts = FALSE,
normalize_wts = TRUE,
check_negative_moments = TRUE
)
running_regression_slope(
x,
y,
window = NULL,
wts = NULL,
na_rm = FALSE,
min_df = 0L,
restart_period = 100L,
check_wts = FALSE,
check_negative_moments = TRUE
)
running_regression_intercept(
x,
y,
window = NULL,
wts = NULL,
na_rm = FALSE,
min_df = 0L,
restart_period = 100L,
check_wts = FALSE,
check_negative_moments = TRUE
)
running_regression_fit(
x,
y,
window = NULL,
wts = NULL,
na_rm = FALSE,
min_df = 0L,
restart_period = 100L,
check_wts = FALSE,
check_negative_moments = TRUE
)
running_regression_diagnostics(
x,
y,
window = NULL,
wts = NULL,
na_rm = FALSE,
min_df = 0L,
used_df = 2,
restart_period = 100L,
check_wts = FALSE,
normalize_wts = TRUE,
check_negative_moments = TRUE
)
x |
a vector |
y |
a vector |
window |
the window size. if given as finite integer or double, passed through.
If |
wts |
an optional vector of weights. Weights are ‘replication’
weights, meaning a value of 2 is shorthand for having two observations
with the corresponding |
na_rm |
whether to remove NA, false by default. |
min_df |
the minimum df to return a value, otherwise |
restart_period |
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 less accurate results. |
check_wts |
a boolean for whether the code shall check for negative weights, and throw an error when they are found. Default false for speed. |
check_negative_moments |
a boolean flag. Normal computation of running
moments can result in negative estimates of even order moments due to loss of
numerical precision. With this flag active, the computation checks for negative
even order moments and restarts the computation when one is detected. This
should eliminate the possibility of negative even order moments. The
downside is the speed hit of checking on every output step. Note also the
code checks for negative moments of every even order tracked, even if they
are not output; that is if the kurtosis, say, is being computed, and a
negative variance is detected, then the computation is restarted.
Defaults to |
used_df |
the number of degrees of freedom consumed, used in the denominator of the standard errors computation. These are subtracted from the number of observations. |
normalize_wts |
a boolean for whether the weights should be
renormalized to have a mean value of 1. This mean is computed over elements
which contribute to the moments, so if |
Computes the correlation or covariance, or OLS regression coefficients and standard errors. These are computed via the numerically robust one-pass method of Bennett et. al.
Typically a matrix, usually only one row of the output value. More specifically:
Returns a single column of the covariance of x
and y
.
Returns a single column of the correlation of x
and y
.
Returns three columns: the variance of x
, the covariance of x
and y
, and the
variance of y
, in that order.
Returns a single column of the slope of the OLS regression.
Returns a single column of the intercept of the OLS regression.
Returns two columns: the regression intercept and the regression slope of the OLS regression.
Returns five columns: the regression intercept, the regression slope, the regression standard error, the standard error of the intercept, the standard error of the slope of the OLS regression.
The moment computations provided by fromo are numerically robust, but will often not provide the same results as the 'standard' implementations, due to differences in roundoff. We make every attempt to balance speed and robustness. User assumes all risk from using the fromo package.
Note that when weights are given, they are treated as replication weights.
This can have subtle effects on computations which require minimum
degrees of freedom, since the sum of weights will be compared to
that minimum, not the number of data points. Weight values
(much) less than 1 can cause computations to return NA
somewhat unexpectedly due to this condition, while values greater
than one might cause the computation to spuriously return a value
with little precision.
As this code may add and remove observations, numerical imprecision
may result in negative estimates of squared quantities, like the
second or fourth moments. By default we check for this condition
in running computations. It may also be mitigated somewhat by setting
a smaller restart_period
. Post an issue if you experience this bug.
Steven E. Pav shabbychef@gmail.com
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. \Sexpr[results=rd]{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/
x <- rnorm(1e5)
y <- rnorm(1e5) + x
rho <- running_correlation(x, y, window=100L)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.