[R] What are the red line and cut line in lm's Residuals vs Fitted plot?

David Winsemius dwinsemius at comcast.net
Sun Sep 18 18:36:16 CEST 2016


> On Sep 18, 2016, at 7:25 AM, mviljamaa <mviljamaa at kapsi.fi> wrote:
> 
> What are the red line and cut line in lm's Residuals vs Fitted plot?
> 
> As seen in e.g.:
> 
> http://i.imgur.com/QvZ6oeT.png

R has a `plot.lm` function that is invoked by `plot(lm_object)` when `lm_object` has a class of "lm". It produces multiple diagnostic plots. The default is for display of 4 of the possible 6 plots.

The help page can be specifically accessed by:

?plot.lm

And run the first example:

lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
plot(lm.SR)


If you have further question about that particular plot, which would be the first one, you should first review the help page and then review the code seen with:

plot.lm
#Error: object 'plot.lm' not found
getAnywhere(plot.lm)  # the way to see code that is not exported:

See:

function (x, which = c(1L:3L, 5L), caption = list("Residuals vs Fitted", 
    "Normal Q-Q", "Scale-Location", "Cook's distance", "Residuals vs Leverage", 
    expression("Cook's dist vs Leverage  " * h[ii]/(1 - h[ii]))), 
    panel = if (add.smooth) panel.smooth else points, sub.caption = NULL, 
    main = "", ask = prod(par("mfcol")) < length(which) && dev.interactive(), 
    ..., id.n = 3, labels.id = names(residuals(x)), cex.id = 0.75, 
    qqline = TRUE, cook.levels = c(0.5, 1), add.smooth = getOption("add.smooth"), 
    label.pos = c(4, 2), cex.caption = 1, cex.oma.main = 1.25) 


Note: I checked the option setting with:

> getOption("add.smooth")
[1] TRUE

---snipped plotting set up logic -----

if (show[1L]) {
        ylim <- range(r, na.rm = TRUE)
        if (id.n > 0) 
            ylim <- extendrange(r = ylim, f = 0.08)
        dev.hold()
        plot(yh, r, xlab = l.fit, ylab = "Residuals", main = main, 
            ylim = ylim, type = "n", ...)
        panel(yh, r, ...)   # this is what is drawing the "red line"
        if (one.fig) 
            title(sub = sub.caption, ...)
        mtext(getCaption(1), 3, 0.25, cex = cex.caption)
        if (id.n > 0) {
            y.id <- r[show.r]
            y.id[y.id < 0] <- y.id[y.id < 0] - strheight(" ")/3
            text.id(yh[show.r], y.id, show.r)
        }
        abline(h = 0, lty = 3, col = "gray")  # this is drawing the dotted line
        dev.flush()


So the `panel` function is creating the "red line" and its help page is at:

?panel.smooth   #...

... since the `panel funciton was given that value.

-- 
David.

> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list