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.255285res<-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
# Levene's test with multiple independent variablesToothGrowth$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