通过模拟比较两均值的假设检验

R 中的数值数据推断

Mine Cetinkaya-Rundel

Associate Professor of the Practice, Duke University

动机

  • 动机问题:与常规疗法相比,胚胎干细胞治疗是否更能改善心梗后的心功能?

  • 数据:openintro 包中的 stem.cell 数据

library(openintro)
data(stem.cell)
   trmt   before   after 
1  ctrl    35.25   29.50
2  ctrl    36.50   29.50
3  ctrl    39.75   36.25
   ...      ...     ... 
n  esc     53.75   51.00
R 中的数值数据推断

分析步骤

步骤 1. 为每只羊计算 change:即心脏泵血能力的术前与术后差值。

   trmt   before   after   change 
1  ctrl    35.25   29.50   ?
2  ctrl    36.50   29.50   ?
3  ctrl    39.75   36.25   ?
   ...      ...     ...   
n  esc     53.75   51.00   ?
R 中的数值数据推断

分析步骤

步骤 2. 设定假设:

$H_0: \mu_{esc} = \mu_{ctrl}$;治疗组与对照组平均变化无差异。

$H_A: \mu_{esc} > \mu_{ctrl}$;治疗组与对照组平均变化有差异。

R 中的数值数据推断

分析步骤

步骤 3. 实施假设检验。

  • change 的数值写在 18 张卡片上。
  • (1) 洗牌并随机分成两等份:治疗组与对照组。
  • (2) 计算并记录检验统计量:治疗组与对照组 change 平均值之差。
  • 多次重复 (1) 与 (2) 以生成抽样分布。
  • 将检验统计量至少与样本均值差观测值同等极端的模拟比例作为 p 值。
R 中的数值数据推断

假设检验:生成重抽样

使用 infer 包进行检验:

library(infer)
R 中的数值数据推断

假设检验:生成重抽样

从数据框开始并指定模型:

library(infer)

diff_ht_mean <- stem.cell %>%
  specify(__) %>%                    # y ~ x
  ...
R 中的数值数据推断

假设检验:生成重抽样

声明原假设,即均值无差异:

library(infer)

diff_ht_mean <- stem.cell %>%
  specify(__) %>%                    # y ~ x
  hypothesize(null = __) %>%         # "independence" or "point"
  ...
R 中的数值数据推断

假设检验:生成重抽样

在 $H_0$ 为真下生成重抽样:

library(infer)

diff_ht_mean <- stem.cell %>%
  specify(__) %>%                    # y ~ x
  hypothesize(null = __) %>%         # "independence" or "point"
  generate(reps = __, type = __) %>% # "bootstrap", "permute", or "simulate"
  ...
R 中的数值数据推断

假设检验:生成重抽样

计算检验统计量:

library(infer)

diff_ht_mean <- stem.cell %>%
  specify(__) %>%                    # y ~ x
  hypothesize(null = __) %>%         # "independence" or "point"
  generate(reps = _N_, type = __) %>%# "bootstrap", "permute", or "simulate"
  calculate(stat = "diff in means")  # type of statistic to calculate
R 中的数值数据推断

假设检验:计算 p 值

将 p 值计算为模拟中样本均值差至少与观测值同等极端的比例:

$$P ((\bar{x}_{esc,sim} - \bar{x}_{ctrl,sim}) \ge (\bar{x}_{esc,obs} - \bar{x}_{ctrl,obs}))$$

R 中的数值数据推断

Passons à la pratique !

R 中的数值数据推断

Preparing Video For Download...