正态性检验之统计推断

正态性检验

x2yline
2017年10月18日

正态性检验最好不使用直接计算法检验偏度和峰度

** SPSS默认的两个方法为Shapiro – Wilk(W 检验)和Kolmogorov – Smirnov(D 检验)**

参考文章:
https://www.yaolibio.com/2017/01/03/normality-test/
http://blog.sina.com.cn/s/blog_5efe47110100d28m.html
http://blog.sina.com.cn/s/blog_403aa80a01019ly5.html
但是其中所说的正态检验水准一般设置为0.10

当样本含量n ≤2000时,结果以Shapiro – Wilk(W 检验)为准

# faithful是R里的一个内置数据集
shapiro.test(faithful$eruptions)
## 
##  Shapiro-Wilk normality test
## 
## data:  faithful$eruptions
## W = 0.84592, p-value = 9.036e-16

当样本含量n >2000 时,结果以Kolmogorov – Smirnov(D 检验)为准

ks.test(faithful$eruptions, "pnorm")
## Warning in ks.test(faithful$eruptions, "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
## 
##  One-sample Kolmogorov-Smirnov test
## 
## data:  faithful$eruptions
## D = 0.94857, p-value < 2.2e-16
## alternative hypothesis: two-sided
Warning message:
In ks.test(faithful$eruptions, "pnorm") :
  ties should not be present for the Kolmogorov-Smirnov test

出现警告,Kolmogorov - Smirnov检验里不应该有相等的值,可以忽略或加上参数exact=FALSE,默认使用近似算法计算近似p值。

直接计算法检验(纯练习用,可忽略)

1. 用R语言计算faithful数据框中的eruptions的3阶中心距(center moment)

e1071::moment(faithful$eruptions,
    order=3, center=TRUE)
## [1] -0.6149059

2. 用R语言计算偏度,偏度计算公式为


μ2 和 μ3 为二阶和三阶中心距,用moment计算中心距时,其除数为n而不是n-1,这里需要注意一下
R语言验证:

u3 <- e1071::moment(faithful$eruptions, order=3, center=TRUE)
u2 <- e1071::moment(faithful$eruptions, order=2, center=TRUE)
u3 <- mean((faithful$eruptions - mean(faithful$eruptions))^3)
u3/(u2)^(3/2)
## [1] -0.415841
u3/(u2*nrow(faithful)/(nrow(faithful)-1))^1.5
## [1] -0.4135498
e1071::skewness(faithful$eruptions)
## [1] -0.4135498
agricolae::skewness(faithful$eruptions)
## [1] -0.4181505
moments::skewness(faithful$eruptions)
## [1] -0.415841
propagate::skewness(faithful$eruptions)
## [1] -0.415841
skew <- agricolae::skewness(faithful$eruptions)

propagate、moments、e1071包和agricolae的包计算结果的差异是由于e1071算的三阶中心距是有偏的估计,而propagate和moments的三阶和二阶距估计都是有偏的,所以agricolae的结果更为准确

偏度小于0说明均值小于中位数,呈左偏分布(负偏分布)
PS: 教材的第39页公式似乎更加精确

3. 用R语言计算峰度

u4 <- e1071::moment(faithful$eruptions, order=4, center=TRUE)
u2 <- e1071::moment(faithful$eruptions, order=2, center=TRUE)
u4/u2^2 -3
## [1] -1.5006
u4/var(faithful$eruptions)^2 - 3
## [1] -1.511605
e1071::kurtosis(faithful$eruptions)
## [1] -1.511605
agricolae::kurtosis(faithful$eruptions)
## [1] -1.506167
moments::kurtosis(faithful$eruptions)
## [1] 1.4994
moments::kurtosis(faithful$eruptions)-3
## [1] -1.5006
propagate::kurtosis(faithful$eruptions)
## [1] -1.5006
kurt <- agricolae::kurtosis(faithful$eruptions)

propagate、moments、e1071包和agricolae的包计算结果的差异是由于e1071算的四中心距是有偏的估计,而propagate和moments的都是有偏估计,所以agricolae的结果更为准确

峰度等于0说明标准化后的分布与正态分布的峰同样尖锐,大于0说明更加尖锐,尾部更粗;小于0说明更加平坦,尾部更细。
PS: 教材的第39页公式似乎更加精确

4. 偏度和峰度的假设检验

H0:skew = 0, krut = 0,即总体服从正态分布
H1:skew != 0或(和)krut!= 0,即总体不服从正态分布
计算出峰度和偏度以后,还能计算出峰度和偏度的标准误

  • 峰度和偏度标准误
ses <- sqrt(6 / length(faithful$eruptions))
sek <- sqrt(24/length(faithful$eruptions))
  • 检验统计量
totests <-  skew/ses
totestk <- kurt/sek
  • 计算p值
pvals <- pt(totests, (length(faithful$eruptions) - 1))
pvalk <- pt(totestk, (length(faithful$eruptions) - 1))
pvals
## [1] 0.002614556
pvalk
## [1] 3.68164e-07

所以按0.01水平,拒绝H0,接受H1,这些样本不服从正态分布

其他正态性检验R包nortest和normtest

R自带的stats包里有Kolmogorov-Smirnov检验和Shapiro-Wilk检验,同时nortest包里的Lilliefor检验、Anderson-Darling检验···和tseries包中的Jarque-Bera检验也能进行检验

# Anderson-Darling test for normality
nortest::ad.test(faithful$eruptions)
## 
##  Anderson-Darling normality test
## 
## data:  faithful$eruptions
## A = 17.305, p-value < 2.2e-16
# Lilliefors (Kolmogorov-Smirnov) test for normality
# Although the test statistic obtained from lillie.test(x) is the same as that obtained from
# ks.test(x, "pnorm", mean(x), sd(x)), it is not correct to use the p-value from the latter for
# the composite hypothesis of normality (mean and variance unknown), since the distribution of the
# test statistic is different when the parameters are estimated.
nortest::lillie.test(faithful$eruptions)
## 
##  Lilliefors (Kolmogorov-Smirnov) normality test
## 
## data:  faithful$eruptions
## D = 0.18135, p-value < 2.2e-16
# jarque.bera.test: This test is a joint statistic using skewness and kurtosis coefficients.
tseries::jarque.bera.test(faithful$eruptions)
## 
##  Jarque Bera Test
## 
## data:  faithful$eruptions
## X-squared = 33.36, df = 2, p-value = 5.702e-08
normtest::skewness.norm.test(faithful$eruptions)
## 
##  Skewness test for normality
## 
## data:  faithful$eruptions
## T = -0.41584, p-value = 0.006
normtest::kurtosis.norm.test(faithful$eruptions)
## 
##  Kurtosis test for normality
## 
## data:  faithful$eruptions
## T = 1.4994, p-value = 0.001

https://cran.r-project.org/web/packages/nortest/nortest.pdf
https://cran.r-project.org/web/packages/normtest/normtest.pdf
https://stats.stackexchange.com/questions/3136/how-to-perform-a-test-using-r-to-see-if-data-follows-normal-distribution
https://cran.r-project.org/web/packages/tseries/tseries.pdf

参考:
http://www.r-tutor.com/elementary-statistics/numerical-measures/variance
http://www.r-tutor.com/elementary-statistics/numerical-measures/moment
https://stat.ethz.ch/pipermail/r-help/1999-July/004529.html
http://www.r-tutor.com/elementary-statistics/numerical-measures/skewness
http://www.r-tutor.com/elementary-statistics/numerical-measures/kurtosis
https://artax.karlin.mff.cuni.cz/r-help/library/agricolae/html/skewness.html
https://artax.karlin.mff.cuni.cz/r-help/library/agricolae/html/kurtosis.html
https://www.researchgate.net/post/Could_anyone_tell_me_how_to_calculate_skewness_and_kurtosis_of_a_numeric_variable_in_R

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容