問題
你想要(精確)檢驗(yàn)樣本的方差同質(zhì)性(同方差,方差齊性)。許多統(tǒng)計(jì)檢驗(yàn)假設(shè)總體同方差。
方案
有許多檢驗(yàn)方差同質(zhì)性的方式,下面列出三種:
- Bartlett’s test - 如果數(shù)據(jù)服從正態(tài)分布,這是最好地檢驗(yàn)方法。該方法對(duì)非正態(tài)數(shù)據(jù)非常敏感,如果數(shù)據(jù)不是正態(tài)的很可能返回假陽性的結(jié)果。
-
Levene’s test - 數(shù)據(jù)偏離正態(tài)性時(shí)比Bartlett檢驗(yàn)更穩(wěn)定(魯棒性更好),內(nèi)置于
car
包 - Fligner-Killeen test - 這是一個(gè)非參數(shù)檢驗(yàn),數(shù)據(jù)偏離正態(tài)是非常穩(wěn)定適用。
對(duì)于所有的檢驗(yàn),零假設(shè)為總體方差相同(同質(zhì);不是相等的意思);備擇假設(shè)是至少兩組樣本(總體方差)不同。
樣例數(shù)據(jù)
這里的例子使用了InsectSprays
和 ToothGrowth
數(shù)據(jù)集。 InsectSprays
數(shù)據(jù)集有一個(gè)獨(dú)立變量,而 ToothGrowth
數(shù)據(jù)集有兩個(gè)獨(dú)立變量。
head(InsectSprays)
#> count spray
#> 1 10 A
#> 2 7 A
#> 3 20 A
#> 4 14 A
#> 5 14 A
#> 6 12 A
tg <- ToothGrowth
tg$dose <- factor(tg$dose) # Treat this column as a factor, not numeric
head(tg)
#> len supp dose
#> 1 4.2 VC 0.5
#> 2 11.5 VC 0.5
#> 3 7.3 VC 0.5
#> 4 5.8 VC 0.5
#> 5 6.4 VC 0.5
#> 6 10.0 VC 0.5
快速繪制數(shù)據(jù)集的箱線圖:
plot(count ~ spray, data = InsectSprays)
plot(len ~ interaction(dose,supp), data=ToothGrowth)
初一看好像數(shù)據(jù)集的方差都不同質(zhì),但這需要像下面一樣進(jìn)行合適的檢驗(yàn)。
Bartlett’s test
有一個(gè)獨(dú)立變量:
bartlett.test(count ~ spray, data=InsectSprays)
#>
#> Bartlett test of homogeneity of variances
#>
#> data: count by spray
#> Bartlett's K-squared = 25.96, df = 5, p-value = 9.085e-05
# Same effect, but with two vectors, instead of two columns from a data frame
# bartlett.test(InsectSprays$count ~ InsectSprays$spray)
有多個(gè)獨(dú)立變量,必須使用interaction()
函數(shù)將這些獨(dú)立變量包裹為含所有因子組合的單個(gè)變量。如果不適應(yīng),那么會(huì)得到錯(cuò)誤的自由度,因而p值也將是錯(cuò)誤的。
bartlett.test(len ~ interaction(supp,dose), data=ToothGrowth)
#>
#> Bartlett test of homogeneity of variances
#>
#> data: len by interaction(supp, dose)
#> Bartlett's K-squared = 6.9273, df = 5, p-value = 0.2261
# The above gives the same result as testing len vs. dose alone, without supp
bartlett.test(len ~ dose, data=ToothGrowth)
#>
#> Bartlett test of homogeneity of variances
#>
#> data: len by dose
#> Bartlett's K-squared = 0.66547, df = 2, p-value = 0.717
Levene’s test
leveneTest
函數(shù)是car
包的一部分。
有一個(gè)獨(dú)立變量:
library(car)
leveneTest(count ~ spray, data=InsectSprays)
#> Levene's Test for Homogeneity of Variance (center = median)
#> Df F value Pr(>F)
#> group 5 3.8214 0.004223 **
#> 66
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
有兩個(gè)獨(dú)立變量。注意這里 interaction
函數(shù)不需要,因?yàn)樵摵瘮?shù)用于其他兩個(gè)檢驗(yàn)。
leveneTest(len ~ supp*dose, data=tg)
#> Levene's Test for Homogeneity of Variance (center = median)
#> Df F value Pr(>F)
#> group 5 1.7086 0.1484
#> 54
Fligner-Killeen test
有一個(gè)獨(dú)立變量:
fligner.test(count ~ spray, data=InsectSprays)
#>
#> Fligner-Killeen test of homogeneity of variances
#>
#> data: count by spray
#> Fligner-Killeen:med chi-squared = 14.483, df = 5, p-value = 0.01282
# Same effect, but with two vectors, instead of two columns from a data frame
# fligner.test(InsectSprays$count ~ InsectSprays$spray)
當(dāng)處理多個(gè)獨(dú)立變量時(shí),這個(gè)fligner.test
函數(shù)有跟bartlett.test
相同的行為。必須使用 interaction()
函數(shù)。
fligner.test(len ~ interaction(supp,dose), data=ToothGrowth)
#>
#> Fligner-Killeen test of homogeneity of variances
#>
#> data: len by interaction(supp, dose)
#> Fligner-Killeen:med chi-squared = 7.7488, df = 5, p-value = 0.1706
# The above gives the same result as testing len vs. dose alone, without supp
fligner.test(len ~ dose, data=ToothGrowth)
#>
#> Fligner-Killeen test of homogeneity of variances
#>
#> data: len by dose
#> Fligner-Killeen:med chi-squared = 1.3879, df = 2, p-value = 0.4996