比較兩組獨立樣本變異數 (Comparing Two Sample variances,parametric and non-parametric)


套路 19: 比較兩組獨立樣本變異數
(Comparing Two Sample variancesparametric and non-parametric)

什麼是比較兩組獨立樣本變異數檢定? 說白了就是兩組分別獨立取樣的資料做比較的假設檢定。**注意** ”比較變異數” (comparing variances)變異數分析” (analysis of variance)不同。變異數分析是多組資料比較平均值。統計假設檢定檢定什麼?H0。例如兩組獨立樣本假設檢定H0 : s12 = s22HA : s12 ¹ s22是檢定兩組資料的變異數是否相同。又例如兩組獨立樣本假設檢定H0 : s12  < s22HA : s12  ³ s22是檢定第一組資料的變異數是否小於第二組資料的變異數。假設相等時為雙尾 (two-tailed test) 檢定。假設不相等時為單尾 (one-tailed test) 檢定。如下圖所示:
1. 使用時機: 用於比較兩組變異數相同獨立樣本變異數(variances)
2. 分析類型: 母數(parametric)分析及無母數(non-parametric)分析。直接使用資料數值算統計叫parametric方法,把資料排序之後用排序的名次算統計叫non-parametric方法。
3. 前提假設: 使用母數(parametric)分析時資料須為常態分布(normal distribution)
4. 資料範例: 咪路調查高一和大一學生體重(kg),資料如下:
高一
41
35
33
36
40
46
31
37
34
30
38
大一
52
57
62
55
64
57
56
55
60
59

請問兩組體重變異數是否相同? H0 : s12 = s22HA : s12 ¹ s22。相同變異數表示樣本來自相同母體(population),不同變異數表示樣本取樣自不同母體。
5. 畫圖看資料分布:
wt = [41, 35, 33, 36, 40, 46, 31, 37, 34, 30, 38, 52, 57, 62, 55, 64, 57, 56, 55, 60, 59]
cl = ["H","H","H","H","H","H","H","H","H","H","H","U","U","U","U","U","U","U","U","U","U"]
dat = {'Weight':wt,'Class':cl}
import pandas as pd
df = pd.DataFrame(dat)
import seaborn as sns
sns.set(style="whitegrid")
ax = sns.boxplot(x = "Class", y = "Weight", data = df, width=0.2, palette="Set3")
ax = sns.swarmplot(x = "Class", y = "Weight", data = df, color = "red")

結果:


6. 檢查資料是否為常態分布 (H0:資料為常態分佈):
dat1 = [41, 35, 33, 36, 40, 46, 31, 37, 34, 30, 38]
dat2 = [52, 57, 62, 55, 64, 57, 56, 55, 60, 59]
import scipy.stats
scipy.stats.shapiro(dat1)
結果: (0.9705673456192017, 0.8921969532966614)
p = 0.8922 > 0.05,接受H0:資料為常態分佈。
scipy.stats.shapiro(dat2)
結果: (0.9728113412857056, 0.9156168699264526)
p = 0.9156 > 0.05,接受H0:資料為常態分佈。

7. Python比較資料是否為相同變異數 (H0: s12 = s22):
方法一: F test for equal variances (parametric test)
dat1 = [41, 35, 33, 36, 40, 46, 31, 37, 34, 30, 38]
dat2 = [52, 57, 62, 55, 64, 57, 56, 55, 60, 59]
import scipy.stats
import statistics
x = statistics.variance(dat1)
y = statistics.variance(dat2)
f = x / y
df1 = len(dat1) - 1
df2 = len(dat2) - 1
p_value = scipy.stats.f.cdf(f, df1, df2)
p_value
結果: 0.7799525392149749
p = 0.7799 > 0.025,接受H0: s12 = s22
# 如果是單尾檢定H0: s12 > s22H0: s12 < s22p > 0.05為接受H0
# 相同變異數表示樣本來自相同母體(population),不同變異數表示樣本取樣自不同母體。

方法二: Levene test for equal variances (parametric test)
dat1 = [41, 35, 33, 36, 40, 46, 31, 37, 34, 30, 38]
dat2 = [52, 57, 62, 55, 64, 57, 56, 55, 60, 59]
import scipy.stats
scipy.stats.levene(dat1, dat2, center = 'mean')
結果: LeveneResult(statistic=0.492665294888687, pvalue=0.4912503045382468)
p = 0.491 > 0.05,接受H0: s12 = s22

方法三: Bartlett's test for equal variances (robust parametric test)
dat1 = [41, 35, 33, 36, 40, 46, 31, 37, 34, 30, 38]
dat2 = [52, 57, 62, 55, 64, 57, 56, 55, 60, 59]
import scipy.stats
scipy.stats.bartlett(dat1, dat2)
結果: BartlettResult(statistic=0.6144311390277282, pvalue=0.4331242694631622)
p = 0.433 > 0.05,接受H0: s12 = s22

方法四: Fligner-Killeen test for equality of variance (non-parametric test)
dat1 = [41, 35, 33, 36, 40, 46, 31, 37, 34, 30, 38]
dat2 = [52, 57, 62, 55, 64, 57, 56, 55, 60, 59]
import scipy.stats
scipy.stats.fligner(dat1, dat2, center = 'median')
結果: FlignerResult(statistic=0.4132372037738178, pvalue=0.5203309848503048)
p = 0.52 > 0.05,接受H0: s12 = s22



留言

這個網誌中的熱門文章

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

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

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