[R] qqplot

Peter Ehlers ehlers at ucalgary.ca
Mon Nov 2 18:21:45 CET 2009


Peter Flom wrote:
> David Winsemius <dwinsemius at comcast.net> wrote
>> I always assumed that the intercept was zero and the slope = unity.
>>
>>  y <- rt(200, df = 5)
>>  qqnorm(y); qqline(y, col = 2)
>>  qqplot(y, rt(300, df = 5))
>>  abline(0, 1, col="red")
>>
> 
> Suppose you have the following
> 
> x <- rnorm(500)
> y <- 500*(x + runif(500, 0,1))
> qqplot(x,y)
> 
> 
> Then an abline(0,1) will not be useful; it will be an almost horizontal line. But
> 
> m1 <- lm(y~x)
> abline(coef(m1))
> 
> 
> does the trick.
> 
> Peter

That's not what qqline() does and for good reason - it treats
x and y asymmetrically.

But qqline() is a very simple function, using the quartiles
as also suggested by John. Here's a modified version that
should work for Carol:

qqline2 <- function (x, y, ...)
{
     y <- quantile(y[!is.na(y)], c(0.25, 0.75))
     x <- quantile(x[!is.na(x)], c(0.25, 0.75))
     slope <- diff(y)/diff(x)
     int <- y[1L] - slope * x[1L]
     abline(int, slope, ...)
}

I've just replaced the line using Normal quantiles with
the quantiles of x (and omitted the datax option).

  -Peter Ehlers

> 
> 
> 
> Peter L. Flom, PhD
> Statistical Consultant
> Website: www DOT peterflomconsulting DOT com
> Writing; http://www.associatedcontent.com/user/582880/peter_flom.html
> Twitter:   @peterflom
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
>




More information about the R-help mailing list