以模擬比較兩母體平均數的假設檢定

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. 進行假設檢定。

  • 在 18 張卡片上寫下各自的 change 值。
  • (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 數值資料推論

一起來練習吧!

R 數值資料推論

Preparing Video For Download...