配对样本t检验

配对样本 t 检验 (Paired Sample t -test),也称为 成对样本 t 检验配对 t 检验 ,用于比较 同一组对象在不同条件下的均值差异,以判断差异是否具有统计学意义。


1. 适用场景

配对样本 t 检验适用于 两组相关数据,常见情况包括:

  • 前后测实验设计(Pre-test & Post-test):如测试某种培训是否提升了学生的考试成绩(同一批学生测试前后的分数)。
  • 同一对象不同条件下测量(Repeated Measures):如测量同一批患者服药前后的血压变化。
  • 配对实验设计(Matched Pairs):如一组双胞胎兄弟分别接受不同的教学方法,比较他们的考试成绩。

2. 假设

  • 零假设(H₀) :两次测量的均值无显著差异,即 <math xmlns="http://www.w3.org/1998/Math/MathML"> μ d = 0 \mu_d = 0 </math>μd=0。
  • 备择假设(H₁) :两次测量的均值存在显著差异,即 <math xmlns="http://www.w3.org/1998/Math/MathML"> μ d ≠ 0 \mu_d \neq 0 </math>μd=0。

其中, <math xmlns="http://www.w3.org/1998/Math/MathML"> μ d \mu_d </math>μd 是两组配对数据的均值差。


3. 计算方法

(1) 计算差值

对于每对样本 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( X i , Y i ) (X_i, Y_i) </math>(Xi,Yi),计算差值:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> d i = X i − Y i d_i = X_i - Y_i </math>di=Xi−Yi

得到一组差值 <math xmlns="http://www.w3.org/1998/Math/MathML"> { d 1 , d 2 , ... , d n } \{ d_1, d_2, \dots, d_n \} </math>{d1,d2,...,dn}。

(2) 计算均值和标准差

计算差值的均值 <math xmlns="http://www.w3.org/1998/Math/MathML"> d ˉ \bar{d} </math>dˉ 和标准差 <math xmlns="http://www.w3.org/1998/Math/MathML"> s d s_d </math>sd:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> d ˉ = 1 n ∑ i = 1 n d i \bar{d} = \frac{1}{n} \sum_{i=1}^{n} d_i </math>dˉ=n1i=1∑ndi
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> s d = ∑ ( d i − d ˉ ) 2 n − 1 s_d = \sqrt{\frac{\sum (d_i - \bar{d})^2}{n-1}} </math>sd=n−1∑(di−dˉ)2

(3) 计算 t 统计量

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> t = d ˉ s d / n t = \frac{\bar{d}}{s_d / \sqrt{n}} </math>t=sd/n dˉ

其中, <math xmlns="http://www.w3.org/1998/Math/MathML"> n n </math>n 是配对样本的数量。

(4) 查表得 p

在自由度 <math xmlns="http://www.w3.org/1998/Math/MathML"> d f = n − 1 df = n - 1 </math>df=n−1 下查找 t 分布表,确定 p 值。

  • p 值小于显著性水平(如 0.05),则拒绝 H₀,说明两组均值存在显著差异。

4. 示例

假设某公司想评估培训对员工生产力的影响,测量培训前(X)和培训后(Y)10 名员工的产量:

员工 训练前 (X) 训练后 (Y) 差值 (d = X - Y)
1 50 55 -5
2 60 62 -2
3 45 50 -5
4 70 72 -2
5 65 68 -3
6 80 82 -2
7 75 78 -3
8 85 88 -3
9 90 93 -3
10 95 98 -3

计算均值差 <math xmlns="http://www.w3.org/1998/Math/MathML"> d ˉ = − 3.1 \bar{d} = -3.1 </math>dˉ=−3.1、标准差 <math xmlns="http://www.w3.org/1998/Math/MathML"> s d = 1.37 s_d = 1.37 </math>sd=1.37,然后计算 t 值,并根据 p 值判断是否显著。


5. 代码实现(Python)

python 复制代码
import scipy.stats as stats

# 训练前后数据
before = [50, 60, 45, 70, 65, 80, 75, 85, 90, 95]
after = [55, 62, 50, 72, 68, 82, 78, 88, 93, 98]

# 计算配对样本t检验
t_stat, p_value = stats.ttest_rel(before, after)

print(f"t 统计量: {t_stat:.4f}")
print(f"p 值: {p_value:.4f}")

如果 p 值 < 0.05,则说明培训前后存在显著差异。


6. 总结

  • 用途 :用于比较 同一组对象在不同条件下的均值差异
  • 关键点
    • 计算配对差值 <math xmlns="http://www.w3.org/1998/Math/MathML"> d i d_i </math>di
    • 计算均值差 <math xmlns="http://www.w3.org/1998/Math/MathML"> d ˉ \bar{d} </math>dˉ 和标准差 <math xmlns="http://www.w3.org/1998/Math/MathML"> s d s_d </math>sd
    • 计算 t 统计量并查找 p
  • 适用于重复测量实验、前后测设计、配对实验等

如果你的数据是两组独立样本 ,应使用独立样本 t 检验 (Independent Sample t-test)。

相关推荐
摆烂仙君2 小时前
小米2025年校招笔试真题手撕(二)
算法
xin007hoyo6 小时前
算法笔记·数学·最大公约数
笔记·算法
丶Darling.6 小时前
Day126 | 灵神 | 二叉树 | 层数最深的叶子结点的和
数据结构·c++·算法·二叉树·深度优先
小O的算法实验室7 小时前
2025年ESWA SCI1区TOP,离散人工蜂群算法+多农场多除草机器人任务分配,深度解析+性能实测
算法·智能算法改进
2401_859049088 小时前
Git使用
arm开发·git·stm32·单片机·mcu·算法
冠位观测者8 小时前
【Leetcode 每日一题】2942. 查找包含给定字符的单词
算法·leetcode·职场和发展
蓝心湄9 小时前
C语言-枚举
c语言·开发语言·算法
轮到我狗叫了9 小时前
力扣小题, 力扣113.路径总和II力扣.111二叉树的最小深度 力扣.221最大正方形力扣5.最长回文子串更加优秀的算法:中心扩展算法
算法·leetcode·深度优先
haven-8529 小时前
最大似然估计(Maximum Likelihood Estimation, MLE)详解
算法·机器学习·概率论
逐光沧海9 小时前
排序和排列——蓝桥杯备考
算法·蓝桥杯