A simple class for keeping track of the running mean and the sum of squared deviations from the mean for a vector.
Welford(dn, means, vars)
# S3 method for class 'Welford'
update(object, newdata, ...)initialization of the Welford object: if means
and vars are given, they are treated as the running means and
variances, and dn is their associated sample size, and if not,
dn is the dimension of the vector (with sample size 0).
a Welford object.
either a numeric vector of length d, a numeric
matrix with d columns for a group update, or another Welford
object with the same d.
additional arguments to methods.
an object of type Welford: a list with four elements:
n: Running number of observations
means: Running mean for each variable
SSDs: Running sum of squared deviations from the mean for each variable
vars: Running variance of each variable
update(Welford): Update a Welford object with new
data.
X <- matrix(rnorm(200), 20, 10)
w0 <- Welford(10)
w <- update(w0, X)
stopifnot(isTRUE(all.equal(w$means, colMeans(X))))
stopifnot(isTRUE(all.equal(w$vars, apply(X,2,var))))
w <- update(w0, X[1:12,])
w <- update(w, X[13:20,])
stopifnot(isTRUE(all.equal(w$means, colMeans(X))))
stopifnot(isTRUE(all.equal(w$vars, apply(X,2,var))))
w <- Welford(12, colMeans(X[1:12,]), apply(X[1:12,], 2, var))
w <- update(w, X[13:20,])
stopifnot(isTRUE(all.equal(w$means, colMeans(X))))
stopifnot(isTRUE(all.equal(w$vars, apply(X,2,var))))