【问题标题】:plot.lm(): extracting numbers labelled in the diagnostic Q-Q plotplot.lm():提取诊断 QQ 图中标记的数字
【发布时间】:2016-10-20 05:15:05
【问题描述】:

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

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 QQ 图中的任何数字。想法?

【问题讨论】:

    标签: r plot regression linear-regression lm


    【解决方案1】:

    为了可重复性,我已使用 set.seed(2016) 编辑了您的问题。为了回答你的问题,我需要解释一下如何产生你看到的QQ图。

    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)
    

    现在,让我们比较一下我们自己生成的QQ图和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 个绝对标准化残差。考虑:

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

    现在,如果您仔细查看图表,您会发现这三个数字正是显示的内容。知道了这个,你也可以去看看,比如id[1:5]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      • 1970-01-01
      • 2018-04-09
      • 1970-01-01
      相关资源
      最近更新 更多