我试图理解为什么qqline
不给我我所期望的,如果(且仅当)log="xy"
:
th <- 0.5
n <- 1e3
set.seed(271)
x <- rgamma(n, shape=th)
qF <- function(p) qgamma(p, shape=th)
## 1) Q-Q plot in normal space
plot(qF(ppoints(n)), sort(x))
qqline(y=sort(x), distribution=qF) # fine
## 2) Q-Q plot in log10-x space
plot(qF(ppoints(n)), sort(x), log="x")
qqline(y=sort(x), distribution=qF, untf=TRUE) # with untf=TRUE, fine
## 3) Q-Q plot in log10-y space
plot(qF(ppoints(n)), sort(x), log="y")
qqline(y=sort(x), distribution=qF, untf=TRUE) # with untf=TRUE, fine
现在?abline
地址untf
。据此,以下内容也应该起作用(在我看来):
## 4) Q-Q plot in log10-log10 space
plot(qF(ppoints(n)), sort(x), log="xy")
qqline(y=sort(x), distribution=qF, untf=TRUE) # ... but this is wrong
qqline(y=sort(x), distribution=qF, col="red") # ... (still) wrong
qqline(y=sort(log10(x)), distribution=function(p) log10(qF(p)), col="blue") # seems to be correct (but draws a line, not the curve resulting from transforming the Q-Q line in 'non-log-log' space)
我的问题是:1)为什么untf=TRUE
没有给出正确的答案,log="xy"
尽管它为log="x"
and做了log="y"
?2)在更一般的基础上,log-log-space 中“正确的”QQ 行(无论如何)是什么?
请注意,我已向R-help发布了类似的问题,但尚未收到答案。
更新
我做了更多的实验,再现(用脚)qqline()
在对数空间产生的奇怪曲线。我还认为,log10()
而不是log()
可能是错误行的原因。因此,如果想在对数空间中有一行,那么蓝线似乎确实是正确的。我不清楚为什么以对数刻度显示的标准 QQ 线的点没有给出正确的曲线,尽管 QQ 图的点也是这样构造的。
th <- 0.5
n <- 1e2 # note the difference for n=1e2 vs n=1e4!
set.seed(271)
x <- rgamma(n, shape=th)
qF <- function(p) qgamma(p, shape=th)
## Q-Q plot
qq.x <- qF(ppoints(n))
qq.y <- sort(x)
plot(qq.x, qq.y, log="xy", main=substitute("Q-Q plot with sample size n="*n., list(n.=n)),
xlab="Theoretical quantiles", ylab="Sample quantiles")
abline(v=quantile(qq.x, probs=0.25), col="gray50") # vertical line through 25%-quantile
abline(v=quantile(qq.x, probs=0.75), col="gray50") # vertical line through 75%-quantile
qqline(y=qq.y, distribution=qF, untf=TRUE) # ... this is wrong (*)
qqline(y=qq.y, distribution=qF, col="red") # ... still wrong
## => doesn't pass first and third quartile, but closer to 'right' line (blue) for large n
qqline(y=sort(log10(x)), distribution=function(p) log10(qF(p)), col="blue")
## => seems to be correct (but draws a line, not the curve resulting from transforming
## the Q-Q line in 'non-log-log' space)
## by foot (as will be seen, this reproduces (*))
probs <- c(0.25, 0.75)
q.x <- qF(probs)
q.y <- quantile(x, probs=probs, names=FALSE)
slope <- diff(q.y)/diff(q.x)
int <- q.y[1L] - slope * q.x[1L]
f <- function(x) slope * x + int # line in *normal* space
qq.y. <- f(qq.x) # points of the Q-Q line evaluated in normal space
lines(qq.x, qq.y., col="darkgreen")
## => reproduces qqline(.., untf=TRUE), see (*) above, that is,
## standard Q-Q line added to a plot in log-log scale = wrong (although points
## of the Q-Q plot are constructed this way!)
## legend
legend("topleft", lty=rep(1,5), col=c("darkgreen", "black", "red", "blue", "gray50"),
legend=c("Standard Q-Q line with 'untf=TRUE'",
"Standard Q-Q line plotted in log-log scale",
"Standard Q-Q line",
"Q-Q line of log10(x) vs log10-quantiles", "1st and 3rd quartiles"))