卡方適合度檢定: 異質性檢定1 (Chi-Square Goodness of Fit: Test of Heterogeneity 1)

套路48: 卡方適合度檢定: 異質性檢定1
(Chi-Square Goodness of Fit: Test of Heterogeneity 1)

1. 使用時機: 用於檢定兩組以上資料是否均質(homogeneous),如果是均質則資料可合併重算卡方適合度檢定
2. 分析類型: 類別資料分析(Categorical Data Analysis)
3. 咪路調查大學生慣用左手或右手人數,將四次調查資料用R進行卡方異質性檢定程序如下:

程序Part I. 先分別計算四次調查資料的卡方適合度檢定。
試問慣用左手或右手比例是否無差別?
H0: 慣用左手或右手比例無差別。
HA: 慣用左手或右手比例有差別。

調查1
慣用左手
慣用右手
總數

15
7
22

第一步: 輸入建立資料。
  x <- c(15, 7)
第二步: 使用stats程式套件的chisq.test函數代入x及期望值。
  chisq.test(x, p = c(1/2, 1/2), correct = FALSE)
  # p = c(1/2, 1/2)比例期望值,加總需等於1
  # p = c(1, 1)不行,因為比例期望值加總需等於1
  # correct = FALSE注意: 雖然自由度為1但此時不做葉慈修正
第三步: 判讀結果。
        Chi-squared test for given probabilities
data:  x
X-squared = 2.9091, df = 1, p-value = 0.08808

調查2
慣用左手
慣用右手
總數

16
8
24

重複上述步驟:
  x <- c(16, 8)
  chisq.test(x, p = c(1/2, 1/2), correct = FALSE)
        Chi-squared test for given probabilities
data:  x
X-squared = 2.6667, df = 1, p-value = 0.1025

調查3
慣用左手
慣用右手
總數

12
5
24

重複上述步驟:
  x <- c(12, 5)
  chisq.test(x, p = c(1/2, 1/2), correct = FALSE)
            Chi-squared test for given probabilities
data:  x
X-squared = 2.8824, df = 1, p-value = 0.08956

調查4
慣用左手
慣用右手
總數

13
5
24

重複上述步驟:
  x <- c(13, 5)
  chisq.test(x, p = c(1/2, 1/2), correct = FALSE)
        Chi-squared test for given probabilities
data:  x
X-squared = 3.5556, df = 1, p-value = 0.05935

程序Part II. 利用FunChisq程式套件中的cp.chisq.test函數進行卡方異質性檢定
H0: 四組調查資料是均質的(homogeneous)
HA: 四組調查資料不是均質的(heterogeneous)

第一步: 安裝FunChisq程式套件。
第二步: 呼叫FunChisq程式套件備用。
  library(FunChisq)
第三步: 閱讀FunChisq程式套件中的cp.chisq.test函數的說明書。
  help(cp.chisq.test)
第四步: 輸入四次調查資料(四個matrix),建立合併資料(一個list)
  w <- matrix(c(15, 7, 22/2, 22/2), nrow = 2, , byrow = TRUE)
  x <- matrix(c(16, 8, 24/2, 24/2), nrow = 2, , byrow = TRUE)
  y <- matrix(c(12, 5, 17/2, 17/2), nrow = 2, , byrow = TRUE)
  z <- matrix(c(13, 5, 18/2, 18/2), nrow = 2, , byrow = TRUE)
# 22/2, 24/2, 17/218/2分別是各組的期望值。
  m <- list(w, x, y, z)
第五步: 使用FunChisq程式套件中的cp.chisq.test函數代入m
  cp.chisq.test(m)
第六步: 判讀結果。
        Comparative chi-square heterogeneity test
data:  m
statistic = 0.15515, parameter = 3, p-value = 0.9845
  # p-value < 0.05H0: 四組調查資料是均質的(homogeneous),不成立。
  # p-value > 0.05H0: 四組調查資料是均質的(homogeneous),成立。

程序Part III. 如果卡方異質性檢定結果同意資料是均質的,則合併(加總)資料重新做卡方適合度檢定。此時要注意,資料為2 x 2列聯表(degree of freedom = 1)時需做葉慈修正(Yates' correction),如下列範例所示。咪路調查大學生慣用左手或右手人數,四組資料加總如下:

調查加總
慣用左手
慣用右手
總數

56
25
81

試問慣用左手或右手比例是否無差別?
H0: 慣用左手或右手比例無差別。
HA: 慣用左手或右手比例有差別。

第一步: 輸入建立資料。
  x <- c(56, 25)
第二步: 使用stats程式套件的chisq.test函數代入m
  chisq.test(x, p = c(1/2, 1/2), correct = TRUE)
  # correct = TRUE 2 x 2列聯表(degree of freedom = 1)時需做葉慈修正(Yates' correction)
第三步: 判讀結果。
        Chi-squared test for given probabilities
data:  x
X-squared = 11.864, df = 1, p-value = 0.0005722
  # p < 0.05H0: 慣用左手或右手比例無差別,不成立。
  # p > 0.05H0: 慣用左手或右手比例無差別,成立。
  # 注意: 四組資料分開計算時結論都是慣用左手或右手比例無差別。然而資料合併重新計算的結果推翻先前的結論。新的結論是慣用左手或右手比例有差別

來勁了嗎? 想知道更多?? 補充資料(連結):
2. 關於Pearson's chi-squared test (https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test)
4. 關於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
5. Zar, JH. 2010. Biostatistical Analysis, Fifth Edition, Pearson.

留言

這個網誌中的熱門文章

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

如何選擇統計方法 1

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