Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot.lm(): extracting numbers labelled in the diagnostic Q-Q plot

For the simple example below, you can see that there are certain points that are identified in the ensuing plots. How can I extract the row numbers identified in these plots, especially the Normal Q-Q plot?

set.seed(2016)
maya <- data.frame(rnorm(100))
names(maya)[1] <- "a"
maya$b <- rnorm(100)
mara <- lm(b~a, data=maya)
plot(mara)

I tried using str(mara) to see if I could find a list there, but I can't see any of the numbers from the Normal Q-Q plot there. Thoughts?

like image 335
Reuben Mathew Avatar asked Jun 18 '16 13:06

Reuben Mathew


People also ask

How do you read a Q plot?

If the bottom end of the Q-Q plot deviates from the straight line but the upper end is not, then we can clearly say that the distribution has a longer tail to its left or simply it is left-skewed (or negatively skewed) but when we see the upper end of the Q-Q plot to deviate from the straight line and the lower and ...

What is Qqline in R programming?

qqline adds a line to a “theoretical”, by default normal, quantile-quantile plot which passes through the probs quantiles, by default the first and third quartiles. qqplot produces a QQ plot of two datasets. Graphical parameters may be given as arguments to qqnorm , qqplot and qqline .

What is a residual QQ plot?

Residual plots and Q-Q plots are used to visually check that your data meets the homoscedasticity and normality assumptions of linear regression. A residual plot lets you see if your data appears homoscedastic.


1 Answers

I have edited your question using set.seed(2016) for reproducibility. To answer your question, I need to explain how to produce the Q-Q plot you see.

se <- sqrt(sum(mara$residuals^2) / mara$df.residual)  ## Pearson residual standard error
hii <- lm.influence(mara, do.coef = FALSE)$hat  ## leverage
std.resi <- mara$residuals / (se * sqrt(1 - hii))  ## standardized residuals
## these three lines can be replaced by: std.resi <- rstandard(mara)

Now, let's compare the Q-Q plot we generate ourselves and that generated by plot.lm:

par(mfrow = c(1,2))
qqnorm(std.resi, main = "my Q-Q"); qqline(std.resi, lty = 2)
plot(mara, which = 2)  ## only display Q-Q plot

Q-Q

The same, right?

Now, the only issue left is how the numbers are labelled. Those labelled points mark the largest 3 absolute standardised residuals. Consider:

x <- sort(abs(std.resi), decreasing = TRUE)
id <- as.integer(names(x))
id[1:3]
# [1] 23  8 12

Now, if you look at the graph closely, you can see that those three numbers are exactly what is shown. Knowing this, you can also check out, for example, id[1:5].

like image 109
Zheyuan Li Avatar answered Sep 17 '22 18:09

Zheyuan Li