成對樣本t檢定 (Paired Sample t test)

套路13: 成對樣本 檢定 (Paired Sample t test)

什麼是兩組成對資料假設檢定? 說白了就是兩組成對的資料做比較的假設檢定。統計假設檢定檢定什麼?H0。例如兩組獨立樣本假設檢定H0 : μ1 = μ2HA : μ1 ¹ μ2是檢定兩組資料的平均值是否相同。又例如兩組獨立樣本假設檢定H0 : μ1 < μ2HA : μ1 ³ μ2是檢定第一組資料的平均值是否小於第二組資料的平均值。假設相等時為雙尾 (two-tailed test) 檢定。假設不相等時為單尾 (one-tailed test) 檢定。如下圖所示:
1. 使用時機: 用於比較觀測到的成對資料平均值(mean)
2. 分析類型: 母數分析(parametric analysis)直接使用資料數值算統計叫parametric方法,把資料排序之後用排序的名次算統計叫non-parametric方法。
3. 前提假設: 兩組資料為成對資料且均為常態分布(normal distribution)
4. 資料範例: 咪路測量幫愛斯基摩人拉車的狗狗前腿與後腿長度,資料如下:
狗狗
後腿(cm)
前腿(cm)
1
14.2
13.8
2
14.0
13.6
3
14.4
14.7
4
14.4
13.9
5
14.2
14.3
6
14.6
14.1
7
14.9
14.3
8
15.0
14.5
9
14.2
13.6
10
14.8
14.6
H0拉車的狗狗前腿與後腿長度沒差。HA拉車的狗狗前腿與後腿長度有差。

5. 畫圖看資料分布:
第一步: 用小c將資料放入名稱為h1f1vector (R最基本資料結構)。用rep函數產生與資料相同
  數目的(10)大寫HF放入名稱為h2f2vector再組合成名稱為datdata frame
h1 <- c(14.2, 14.0, 14.4, 14.4, 14.2, 14.6, 14.9, 15.0, 14.2, 14.8)
f1 <- c(13.8, 13.6, 14.7, 13.9, 14.3, 14.1, 14.3, 14.5, 13.6, 14.6)
h2 <- rep("H", 10)
f2 <- rep("F", 10)
Length <- c(h1, f1)
Leg <- c(h2, f2)
dat <- data.frame(Length, Leg)
第二步: 安裝ggplot2程式套件。
第三步: 呼叫ggplot2程式套件備用。
library(ggplot2)
第四步: 畫圖。
ggplot(dat, aes(x = Leg, y = Length)) +
   geom_boxplot(color = "red")+
   geom_jitter(position = position_jitter(0.05))

# 同時畫x-y散布(黑色點)圖及盒圖(紅色box plot)
# ggplot2程式套件geom_jitter函數讓重疊(數值相同)的資料點錯開,避免誤判

6. 檢查資料是否為常態分布:
第一步: 閱讀基本模組(base)shapiro.test函數的說明書。
  help(shapiro.test)
第二步: 使用基本模組(base)shapiro.test函數檢查h1u1中資料是否為常態分布。
  shapiro.test(h1)
  shapiro.test(f1)
第三步: 判讀結果。
            Shapiro-Wilk normality test
data:  h1
W = 0.92581, p-value = 0.408  # p-value > 0.05,資料符合常態分布。
        Shapiro-Wilk normality test
data:  f1
W = 0.93063, p-value = 0.4541  # p-value > 0.05,資料符合常態分布。
  # p-value > 0.05,資料符合常態分布。
  # p-value < 0.05,資料不符合常態分布。

7. 使用R計算成對樣本t檢定:
第一步: 閱讀基本模組(base)中的t.test函數的使用說明。
  help(t.test)
第二步: 使用stats程式套件的prop.test函數代入資料數值。
  t.test(h1, f1, alternative = "two.sided", paired = TRUE)
  # paired = TRUE樣本為成對資料。
  # alternative = "two.sided" 執行雙尾檢定。
  # 如果要檢定: H0: m ≥ 15 & HA: m < 15H0: m > 15 & HA: m ≤ 15alternative = "less"
  # 如果要檢定: H0: m ≤ 15 & HA: m > 15H0: m < 15 & HA: m ≥ 15alternative = "greater"
第三步: 判讀結果
        Paired t-test
data:  h1 and f1
t = 3.4138, df = 9, p-value = 0.007703  
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 0.1113248 0.5486752
sample estimates:
mean of the differences   0.33
  # p-value < 0.05H0拉車的狗狗前腿與後腿長度沒差不成立。
  # p-value > 0.05H0拉車的狗狗前腿與後腿長度沒差成立。

來勁了嗎? 想知道更多?? 補充資料(連結):
3. 關於R基礎R繪圖及統計快速入門:
   b. Cookbook for R: http://www.cookbook-r.com/
   d. Statistical tools for high-throughput data analysis (STHDA): http://www.sthda.com/english/
e. The Handbook of Biological Statistics: http://www.biostathandbook.com/
f. An R Companion for the Handbook of Biological Statistics: http://rcompanion.org/rcompanion/index.html
4. Zar, JH. 2010. Biostatistical Analysis, Fifth Edition, Pearson.

留言

這個網誌中的熱門文章

統計不球人 目錄 (Table of Contents)

如何選擇統計方法 1

如何檢查資料是否接近常態分布 (Normality Test using R)