Welford | R Documentation |
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, ...)
dn , means , vars |
initialization of the Welford object: if |
object |
a |
newdata |
either a numeric vector of length |
... |
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))))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.