列聯表: 卡方獨立檢定 (Contingency Table: Chi-square Test of Independence)

套路51: 列聯表: 卡方獨立檢定
(Contingency Table: Chi-square Test of Independence)

1. 使用時機: 用於檢定兩個隨機變數之間是否無關(independent)
2. 分析類型: 類別資料分析(Categorical Data Analysis)
3. 使用R計算列聯表卡方分析範例一:
咪路調查不同性別大學生頭髮顏色資料如下:
髮色
黑色
棕色
金色
紅色
總數
男生
32
43
16
9
100
女生
55
65
64
16
200
試問髮色與性別是否有關?
H0: 髮色與性別無關(independent)
HA: 髮色與性別有關。

第一步: 閱讀基本模組(base)中的chisq.test函數的使用說明。
  help(chisq.test)
第二步: 輸入建立資料
  v1 <- c(32, 43, 16, 9)
  v2 <- c(55, 65, 64, 16)
  m <- matrix(cbind(c(v1, v2)), nrow = 2, byrow = TRUE)
  # 將兩組vector組合成2 x 4矩陣。
  m
  # 顯示矩陣內容確保數值排列方式如資料表格所示。
第三步: 使用基本模組(base)chisq.test函數代入m
  chisq.test(m, simulate.p.value = TRUE, B = 2000)
  # simulate.p.value = TRUE, B = 2000估計p值。
第四步: 判讀結果
        Pearson's Chi-squared test with simulated p-value (based on 2000 replicates)
data:  m
X-squared = 8.9872, df = NA, p-value = 0.03348
  # p-value < 0.05H0: 髮色與性別無關(independent)不成立。
  # p-value > 0.05H0: 髮色與性別無關(independent)成立。

4. 注意有一種狀況是資料為2 x 2列聯表(degree of freedom = 1)時需做葉慈修正(Yates' correction)如下列範例所示:
咪路調查大學生不同性別慣用左手或右手人數資料如下:

男生
女生
總數
慣用左手
6
12
18
慣用右手
28
24
52
試問慣用左手或右手是否與性別有關?
H0: 慣用左手或右手與性別無關(independent)
HA: 慣用左手或右手與性別有關。

第一步: 閱讀基本模組(base)中的chisq.test函數的使用說明。
  help(chisq.test)
第二步: 輸入建立資料
  v1 <- c(6, 12)
  v2 <- c(28, 24)
  m <- matrix(cbind(c(v1, v2)), nrow = 2, byrow = TRUE)
  # 將兩組vector組合成2 x 2矩陣。
  m
  # 顯示矩陣內容確保數值排列方式如資料表格所示。
第三步: 使用模組(base)中的chisq.test函數代入m
  chisq.test(m, correct = TRUE, simulate.p.value = TRUE, B = 2000)
  # correct = TRUE 2 x 2列聯表(degree of freedom = 1)時需做葉慈修正(Yates' correction)
  # simulate.p.value = TRUE, B = 2000估計p值。
第四步: 判讀結果
        Pearson's Chi-squared test with simulated p-value (based on 2000 replicates)
data:  m
X-squared = 2.2524, df = NA, p-value = 0.1839
  # p-value < 0.05H0: 慣用左手或右手與性別無關(independent)不成立。
  # p-value > 0.05H0: 慣用左手或右手與性別無關(independent)成立。

來勁了嗎? 想知道更多?? 補充資料(連結):
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.

留言

這個網誌中的熱門文章

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

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

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