centcosums: Multivariate centered sums; join and unjoined.

View source: R/zzz_centsums.R

cent_cosumsR Documentation

Multivariate centered sums; join and unjoined.

Description

Compute, join, or unjoin multivariate centered (co-) sums.

Usage

cent_cosums(v, max_order = 2L, na_omit = FALSE)

cent_comoments(v, max_order = 2L, used_df = 0L, na_omit = FALSE)

join_cent_cosums(ret1, ret2)

unjoin_cent_cosums(ret3, ret2)

Arguments

v

an m by n matrix, each row an independent observation of some n variate variable.

max_order

the maximum order of cosum to compute. For now this can only be 2; in the future higher order cosums should be possible.

na_omit

a boolean; if TRUE, then only rows of v with complete observations will be used.

used_df

the number of degrees of freedom consumed, used in the denominator of the centered moments computation. These are subtracted from the number of observations.

ret1

a multdimensional array as output by cent_cosums.

ret2

a multdimensional array as output by cent_cosums.

ret3

a multdimensional array as output by cent_cosums.

Value

a multidimensional arry of dimension max_order, each side of length 1+n. For the case currently implemented where max_order must be 2, the output is a symmetric matrix, where the element in the 1,1 position is the count of (complete) rows of v, the 2:(n+1),1 column is the mean, and the 2:(n+1),2:(n+1) is the co sums matrix, which is the covariance up to scaling by the count. cent_comoments performs this normalization for you.

When na_omit is false, the output can have NA elements in it. Rows and columns without NA should be positive definite, and the 1,1 element will contain the counts of the complete observations (the number of rows of the input v). When na_omit is true, an NA in any row of v means the entire row is skipped. The output should be positive semidefinite when there is any complete row. If there are no complete observations, the output will be a matrix of all zeroes.

Note

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.

Author(s)

Steven E. Pav shabbychef@gmail.com

References

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/

See Also

cent_sums

Examples


 set.seed(1234)
 x1 <- matrix(rnorm(1e3*5,mean=1),ncol=5)
 x2 <- matrix(rnorm(1e3*5,mean=1),ncol=5)
 max_ord <- 2L
 rs1 <- cent_cosums(x1,max_ord)
 rs2 <- cent_cosums(x2,max_ord)
 rs3 <- cent_cosums(rbind(x1,x2),max_ord)
 rs3alt <- join_cent_cosums(rs1,rs2)
 stopifnot(max(abs(rs3 - rs3alt)) < 1e-7)
 rs1alt <- unjoin_cent_cosums(rs3,rs2)
 rs2alt <- unjoin_cent_cosums(rs3,rs1)
 stopifnot(max(abs(rs1 - rs1alt)) < 1e-7)
 stopifnot(max(abs(rs2 - rs2alt)) < 1e-7)


fromo documentation built on Nov. 5, 2026, 9:06 a.m.