R語言學習筆記——QQ圖畫法解析
阿新 • • 發佈:2018-12-14
qqnorm()與qqline()
qqnorm()
- qqnorm()函式原始碼
function (y, ylim, main = "Normal Q-Q Plot", xlab = "Theoretical Quantiles", ylab = "Sample Quantiles", plot.it = TRUE, datax = FALSE, ...) { if (has.na <- any(ina <- is.na(y))) { yN <- y y <- y[!ina] } if (0 == (n <- length(y))) stop("y is empty or has only NAs") if (plot.it && missing(ylim)) ylim <- range(y) x <- qnorm(ppoints(n))[order(order(y))] if (has.na) { y <- x x <- yN x[!ina] <- y y <- yN } if (plot.it) if (datax) plot(y, x, main = main, xlab = ylab, ylab = xlab, xlim = ylim, ...) else plot(x, y, main = main, xlab = xlab, ylab = ylab, ylim = ylim, ...) invisible(if (datax) list(x = y, y = x) else list(x = x, y = y)) }
- ppoints()函式解釋
#ppoints(n, a = if(n <= 10) 3/8 else 1/2)
> ppoints(6)
[1] 0.10 0.26 0.42 0.58 0.74 0.90
ppoints(n) 通過(1:m - a)/(m + (1-a)-a) 生成一列分位點,即數理統計中提到的正態檢驗圖的方法 (當n<=10) 或者(當n>10)
qqline()
- qqline()原始碼
function (y, datax = FALSE, distribution = qnorm, probs = c(0.25, 0.75), qtype = 7, ...) { stopifnot(length(probs) == 2, is.function(distribution)) y <- quantile(y, probs, names = FALSE, type = qtype, na.rm = TRUE) x <- distribution(probs) if (datax) { slope <- diff(x)/diff(y) int <- x[1L] - slope * y[1L] } else { slope <- diff(y)/diff(x) int <- y[1L] - slope * x[1L] } abline(int, slope, ...) }