算法手册/ 通用方法/ 独立t检验

独立t检验

计算公式

符号

符号含义
n1、n2第 1 组、第 2 组的有效样本量
y1、y2两组的样本均值;第 1 组是类别升序中的前一组
s12、s22两组的样本方差,分母为 nj − 1
sp2合并方差
Δ两组均值之差,固定为第 1 组减第 2 组
SE均值差值的标准误
t、dft 统计量与自由度
α显著性水平;95% 置信区间对应 α = 0.05
zij、FLeveneLevene 检验中的绝对离差与检验统计量

核心公式

公式 (1.1):两组均值差值
\[ \Delta=\bar{y}_1-\bar{y}_2,\quad \bar{y}_j=\frac{1}{n_j}\sum_i y_{ij} \]
公式 (1.2):学生 t(方差齐)
\[ \begin{aligned} s_p^2&=\frac{(n_1-1)s_1^2+(n_2-1)s_2^2}{n_1+n_2-2}\\ \mathrm{SE}_p&=\sqrt{s_p^2\left(\frac{1}{n_1}+\frac{1}{n_2}\right)}\\ t&=\frac{\Delta}{\mathrm{SE}_p},\quad \mathrm{df}=n_1+n_2-2 \end{aligned} \]
公式 (1.3):Welch t(方差不齐)
\[ \begin{aligned} \mathrm{SE}_w&=\sqrt{\frac{s_1^2}{n_1}+\frac{s_2^2}{n_2}}\\ t&=\frac{\Delta}{\mathrm{SE}_w}\\ \mathrm{df}_w&=\frac{\left(\frac{s_1^2}{n_1}+\frac{s_2^2}{n_2}\right)^2} {\frac{\left(s_1^2/n_1\right)^2}{n_1-1}+\frac{\left(s_2^2/n_2\right)^2}{n_2-1}} \end{aligned} \]
公式 (1.4):双侧 p 值与均值差值的置信区间
\[ \begin{aligned} p&=2\cdot P\left(T_{\mathrm{df}}\ge|t|\right)\\ \mathrm{CI}&=\Delta\pm t_{1-\alpha/2,\,\mathrm{df}}\cdot\mathrm{SE} \end{aligned} \]
公式 (1.5):Cohen's d
\[ \text{Cohen's } d=\frac{\bar{y}_1-\bar{y}_2}{\sqrt{s_p^2}} \]
公式 (1.6):Levene 检验(中心取均值)
\[ z_{ij}=\left|y_{ij}-\bar{y}_j\right|,\quad F_{\mathrm{Levene}}=\frac{\mathrm{MS}_{\mathrm{between}}(z)}{\mathrm{MS}_{\mathrm{within}}(z)} \]

参考文献

  1. Student (1908). The probable error of a mean. Biometrika, 6(1), 1–25.
  2. Welch, B. L. (1947). The generalization of “Student's” problem when several different population variances are involved. Biometrika, 34(1–2), 28–35.
  3. Levene, H. (1960). Robust tests for equality of variances. In I. Olkin (Ed.), Contributions to Probability and Statistics (pp. 278–292). Stanford University Press.
  4. Cohen, J. (1988). Statistical Power Analysis for the Behavioral Sciences (2nd ed.). Lawrence Erlbaum Associates.

附录 A:Welch t检验

符号

符号含义
n1、n2两组的有效样本量
x1、x2两组均值
s12、s22两组样本方差(分母 n−1)
Δ平均值差值,Δ = x̄1 − x̄2
tWelch t 统计量
dfWelch–Satterthwaite 近似自由度
α显著性水平,置信水平 = 1 − α

核心公式

公式 (A.1):平均值差值
\[ \Delta=\bar{x}_1-\bar{x}_2 \]
公式 (A.2):Welch t 统计量
\[ t=\frac{\Delta}{\sqrt{\dfrac{s_1^2}{n_1}+\dfrac{s_2^2}{n_2}}} \]
公式 (A.3):Welch–Satterthwaite 自由度
\[ \mathrm{df}=\frac{\left(\dfrac{s_1^2}{n_1}+\dfrac{s_2^2}{n_2}\right)^2}{\dfrac{\left(s_1^2/n_1\right)^2}{n_1-1}+\dfrac{\left(s_2^2/n_2\right)^2}{n_2-1}} \]
公式 (A.4):差值的置信区间
\[ \Delta\pm t_{1-\alpha/2,\,\mathrm{df}}\cdot\sqrt{\dfrac{s_1^2}{n_1}+\dfrac{s_2^2}{n_2}} \]

附录 B:独立样本z检验

符号

符号含义
n1、n2两组的有效样本量
x1、x2两组均值
σ1、σ2两组总体的已知标准差
Δ平均值差值,Δ = x̄1 − x̄2
z检验统计量
dCohen's d,以合并的已知 σ 为尺度

核心公式

公式 (B.1):平均值差值
\[ \Delta=\bar{x}_1-\bar{x}_2 \]
公式 (B.2):独立样本 z 统计量
\[ z=\frac{\Delta}{\sqrt{\dfrac{\sigma_1^2}{n_1}+\dfrac{\sigma_2^2}{n_2}}} \]
公式 (B.3):差值的置信区间
\[ \Delta\pm z_{1-\alpha/2}\cdot\sqrt{\dfrac{\sigma_1^2}{n_1}+\dfrac{\sigma_2^2}{n_2}} \]
公式 (B.4):Cohen's d
\[ d=\frac{\bar{x}_1-\bar{x}_2}{\sqrt{(\sigma_1^2+\sigma_2^2)/2}} \]

算法参数

参数取值默认影响
缺失处理 成对剔除 / 按列表剔除 成对剔除 成对剔除时每个因变量使用各自的有效样本;按列表剔除时共用同一批样本。
置信水平 0 到 1 之间的小数 0.95 只改变均值差值的置信区间,不改变 t、df 和 p。

验证报告

数据与 R 包

数据文件 ttest.csv
样本与变量 N = 80;X 二分类分组(A、B 各 40);Y1、Y2 定量因变量
R 包 car 3.1.5、effsize 0.8.1

提示词

以 X 为分组变量,对 Y1 和 Y2 分别做独立样本 t 检验,报告均值、标准差、Levene 检验、t、df、p、95% 置信区间和 Cohen's d。

R 代码

library(car)
library(effsize)
d <- read.csv("ttest.csv")
d$X <- factor(d$X)
leveneTest(Y1 ~ X, data = d, center = mean)
t.test(Y1 ~ X, data = d, var.equal = TRUE)
cohen.d(Y1 ~ X, data = d)
leveneTest(Y2 ~ X, data = d, center = mean)
t.test(Y2 ~ X, data = d, var.equal = FALSE)
cohen.d(Y2 ~ X, data = d)

对比结果

指标SPSSAUR差值
Y1 均值(组 1)49.031962275049.03196227507.11e−15
Y1 均值(组 2)52.990233425052.99023342501.42e−14
Y1 标准差(组 1)10.459743288610.45974328863.55e−14
Y1 标准差(组 2)10.487803350410.48780335044.26e−14
Y1 Levene F0.00792861980.00792861981.03e−15
Y1 Levene p0.92927613180.92927613182.03e−15
Y1 t(学生 t)−1.6901178122−1.69011781223.77e−15
Y1 df78780
Y1 p0.09499851230.09499851232.89e−15
Y1 95% CI 下限−8.6208529296−8.62085292979.34e−11
Y1 95% CI 上限0.70431062960.70431062979.33e−11
Y1 Cohen's d−0.3779218318−0.37792183182.78e−16
Y2 均值(组 1)51.365603325051.36560332507.11e−15
Y2 均值(组 2)57.895066825057.89506682500
Y2 标准差(组 1)4.51509161294.51509161292.66e−15
Y2 标准差(组 2)15.028169806915.02816980693.55e−15
Y2 Levene F38.885645235638.88564523562.84e−14
Y2 Levene p(差值按 −log10)2.148803e−82.148803e−86.25e−10
Y2 t(Welch t)−2.6316934572−2.63169345722.22e−15
Y2 df45.983792540645.98379254061.42e−14
Y2 p0.01152579470.01152579477.33e−15
Y2 95% CI 下限−11.5236824568−11.52368245683.02e−14
Y2 95% CI 上限−1.5352445432−1.53524454329.77e−15
Y2 Cohen's d−0.5884645466−0.58846454669.99e−16

结论:本次验证的是独立样本 t 检验,学生 t 与 Welch t 两条路径都已核对;独立样本 z 检验见附录。Y1 由 Levene 检验判定方差齐、采用学生 t,Y2 判定方差不齐、采用 Welch t,两条路径的 t、df、p、置信区间与 Cohen's d 均与 R 一致,最大差值 6.3e−10。

操作手册 问题反馈

引用SPSSAU. 独立t检验:计算公式与参数[EB/OL]. SPSSAU算法手册, 2026-11-19. https://www.spssau.com/algorithm/general/ttest.html.