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
步骤 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 ?
步骤 2. 设定假设:
$H_0: \mu_{esc} = \mu_{ctrl}$;治疗组与对照组平均变化无差异。
$H_A: \mu_{esc} > \mu_{ctrl}$;治疗组与对照组平均变化有差异。
步骤 3. 实施假设检验。
change 的数值写在 18 张卡片上。change 平均值之差。使用 infer 包进行检验:
library(infer)
从数据框开始并指定模型:
library(infer)
diff_ht_mean <- stem.cell %>%
specify(__) %>% # y ~ x
...
声明原假设,即均值无差异:
library(infer)
diff_ht_mean <- stem.cell %>%
specify(__) %>% # y ~ x
hypothesize(null = __) %>% # "independence" or "point"
...
在 $H_0$ 为真下生成重抽样:
library(infer)
diff_ht_mean <- stem.cell %>%
specify(__) %>% # y ~ x
hypothesize(null = __) %>% # "independence" or "point"
generate(reps = __, type = __) %>% # "bootstrap", "permute", or "simulate"
...
计算检验统计量:
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
将 p 值计算为模拟中样本均值差至少与观测值同等极端的比例:
$$P ((\bar{x}_{esc,sim} - \bar{x}_{ctrl,sim}) \ge (\bar{x}_{esc,obs} - \bar{x}_{ctrl,obs}))$$
R 中的数值数据推断