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

这一生的挚爱 提交于 2019-12-04 23:32:38

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

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].

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!