Skip to content

Instantly share code, notes, and snippets.

@atyre2
Last active July 7, 2022 12:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atyre2/ff4e1ec24e42adda8dbd43cda99d6282 to your computer and use it in GitHub Desktop.
Save atyre2/ff4e1ec24e42adda8dbd43cda99d6282 to your computer and use it in GitHub Desktop.
ggplot version of plot(lmObject)
## function to do graphical checks in one hit, given a model
## assumes that broom and gridExtra are available
## and that the model object has tidiers available
check_assumptions <- function(x, ...){
if (inherits(x,"gam")){
stop("check_assumptions doesn't work with gam() objects")
}
my_theme <- ggplot2::theme_classic() +
ggplot2::theme(text=ggplot2::element_text(size=rel(4)))
# get the residuals etc.
rr <- broom::augment(x)
## plot 0 resid vs. fitted
rr0 <- ggplot2::ggplot(rr, ggplot2::aes(x = .fitted, y = .resid)) +
ggplot2::geom_point() +
ggplot2::geom_smooth() +
ggplot2::geom_hline(yintercept = 0, linetype=2) +
my_theme
## plot 1 qq plot
# get int and slope for qqline
probs <- c(0.25,0.75)
y <- quantile(rr$.std.resid, probs, names = FALSE, na.rm = TRUE)
x <- qnorm(probs)
slope <- diff(y)/diff(x)
int <- y[1L] - slope * x[1L]
rr1 <- ggplot2::ggplot(rr, ggplot2::aes(sample=.std.resid)) +
ggplot2::geom_qq(size=rel(4)) +
ggplot2::geom_abline(intercept = int, slope = slope, linetype = 2, size = 2) +
my_theme
## plot 2 scale location plot
rr2 <- ggplot2::ggplot(rr, ggplot2::aes(x = .fitted, y = sqrt(abs(.std.resid)))) +
ggplot2::geom_point() +
ggplot2::geom_smooth() +
ggplot2::geom_hline(yintercept = 1) +
my_theme
## plot 3 cooks distance plot
rr3 <- ggplot2::ggplot(rr, ggplot2::aes(.hat, .std.resid)) +
ggplot2::geom_vline(size = 2, xintercept = 0) +
ggplot2::geom_hline(size = 2, yintercept = 0) +
ggplot2::geom_point(ggplot2::aes(size = .cooksd)) +
ggplot2::geom_smooth(se = FALSE) +
my_theme
plot(gridExtra::arrangeGrob(rr0, rr1, rr2, rr3, nrow=2))
}
@ptoche
Copy link

ptoche commented May 9, 2018

Found a link to this somewhere. Seemed to work. You have an aes missing a ggplot2 prefix. Here it is in slightly edited form:

plot_residuals <- function(model, size = 1, linewidth = 1){  
    # extract residuals
    require(broom)
    d <- broom::augment(model) 

    # 1: residuals vs. fitted
    require(ggplot2)
    ggplot(data = d, aes(x = .fitted, y = .resid)) + 
        geom_point(size = size) + 
        geom_smooth() + 
        geom_hline(yintercept = 0, linetype = 2) -> p1

    ## 2: qq-plot
    probs <- c(0.25, 0.75)
    y <- quantile(d$.std.resid, probs, names = FALSE, na.rm = TRUE)
    x <- qnorm(probs)
    slope <- diff(y)/diff(x)
    intercept <- y[1L] - slope * x[1L]
    ggplot(data = d, aes(sample = .std.resid)) + 
        geom_qq(size = size) + 
        geom_abline(intercept = intercept, slope = slope, 
                    linetype = 2, size = linewidth) -> p2

    ## 3: scale location plot
    ggplot(data = d, aes(x = .fitted, y = sqrt(abs(.std.resid)))) + 
        geom_point(size = size) + 
        geom_smooth() + 
        geom_hline(yintercept = 1) -> p3 

    ## 4: Cooks distance plot
    ggplot(data = d, aes(.hat, .std.resid)) +
        geom_vline(size = linewidth, xintercept = 0) +
        geom_hline(size = linewidth, yintercept = 0) +
        geom_point(aes(size = .cooksd)) + 
        geom_smooth(se = FALSE) -> p4 
    require(gridExtra)
    return(plot(arrangeGrob(p1, p2, p3, p4, nrow = 2))) 
}

plot_residuals(m)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment