plot.lm():提取诊断Q-Q图中标记的数字 [英] plot.lm(): extracting numbers labelled in the diagnostic Q-Q plot

查看:83
本文介绍了plot.lm():提取诊断Q-Q图中标记的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于下面的简单示例,您可以看到在随后的图中确定了某些点.如何提取这些图中标识的行号,尤其是正态Q-Q图?

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)

我尝试使用str(mara)来查看是否可以在其中找到列表,但是在该处的Normal Q-Q图中看不到任何数字.有想法吗?

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?

推荐答案

为了重现性,我已经使用set.seed(2016)编辑了您的问题.要回答您的问题,我需要解释如何生成您看到的Q-Q图.

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)

现在,让我们比较一下我们自己生成的Q-Q图和plot.lm生成的Q-Q图:

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

一样,对吧?

现在,剩下的唯一问题是数字的标签方式.这些标记的点标记了最大的3个绝对标准化残差.考虑:

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

现在,如果您仔细查看该图,您会发现这三个数字正好显示.知道这一点后,您还可以签出例如id[1:5].

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

这篇关于plot.lm():提取诊断Q-Q图中标记的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆