【r<-基础|统计】变量同质性检验

作者: 王诗翔 | 来源:发表于2018-03-13 10:53 被阅读52次

问题

你想要(精确)检验样本的方差同质性(同方差,方差齐性)。许多统计检验假设总体同方差

方案

有许多检验方差同质性的方式,下面列出三种:

  • Bartlett’s test - 如果数据服从正态分布,这是最好地检验方法。该方法对非正态数据非常敏感,如果数据不是正态的很可能返回假阳性的结果。
  • Levene’s test - 数据偏离正态性时比Bartlett检验更稳定(鲁棒性更好),内置于car
  • Fligner-Killeen test - 这是一个非参数检验,数据偏离正态是非常稳定适用。

对于所有的检验,零假设为总体方差相同(同质;不是相等的意思);备择假设是至少两组样本(总体方差)不同。

样例数据

这里的例子使用了InsectSpraysToothGrowth 数据集。 InsectSprays 数据集有一个独立变量,而 ToothGrowth 数据集有两个独立变量。

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

快速绘制数据集的箱线图:

plot(count ~ spray, data = InsectSprays)

plot of chunk unnamed-chunk-2
plot(len ~ interaction(dose,supp), data=ToothGrowth)

plot of chunk unnamed-chunk-3

初一看好像数据集的方差都不同质,但这需要像下面一样进行合适的检验。

Bartlett’s test

有一个独立变量:

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)

有多个独立变量,必须使用interaction()函数将这些独立变量包裹为含所有因子组合的单个变量。如果不适应,那么会得到错误的自由度,因而p值也将是错误的。

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 函数是car 包的一部分。

有一个独立变量:

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

有两个独立变量。注意这里 interaction函数不需要,因为该函数用于其他两个检验。

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

有一个独立变量:

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)

当处理多个独立变量时,这个fligner.test 函数有跟bartlett.test相同的行为。必须使用 interaction() 函数。

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

相关文章

  • 【r<-基础|统计】变量同质性检验

    问题 你想要(精确)检验样本的方差同质性(同方差,方差齐性)。许多统计检验假设总体同方差。 方案 有许多检验方差同...

  • 【r<-基础|统计】t检验

    问题 你想要检验来自两个总体的样本是否有不同的均值(显著性差异),或者检验从一个总体抽取的样本均值和理论均值有显著...

  • 【r<-基础|统计】频数检验

    问题 你有分类数据然后想要检验是否这些数据值的频数分布是否与预期不符,或者是否组间的频数分布有(显著)差异。 方案...

  • R统计分析_变量同质性检验

    英文:http://www.cookbook-r.com/Statistical_analysis/Homogen...

  • pyspark卡方检验用于特征选择

    卡方检验特征选择原理 计算特征变量与因变量的卡方独立性检验统计量,如果特征变量与因变量独立,说明其对预测因变量效果...

  • 拟合优度检验

    拟合优度检验是对一个分类变量的检验 拟合优度检验是用卡方统计量进行统计显著性检验的重要内容之一。它是依据总体分布状...

  • 假设检验——统计学任务3打卡

    假设检验是推论统计中用于检验统计假设的一种方法。而“统计假设”是可通过观察一组随机变量的模型进行检验的科学假说。一...

  • 统计学学习方法推荐

    学习目标: 统计学基础知识 统计(假设)检验相关 用R语言进行统计学相关分析计算 统计学基础知识: 统计学入门路线...

  • 【r<-基础|统计】ANOVA

    问题 你想要使用ANOVA比较多组之间的差异。 方案 假设这是你的数据: 单因素ANOVA分析 双因素ANOVA分...

  • 15-假设检验之F检验

    F检验(又称为方差齐性检验)主要对于方差齐性或方差同质性进行检验。 独立样本T检验前需要进行方差齐性检验,F检验的...

网友评论

    本文标题:【r<-基础|统计】变量同质性检验

    本文链接:https://www.haomeiwen.com/subject/qkaafftx.html