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 值。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 數值資料推論