A simple class for keeping track of the running mean and the sum of squared deviations from the mean for a vector.
Usage
Welford(dn, means, vars)
# S3 method for class 'Welford'
update(object, newdata, ...)Arguments
- dn, means, vars
initialization of the Welford object: if
meansandvarsare given, they are treated as the running means and variances, anddnis their associated sample size, and if not,dnis the dimension of the vector (with sample size 0).- object
a
Welfordobject.- newdata
either a numeric vector of length
d, a numeric matrix withdcolumns for a group update, or anotherWelfordobject with the samed.- ...
additional arguments to methods.
Value
an object of type Welford: a list with four elements:
n: Running number of observationsmeans: Running mean for each variableSSDs: Running sum of squared deviations from the mean for each variablevars: Running variance of each variable
Examples
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))))