@drsimonj here to share my code for using Welch’s t-test to compare group means using summary statistics.
 Motivation
I’ve just started working with A/B tests that use big data. Where once I’d whimsically run t.test(), now my data won’t fit into memory!
I’m sharing my solution here in the hope that it might help others.
 In-memory data
As a baseline, let’s start with an in-memory case by comparing whether automatic and manual cars have different Miles Per Gallon ratings on average (using the mtcars data set).
t.test(mpg ~ am, data = mtcars)
#__ #__ Welch Two Sample t-test
#__ #__ data: mpg by am
#__ t = -3.7671, df = 18.332, p-value = 0.001374
#__ alternative hypothesis: true difference in means is not equal to 0
#__ 95 percent confidence interval:
#__ -11.280194 -3.209684
#__ sample estimates:
#__ mean in group 0 mean in group 1 #__ 17.14737 24.39231
Well… that was easy!
 Big Data
The problem with big data is that we can’t pull it into memory and work with R.
Fortunately, we don’t need the raw data to run Welch’s t-test. All we need is the mean, variance, and sample size of each group. So our raw data might have billions of rows, but we only need six numbers.
Here are the numbers we need for the previous example:
library(dplyr)
grp_summary % group_by(am) %__% summarise(
mpg_mean = mean(mpg),
mpg_var = var(mpg),
n = n()
)
grp_summary
#__ # A tibble: 2 x 4
#__ am mpg_mean mpg_var n
#__ #__ 1 0 17.14737 14.69930 19
#__ 2 1 24.39231 38.02577 13
This is everything we need to obtain a t value, degrees of freedom, and a p value.
 t value
Here we use the means, varianes, and sample sizes to compute Welch’s t:
welch_t Welch's t value of the mean difference is 3.767123
This is the same value returned by t.test(), apart from the sign (which is unimportant).
 Degrees of Freedom
Here, we use the variances and sample sizes to compute the degrees of freedom, which is estimated by the Welch–Satterthwaite equation:
welch_df Degrees of Freedom for Welch's t is 18.33225
Again, same as t.test().
 p value
We can now calculate the p value thanks to R’s pt(). Assuming we want to conduct a two-tailed test, here’s what we need to do:
welch_p p-value for Welch's t is 0.001373638
Same as t.test() again!
 All-in-one Function
Now we know the math, let’s write a function that takes 2-element vectors of means, variances, and sample sizes, and returns the results in a data frame:
welch_t_test