8  方差齐性检验

8.1 两样本

8.1.1 F 检验

F 检验:比较两组的方差。数据必须呈正态分布。

步骤

  1. 检查数据是否呈正态分布(例如使用 Shapiro-Wilk 检验)。

  2. 进行 F 检验以比较两组的方差。

Code
df <- tibble(
    ctrl=c(2,6,13,5,8,9,12,11,8,10,12,14,13,6,7,4),
    trt=c(8,6,8,9,12,12,14,15,16,17,14,12,11,8,10,10)
)
df %>% 
    DT::datatable()
Code
var.test(df$ctrl,df$trt)
#> 
#>  F test to compare two variances
#> 
#> data:  df$ctrl and df$trt
#> F = 1.2553, num df = 15, denom df = 15, p-value = 0.6653
#> alternative hypothesis: true ratio of variances is not equal to 1
#> 95 percent confidence interval:
#>  0.4385898 3.5927405
#> sample estimates:
#> ratio of variances 
#>           1.255285

res <- var.test(len ~ supp, data = ToothGrowth)
res
#> 
#>  F test to compare two variances
#> 
#> data:  len by supp
#> F = 0.6386, num df = 29, denom df = 29, p-value = 0.2331
#> alternative hypothesis: true ratio of variances is not equal to 1
#> 95 percent confidence interval:
#>  0.3039488 1.3416857
#> sample estimates:
#> ratio of variances 
#>          0.6385951

p 值为 p = 0.2,大于显著性水平 0.05,可以认为两个方差之间没有显著差异。

8.2 多样本

8.2.1 Bartlett 检验

Bartlett 检验:比较两组或多组的方差。数据必须呈正态分布。

具有一个自变量的 Bartlett 检验

Code
res <- bartlett.test(weight ~ group, data = PlantGrowth)
res
#> 
#>  Bartlett test of homogeneity of variances
#> 
#> data:  weight by group
#> Bartlett's K-squared = 2.8786, df = 2, p-value = 0.2371

具有多个自变量的 Bartlett 检验:必须使用interaction() 函数将多个因子折叠成一个包含因子所有组合的变量

Code

with(ToothGrowth,interaction(supp,dose)) |> levels()
#> [1] "OJ.0.5" "VC.0.5" "OJ.1"   "VC.1"   "OJ.2"   "VC.2"



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

8.2.2 Levene’s 检验

Levene’s 检验:Bartlett 检验的可靠替代方案,对偏离正态不太敏感。

  • Levene 检验有三个版本:

    • 使用平均值(原始)

    • 使用中位数(Brown-Forsythe扩展

    • 10% trimmed mean(Brown-Forsythe扩展

Note

Levene 检验是文献中最常用的检验。

Code
library(car)
# Levene's test with one independent variable
leveneTest(weight ~ group, data = PlantGrowth,center=mean)
Df F value Pr(>F)
group 2 1.236963 0.3061949
27 NA NA
Code
leveneTest(weight ~ group, data = PlantGrowth,center=mean,trim=0.1)
Df F value Pr(>F)
group 2 1.277734 0.2949851
27 NA NA
Code
leveneTest(weight ~ group, data = PlantGrowth,center=median)
Df F value Pr(>F)
group 2 1.119186 0.3412266
27 NA NA
Code

rstatix::levene_test(PlantGrowth,weight ~ group,center = mean)
df1 df2 statistic p
2 27 1.236963 0.3061949
Code
# Levene's test with multiple independent variables
ToothGrowth$dose <- factor(ToothGrowth$dose)
leveneTest(len ~ supp*dose, data = ToothGrowth)
Df F value Pr(>F)
group 5 1.708578 0.1483606
54 NA NA
  • Brown-Forsythe 检验 作为 Levene 检验的扩展,特别适用于处理非正态数据。
Code
leveneTest(weight ~ group, data = PlantGrowth,center=median)
Df F value Pr(>F)
group 2 1.119186 0.3412266
27 NA NA
Code
HH::hov(weight ~ group, data = PlantGrowth)
#> 
#>  hov: Brown-Forsyth
#> 
#> data:  weight
#> F = 1.1192, df:group = 2, df:Residuals = 27, p-value = 0.3412
#> alternative hypothesis: variances are not identical

8.2.3 Fligner-Killeen 检验

Fligner-Killeen 检验:一种非参数检验,对偏离正态非常稳健。

Code
fligner.test(weight ~ group, data = PlantGrowth)
#> 
#>  Fligner-Killeen test of homogeneity of variances
#> 
#> data:  weight by group
#> Fligner-Killeen:med chi-squared = 2.3499, df = 2, p-value = 0.3088