R语言学习笔记——QQ图画法解析


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) 生成一列分位点,即数理统计中提到的正态检验图的方法
i − 3 8 n + 1 4 \frac{i-\frac{3}{8}}{n+\frac{1}{4}} n+41i83(当n<=10) 或者 i − 1 2 n \frac{i-\frac{1}{2}}{n} ni21(当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, ...)
}

你可能感兴趣的:(R语言学习笔记——QQ图画法解析)