Welford {statnet.common} | R Documentation |
A Welford accumulator for sample mean and variance
Description
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 |
object |
a |
newdata |
either a numeric vector of length |
... |
additional arguments to methods. |
Value
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
Methods (by generic)
-
update(Welford)
: Update aWelford
object with new data.
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))))