比較兩組比例 (Comparing Two-Proportions)


套路22: 比較兩組比例 (Comparing Two-Proportions)

1. 使用時機: 用於比較觀測到的兩組比例(proportion),兩組二分變量(dichotomous variables)分析。二分變量是結果只有兩種的事件。

2. 分析類型: 母數分析(parametric analysis)。直接使用資料數值算統計叫parametric方法,把資料排序之後用排序的名次算統計叫non-parametric方法。

3. 前提假設: 無。

4. 資料範例: 咪路調查兩個養雞場的蛋雞感染沙門氏菌的比例,資料如下:

養雞場1
養雞場2
加總
有沙門氏菌
18
10
28
沒沙門氏菌
6
15
21
加總
24
25
49
試問兩個養雞場的蛋雞感染沙門氏菌的比例是否有差異?
H0: p1 = p2。HA: p1 ≠ p2。

5. 使用Python比較兩組比例
方法:
import statsmodels.stats.proportion
x = [18,10]
y = [24,25]
statsmodels.stats.proportion.proportions_ztest(x, y, alternative='two-sided')
# x = [18,10]及y = [24,25]是觀測數據,亦即觀測到的比例是18/24及10/25。
# alternative = "two.sided"執行雙尾檢定,H0: p1 = p2。
# 如果要檢定: H0: p1 ≥ p2 & HA: p1 < p2或H0: p1 > p2 & HA: p1 ≤ p2,alternative = "less"。
# 如果要檢定: H0: p1 ≤ p2 & HA: p1 > p2或H0: p1 < p2 & HA: p1 ≥ p2,alternative = "greater"。
結果: (2.4748737341529163, 0.013328328780817546)
# p-value = 0.0133 < 0.05,H0: p1 = p2不成立,兩個養雞場的蛋雞感染沙門氏菌的比例沒有差異。
# p-value < 0.05,H0: p1 = p2,不成立。
# p-value > 0.05,H0: p1 = p2,成立。


留言

  1. 您好,這裡的alternative應該是分"smaller"跟"larger"喔

    alternative : str in ['two-sided', 'smaller', 'larger']
    The alternative hypothesis can be either two-sided or one of the one-
    sided tests, smaller means that the alternative hypothesis is
    ``prop < value`` and larger means ``prop > value``. In the two sample
    test, smaller means that the alternative hypothesis is ``p1 < p2`` and
    larger means ``p1 > p2`` where ``p1`` is the proportion of the first
    sample and ``p2`` of the second one.

    以上是proportions_ztest說明書的一部份

    回覆刪除

張貼留言

這個網誌中的熱門文章

比較多組不同變異數獨立樣本平均值檢定 (Welch's Test for Analysis of Variance,parametric)

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

雙因子變異數分析 (Two-Way ANOVA)