【r<-基礎(chǔ)|統(tǒng)計(jì)】變量同質(zhì)性檢驗(yàn)

問題

你想要(精確)檢驗(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ù)

這里的例子使用了InsectSpraysToothGrowth 數(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 of chunk unnamed-chunk-2
plot(len ~ interaction(dose,supp), data=ToothGrowth)

plot of chunk unnamed-chunk-3

初一看好像數(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 時(shí)間雖然已經(jīng)很晚了,但我還是想在這兒簡(jiǎn)單的寫幾個(gè)字,來記錄一下我今天的生活。怎么說呢,這幾天當(dāng)了代理班主任,感覺時(shí)...
    鉞鼓閱讀 164評(píng)論 0 0
  • 最近接觸到了兩個(gè)概念:直接融資和間接融資。我并沒有認(rèn)真研究這兩個(gè)概念,我直覺的理解是:直接融資就是需要錢的企業(yè),找...
    Lcww閱讀 353評(píng)論 0 0
  • 007-83班戰(zhàn)友們大家好 這是我加入大家庭83班,第一次寫自己的文章,平時(shí)喜歡在書上看別人的經(jīng)典語錄,用敬仰的心...
    mynameis洪香蘭閱讀 263評(píng)論 1 2
  • 歲寒竹斜依 破土青云直 忽如一夜升 脫梭節(jié)節(jié)新
    絲路孤獨(dú)閱讀 663評(píng)論 0 0