5  方差齐性检验

Modified

November 20, 2024

5.1 两样本

5.1.1 F 检验

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

步骤

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

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

Show the 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()
Show the 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,可以认为两个方差之间没有显著差异。

5.2 多样本

5.2.1 Bartlett 检验

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

具有一个自变量的 Bartlett 检验

Show the 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() 函数将多个因子折叠成一个包含因子所有组合的变量

Show the 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

5.2.2 Levene’s 检验

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

  • Levene 检验有三个版本:

    • 使用平均值(原始)

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

    • 10% trimmed mean(Brown-Forsythe扩展

Note

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

Show the code
library(car)
# Levene's test with one independent variable
leveneTest(weight ~ group, data = PlantGrowth,center=mean)
#> Levene's Test for Homogeneity of Variance (center = mean)
#>       Df F value Pr(>F)
#> group  2   1.237 0.3062
#>       27
leveneTest(weight ~ group, data = PlantGrowth,center=mean,trim=0.1)
#> Levene's Test for Homogeneity of Variance (center = mean: 0.1)
#>       Df F value Pr(>F)
#> group  2  1.2777  0.295
#>       27
leveneTest(weight ~ group, data = PlantGrowth,center=median)
#> Levene's Test for Homogeneity of Variance (center = median)
#>       Df F value Pr(>F)
#> group  2  1.1192 0.3412
#>       27

rstatix::levene_test(PlantGrowth,weight ~ group,center = mean)
#> # A tibble: 1 × 4
#>     df1   df2 statistic     p
#>   <int> <int>     <dbl> <dbl>
#> 1     2    27      1.24 0.306
Show the code
# Levene's test with multiple independent variables
ToothGrowth$dose <- factor(ToothGrowth$dose)
leveneTest(len ~ supp*dose, data = ToothGrowth)
#> Levene's Test for Homogeneity of Variance (center = median)
#>       Df F value Pr(>F)
#> group  5  1.7086 0.1484
#>       54
  • Brown-Forsythe 检验 作为 Levene 检验的扩展,特别适用于处理非正态数据。
Show the code
leveneTest(weight ~ group, data = PlantGrowth,center=median)
#> Levene's Test for Homogeneity of Variance (center = median)
#>       Df F value Pr(>F)
#> group  2  1.1192 0.3412
#>       27
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

5.2.3 Fligner-Killeen 检验

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

Show the 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