R: A Welford accumulator for sample mean and variance

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 `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).
`object` | a `Welford` object.
`newdata` | 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.

### Value

an object of type `Welford`: a list with four elements:

1. `n`: Running number of observations

2. `means`: Running mean for each variable

3. `SSDs`: Running sum of squared deviations from the mean for each variable

4. `vars`: Running variance of each variable

### Methods (by generic)

- `update`: Update a `Welford` 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))))

[Package statnet.common version 4.6.0 Index]