Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wondering how to number outliers when using qqnorm?

Tags:

r

In R, I'm wondering how to get qqnorm to number its outliers (I've circled these in red, below).

Here is an example. I do a linear regression:

lm1 <- lm (y ~ x)

I then plot the model:

plot(lm1)

This produces a very nice QQ plot. You can see that it numbers the outliers (I've circled this in red).

enter image description here

However, if I do my own qqnorm, the outliers are not numbered. How can I number these outliers, just like in the previous graph?

qqnorm(y)
qqline(y)

enter image description here

like image 630
Contango Avatar asked Dec 15 '22 13:12

Contango


2 Answers

Another very simple way is the following:

QQ_y=qqnorm(y)
identify(QQ_y) 

The code will pause here. Hover over your plot, click on the assumed outliers or other points of interest, then control click or escape to continue code.

qqline(y)
like image 174
user4215619 Avatar answered Dec 28 '22 19:12

user4215619


Learn to look at the code:

 plot.lm
 # snipping the rather long output top and bottom and showing hte relevant section
 if (show[2L]) {
    ylim <- range(rs, na.rm = TRUE)
    ylim[2L] <- ylim[2L] + diff(ylim) * 0.075
    dev.hold()
    qq <- qqnorm(rs, main = main, ylab = ylab23, ylim = ylim, 
        ...)
    if (qqline) 
        qqline(rs, lty = 3, col = "gray50")
    if (one.fig) 
        title(sub = sub.caption, ...)
    mtext(getCaption(2), 3, 0.25, cex = cex.caption)
    if (id.n > 0) 
        text.id(qq$x[show.rs], qq$y[show.rs], show.rs)
    dev.flush()
like image 33
IRTFM Avatar answered Dec 28 '22 19:12

IRTFM