正态性和方差齐性是经典的统计模型应用的前提条件:t检验,方差分析以及线性回归等都需要满足这两个条件
所有正态统计的无效假设都是:数据服从正态分布。即P<0.05的时候代表不服从正态分布
目前常用的用于判断正态的统计方法有:Shapiro-Wilk(SW);Kolmogorov-Smirnov(KS); Cramer-von Mises(CVM); Anderson-Darling(AD)检验四种。根据他们的思想,可以分为两类
SW检验是基于峰度和偏度来进行分析的方式。峰度是来讲分布是尖峰还是平坦。偏度则是看分布是否对称。其中正态分布的时候,峰度和偏度均为0。如果峰度>0则为尖峰。如果峰度<0则为平坦峰。如果偏度>0提示右偏,如果偏度<0提示左偏 我们可是使用shapiro.test来进行SW检验set.seed(2019)
shapiro.test(runif(100, min = 2, max = 4))
##
## Shapiro-Wilk normality test
##
## data: runif(100, min = 2, max = 4)
## W = 0.94061, p-value = 0.0002101
数据不符合正态分布。
三个检验的基本思想是:计算理论正态分布和数据得到的分布之间的差异。 - 我们可以使用nortest::lillie.test;nortest::ad.test以及nortest::cvm.test来分别执行KS;AD;CVM检验
example <- runif(100, min = 2, max = 4)
library(nortest)
lillie.test(example)
##
## Lilliefors (Kolmogorov-Smirnov) normality test
##
## data: example
## D = 0.098768, p-value = 0.01761
ad.test(example)
##
## Anderson-Darling normality test
##
## data: example
## A = 1.131, p-value = 0.005579
cvm.test(example)
##
## Cramer-von Mises normality test
##
## data: example
## W = 0.17431, p-value = 0.01118
PS:由于不同统计方法可能结果不同。在选择的时候,如果是2000以内的数据;优先使用SW检验。如果是2000以上的数据,则考虑使用AD检验的效率最高。
在做正态性检验的时候,有时候统计学检验判断的正态性不一定准确,尤其是大数据的时候,在统计检验的角度来看,你的数据是永远不服从正态分布的。因此,不少人建议使用图形等描述性方法来进行判断。常用的方法有
qqnorm(example)
qqline(example, col = "blue")
library(StatDA)
## Loading required package: geoR
## --------------------------------------------------------------
## Analysis of Geostatistical Data
## For an Introduction to geoR go to http://www.leg.ufpr.br/geoR
## geoR version 1.7-5.2.1 (built on 2016-05-02) is now loaded
## --------------------------------------------------------------
## Loading required package: sgeostat
ppplot.das(example)
Bartlett.test来进行Barlett检验。使用leveneTest进行Levene检验require(graphics)
plot(count ~ spray, data = InsectSprays)
bartlett.test(InsectSprays$count, InsectSprays$spray)
##
## Bartlett test of homogeneity of variances
##
## data: InsectSprays$count and InsectSprays$spray
## Bartlett's K-squared = 25.96, df = 5, p-value = 9.085e-05
library(car)
## Loading required package: carData
leveneTest(InsectSprays$count, InsectSprays$spray)
## 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