qqplot
0.背景知識(shí)
要看懂qq圖,先要知道什么是分位數(shù)脱盲。
我們生成一組數(shù)字邑滨,作為示例數(shù)據(jù)
set.seed(6666)
dat = sample(1:1000,15);head(dat)
## [1] 611 462 612 520 289 338
把這些數(shù)據(jù)畫在圖上
library(ggplot2)
p = ggplot(dat = as.data.frame(dat),aes(x = factor(1),y = dat))+
geom_point(aes(color = factor(dat),fill = factor(dat)),
size = 3.5,shape = 21,alpha = 0.5)+
labs(x = "")+
theme_bw()+
theme(legend.position = "none")
p
橫坐標(biāo)沒有意義,縱坐標(biāo)代表dat的數(shù)值钱反。
四分位數(shù)驼修,即可以將這組數(shù)據(jù)分成等量的四個(gè)部分的三個(gè)數(shù)殿遂。
quantile(dat)
## 0% 25% 50% 75% 100%
## 111 333 520 701 993
p + geom_hline(yintercept = quantile(dat)[2:4],linetype = 4)
三條線分別代表25% 50% 75% 的四分位數(shù),與箱線圖異曲同工乙各。
四分位數(shù)是最常見的墨礁,在此基礎(chǔ)上可以擴(kuò)展到百分位數(shù),例如:
qk = quantile(dat,probs = seq(0, 1, length.out = 15));qk
## 0% 7.142857% 14.28571% 21.42857% 28.57143% 35.71429% 42.85714% 50%
## 111 285 289 328 338 350 462 520
## 57.14286% 64.28571% 71.42857% 78.57143% 85.71429% 92.85714% 100%
## 558 611 612 790 847 926 993
p + geom_hline(yintercept = qk,linetype = 2)
這是15個(gè)數(shù)字耳峦,所以分成了15份恩静,是為了方便計(jì)算和理解。事實(shí)上quantile函數(shù)提供了多種方法計(jì)算分位數(shù)蹲坷,可作為了解驶乾。
1.準(zhǔn)備數(shù)據(jù)
生成符合三種分布的數(shù)據(jù),作為檢驗(yàn)示例數(shù)據(jù)屬于那種分布的參考循签。任何一個(gè)分布都可以级乐。
library(patchwork)
df = data.frame(x = 1:100,
normal = dnorm(1:100,50,15),
uniform = dunif(1:100,1,100),
exponential = dexp(1:100,0.06))
set.seed(1004)
normal = rnorm(100,50,15)
set.seed(1004)
uniform = 1:100
set.seed(1004)
exponential = rexp(100,0.06)
rn = data.frame(x = 1:100,
normal = normal,
uniform = uniform ,
exponential = exponential )
head(df)
## x normal uniform exponential
## 1 1 0.0001281199 0.01010101 0.05650587
## 2 2 0.0001589392 0.01010101 0.05321523
## 3 3 0.0001962978 0.01010101 0.05011621
## 4 4 0.0002413624 0.01010101 0.04719767
## 5 5 0.0002954566 0.01010101 0.04444909
## 6 6 0.0003600704 0.01010101 0.04186058
head(rn)
## x normal uniform exponential
## 1 1 40.88239 1 12.995556
## 2 2 61.51444 2 28.127434
## 3 3 47.53592 3 4.341248
## 4 4 49.56835 4 0.180288
## 5 5 50.20337 5 14.291344
## 6 6 39.26425 6 36.579238
兩個(gè)數(shù)據(jù)框,一個(gè)是符合某分布的某個(gè)數(shù)值大小的概率县匠,一個(gè)是符合某分布的具體數(shù)值风科,兩個(gè)數(shù)據(jù)框的二三四列分別是正態(tài)分布、均勻分布和指數(shù)分布乞旦。
理解qq圖可以幫助我們探索數(shù)據(jù)屬于哪種分布
2.三種分布的密度圖
#1.正態(tài)分布
p1 = ggplot(df,aes(x = x,y = normal ))+
geom_line()+theme_classic()
#2.均勻分布
p2 = ggplot(df,aes(x = x,y = uniform ))+
geom_line()+theme_classic()
#3.指數(shù)分布
p3 = ggplot(df,aes(x = x,y = exponential ))+
geom_line()+theme_classic()
p1+p2+p3
3.畫出15個(gè)分位數(shù)
qn = apply(rn,2,function(x){
quantile(x,probs = seq(0, 1, length.out = 15))
})
qn[,1] = 1:nrow(qn)
qn = data.frame(qn)
qp = function(p,nc){
a = p
n = c()
for(i in 1:nrow(qn)){
n[[i]] = qn[i,nc]
a = a + geom_vline(xintercept = n[[i]],color = "red",size = 0.3,alpha = 0.3)
}
return(a)
}
qp(p1,2)+qp(p2,3)+qp(p3,4)
4.繪制qq圖
15個(gè)數(shù)據(jù)實(shí)在太少贼穆,比較不同分布時(shí)不夠明顯,所以我擴(kuò)展到100個(gè)兰粉。
dat = sample(1:1000,100)
qn$nk = quantile(dat,probs = seq(0, 1, length.out = 15))
qq = function(k){
ggplot(data = qn,aes_string(x = colnames(qn)[k],y = "nk"))+
geom_point()+
geom_smooth(method = "lm",se = F)+
theme_bw()
}
qq(2)+qq(3)+qq(4)
上面畫參考線的方法直接按照線性擬合來畫了故痊。qqline函數(shù)是將上下四分位數(shù)連線作為參考線的。
哪種分布作為橫坐標(biāo)畫出的qq圖更接近一條直線玖姑,數(shù)據(jù)就更接近哪種分布
5.函數(shù)繪制正態(tài)qq圖
基礎(chǔ)包函數(shù)qqnorm和qqline愕秫,橫坐標(biāo)是標(biāo)準(zhǔn)正態(tài)分布的分位數(shù),縱坐標(biāo)是輸入數(shù)據(jù)的分位數(shù)焰络,也就是檢驗(yàn)數(shù)據(jù)是否符合正態(tài)分布戴甩。
qqnorm(qn$nk)
qqline(qn$nk, col=2)
也可使用R包c(diǎn)ar來繪制
library(car)
qqPlot(qn$nk)
## [1] 1 15