Inference

Tests for means

Is a mean what it is claimed to be? Do two means differ? The z and t tests answer with a p-value, the matching intervals with a range of plausible values. Here the statistic is exact, the p-value is a certified enclosure, and a decision that sits on the edge says so.

One sample, and pairs

// Student's sleep data (1908): extra hours of sleep for ten patients on two drugs
const drug1 = [0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2.0];
const drug2 = [1.9, 0.8, 1.1, 0.1, -0.1, 4.4, 5.5, 1.6, 4.6, 3.4];
import { tTest, meanInterval, show } from "@ratmath/stats";

const p = tTest(drug2, drug1, { paired: true });
String(p);             // "paired t test: p = 0.002833 (certified, two-sided)"
p.statisticSquared;    // 56169/3404: t² exactly
show(p.statistic);     // "4.062128"
p.df;                  // 9
p.interval();          // { low: 0.7001…, high: 2.4599…, level: 19/20, … }: the mean extra sleep
p.reject("0.01");      // true

For a sample x1,…,xnx_1, \dots, x_n with mean xˉ\bar x and variance s2=1n−1∑(xi−xˉ)2s^2 = \frac{1}{n-1}\sum (x_i - \bar x)^2, the t statistic for H0:μ=μ0H_0: \mu = \mu_0 is

t=xˉ−μ0s/n, t = \frac{\bar x - \mu_0}{s/\sqrt n},

and for normal data it follows Student's t distribution with n−1n - 1 degrees of freedom. A paired test is the one-sample test applied to the differences within pairs, which removes the variation between patients: here the same data give p = 0.0028 paired and p = 0.079 unpaired.

How exactness survives the square root

tt involves s2/n\sqrt{s^2/n}, which is rarely rational. Its square

t2=n(xˉ−μ0)2s2 t^2 = \frac{n(\bar x - \mu_0)^2}{s^2}

is, so that is what is kept, and the two-sided p-value is computed from it: if TT has the t distribution with ν\nu degrees of freedom, T2T^2 has the F(1,ν)F(1, \nu) distribution, so P(∣T∣≥∣t∣)=P(F1,ν≥t2)P(|T| \ge |t|) = P(F_{1,\nu} \ge t^2), evaluated as a certified enclosure at an exact argument. A one-sided p-value is half of that, or one minus half, according to the sign of xˉ−μ0\bar x - \mu_0. The z test works the same way with χ12\chi^2_1, the distribution of Z2Z^2.

Two samples: Welch, or pooled

tTest(drug2, drug1);                           // Welch: p = 0.079394, df = 17.776474…
tTest(drug2, drug1).df;                        // an exact fraction
tTest(drug2, drug1, { equalVariance: true });  // pooled: p = 0.079187, df = 18

With two independent samples the standard error of xˉ1−xˉ2\bar x_1 - \bar x_2 is s12/n1+s22/n2\sqrt{s_1^2/n_1 + s_2^2/n_2}. Welch's test uses it with the Welch–Satterthwaite degrees of freedom

ν=(s12/n1+s22/n2)2(s12/n1)2n1−1+(s22/n2)2n2−1, \nu = \frac{\left(s_1^2/n_1 + s_2^2/n_2\right)^2}{\dfrac{(s_1^2/n_1)^2}{n_1 - 1} + \dfrac{(s_2^2/n_2)^2}{n_2 - 1}},

which is an exact rational here. The pooled test assumes equal variances and uses sp2=(n1−1)s12+(n2−1)s22n1+n2−2s_p^2 = \frac{(n_1-1)s_1^2 + (n_2-1)s_2^2}{n_1+n_2-2} on n1+n2−2n_1 + n_2 - 2 degrees of freedom. When the variances differ and the smaller sample has the larger variance the pooled test rejects far too often; the testing the tests page measures by how much. Welch's test is the default.

Intervals are the tests turned around

The interval xˉ±tν, 1−α/2 s/n\bar x \pm t_{\nu,\,1-\alpha/2}\, s/\sqrt n holds exactly the values μ0\mu_0 that the two-sided test at level α\alpha keeps. The quantile and the square root are enclosures, so each end is rounded outward to a rational: the interval returned contains the true interval.

meanInterval(drug1);                       // t interval, 95%
meanInterval(drug1, { level: "0.99" });     // wider
meanInterval(drug1, { alternative: "greater" });   // { low: …, high: null }: one-sided
zTest([102, 98, 110, 105, 95, 108, 101, 99], { mu: 100, sigma: 5 });   // σ known: p = 0.203092

Textbook problems often give only a summary. meanInterval takes { n, mean, sd } (or variance) in place of the data, with the sample standard deviation (divisor n−1n - 1); with sigma it is the z interval. For power and sample sizes see Power and sample size.

meanInterval({ n: 16, mean: "25.3", sd: "4.2" });   // t interval: 23.0620 to 27.5380 (t* = 2.1314, SE = 1.05)
Try itEdit the code and run it with @ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs