moment_stream: Online sample mean and sample variance computation

Description Usage Format Details Examples

Description

The river where you set your foot just now is gone – those waters giving way to this, now this. —Heraclitus

Usage

1

Format

An object of class R6ClassGenerator of length 24.

Details

A moment_stream object is an R6 object with reference semantics. Rather than calculating the mean and variance on a complete data set, which is difficult when the data does not fit in memory or when only parts of it are available, moment_streams use Welford's online algorithm to calculate these values during a single pass through the data (which can then be discarded if no other summary statistics are needed).

A stream can be monitored by creating an object with stream_name = moment_stream$new(), and the mean and variance can be calculated as new data comes in using stream_name$update(x). The sample mean is stored in stream_name$m and the sample variance is stored in stream_name$v_hat.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
library(Heraclitus)
set.seed(1)
x = rnorm(1E4)
moments = moment_stream$new(x)
moments$m     #sample mean of x
moments$v_hat #sample variance of x

x2 = rnorm(1E3)
moments$update(x2)
all.equal(moments$m, mean(c(x, x2)))
all.equal(moments$v_hat, mean(c(x, x2)))

davharris/Heraclitus documentation built on May 14, 2019, 9:27 p.m.