單一樣本Z檢定 (One-Sample Z test)

套路6: 單一樣本Z檢定 (One-Sample Z test)

什麼是單一樣本假設檢定? 說白了就是只有一組資料做假設檢定。統計假設檢定檢定什麼?H0。例如單一樣本假設檢定H0 : μ = 0HA : μ ¹ 0是檢定資料的平均值是否為0。又例如單一樣本假設檢定H0 : μ < 8HA : μ ³ 8是檢定資料的平均值是否小於8。假設相等時為雙尾 (two-tailed test) 檢定。假設不相等時為單尾 (one-tailed test) 檢定。如下圖所示:
1. 使用時機: 用於比較觀測到的平均值(mean)和理論(期望)大樣本用Z檢定小樣本用t檢定。
2. 分析類型: 母數分析(parametric analysis)直接使用資料數值算統計叫parametric方法把資料排序之後用排序的名次算統計叫non-parametric方法。
3. 前提假設: 資料為常態分布(normal distribution)或接近常態分布。
4. 資料範例: 咪路調查淡水河口彈塗魚的體長(cm)資料如下:
   14.3, 15.8, 14.6, 16.1, 12.9, 15.1, 17.3, 14.0, 14.5, 13.9, 16.2, 14.3, 14.6, 13.3, 15.5, 11.8, 14.8, 13.5, 16.3, 15.4, 15.5, 13.9, 10.7, 14.8, 12.9, 15.4
   : 這種彈塗魚的平均身長為15 cm? H0: m = 15HA: m ≠ 15
5. 畫圖看資料分布:
   第一步: 用小c將資料放入名稱為Lenvector (R最基本資料結構)。用rep函數產生與資料相同數目的(26)大寫F放入名稱為Fishvector再組合成名稱為datdata frame
    Len <- c(14.3, 15.8, 14.6, 16.1, 12.9, 15.1, 17.3, 14.0, 14.5, 13.9, 16.2, 14.3, 14.6, 13.3, 15.5, 11.8, 14.8, 13.5, 16.3, 15.4, 15.5, 13.9, 10.7, 14.8, 12.9, 15.4)
Fish <- rep("F", 26)
dat <- data.frame(Len, Fish)
   第二步: 安裝ggplot2程式套件。
   第三步: 呼叫ggplot2程式套件備用。
    library(ggplot2)
   第四步: 畫圖。
    ggplot(dat, aes(x = Fish, y = Len)) +
         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函數檢查Len中資料是否為常態分布。
shapiro.test(Len)
    第三步: 判讀結果。
            Shapiro-Wilk normality test
data:  Len
W = 0.97097, p-value = 0.6485
    # p-value > 0.05H0成立,資料符合常態分布。
    # p-value < 0.05H0不成立,資料不符合常態分布。

7. 使用R計算單一樣本Z檢定:
  第一步: 安裝BSDA程式套件。
  第二步: 呼叫BSDA程式套件備用。
     library(BSDA)
  第三步: 閱讀BSDA程式套件中z.test函數的使用說明。
     help(z.test)
  第四步: 使用BSDA程式套件的z.test函數代入資料數值。
     z.test(Len, alternative = "two.sided", mu = 15, sigma.x = 1, conf.level = 0.95)
     # mu = 15是待測的期望值,H0: m = 15
  # sigma.x = 1 使用說明: Two-sided one-sample z-test where the assumed value for sigma.x is 1.
    # 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"
  第五步: 判讀結果
        One-sample z-Test
data:  Len
z = -2.4711, p-value = 0.01347
alternative hypothesis: true mean is not equal to 15
95 percent confidence interval:
 14.13100 14.89977
sample estimates: mean of x 14.51538    # 由數據推估平均值
    # p-value > 0.05H0: m = 15成立,資料符合常態分布。
    # p-value < 0.05H0: m = 15不成立,資料不符合常態分布。

來勁了嗎? 想知道更多?? 補充資料(連結):
7. 關於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
8. Zar, JH. 2010. Biostatistical Analysis, Fifth Edition, Pearson.

留言

這個網誌中的熱門文章

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

比較二或多組變異數Levene’s 檢定 (Levene’s Test for Comparing Two or More Variances)

三因子變異數分析 (Three Way ANOVA)