Skip to contents

Four diagnostic plots, drawn with ggplot2, for any model that predictmeans handles: lm, aov (also with Error() strata), glm, gls, lme, lmer, glmer, glmmTMB, mgcv::gam, semireg fits, and other classes with fitted() and residuals() methods. With interactive = TRUE they are shown with plotly: hovering a point gives its row of the data.

Usage

residplot(model, group = "none", re_level = 1, slope = FALSE, id = 3,
    interactive = FALSE, plot = TRUE, dense = c("auto", "points", "bins"), ...)

Arguments

model

A fitted model (see Description).

group

Optional variable that colours the points: the name (in "quotes") of a variable in the data of the model, or a vector with one value per observation. The default "none" uses one colour. The plots have no legend; interactive = TRUE shows each point's row and group on hover, and the plots can be given one with + ggplot2::theme(legend.position = "right").

re_level

For mixed models (level before 2.0): which random-effect term the random-effect plot shows, 1, 2, ... (in the order of ranef(): for lme the outermost grouping first, for lmer the innermost first).

slope

For mixed models: TRUE plots the random slopes (the second column of the random effects) instead of the random intercepts.

id

Number of the most extreme points labelled with their row names in the data (rows left out for missing values keep their numbers, so a label is the row to look at; largest absolute residuals; for the Cook's distance plot the largest distances); FALSE or 0 labels none. In 1.x id = TRUE let you click on points; now TRUE labels 3 points, and interactive = TRUE shows every point's row name.

...

Not used: level, newwd, ask (before 2.0) stop with their 2.0 spelling.

interactive

TRUE: show the plots with plotly (plotly::ggplotly()), which must be installed.

plot

FALSE: do not draw; only return the plots.

dense

How overlapping points are drawn. "points" (also "auto", the default): every point; beyond 200 points as open circles, more transparent as their number grows, so dense regions show darker and no point is hidden. "bins": the scatter plots as shaded counts in a grid of bins (darker: more points; no colour bar), with the points that stand out (the id largest and those beyond 3 on the residual scale) drawn on top. The Q-Q plot of more than 2000 residuals draws every point in the tails (outside the central 95%) and an even selection of the others.

Value

Invisibly, a list of the four ggplot objects (class "pm_residplot"), with attribute "data": the data frame of the points (row label, fitted, residual, observed values, group). With interactive = TRUE, the plotly widget.

Details

The plots:

  1. Residuals against fitted values, with a loess smooth (with fewer than 7 distinct fitted values, the mean residual at each, joined): look for trends (a missing term, a wrong link) and for spread that changes with the fitted values.

  2. Normal Q-Q plot of the residuals: the points should lie near the line.

  3. Mixed models: normal Q-Q plot of the random effects of term re_level. gls: autocorrelation of the normalized residuals (with approximate 95% limits). Other models: square root of the absolute residuals against fitted values (scale-location), for spread that changes with the fitted values.

  4. lm, aov and glm: Cook's distances. Other models (also gam: Cook's distances of a penalized fit are only approximate): fitted against observed values of the response as modelled (from pm_data: the rows used in the fit; the axis is labelled with the response as written, e.g. log(yield + 1), or the observed proportion for cbind(successes, failures)); for a 0/1 response (also a two-level factor), the observed proportion against the mean fitted probability in tenths of the fitted values (calibration).

Residuals: standardized for lm (rstandard()), normalized for gls and lme (the correlation structure removed), scaled by the residual standard deviation for lmer. For Poisson, binomial and negative binomial models (glm, glmer, glmmTMB, gam) randomized quantile residuals (Dunn and Smyth, 1996): exactly normal when the model is right, so plots 1 and 2 are read as for a normal model (they use a fixed random seed, so the plots do not change between calls); fitted values are then on the link scale. Other families: standardized deviance residuals. aov with Error(): the residuals of the lowest stratum. The plots inform; they do not test. For a table of checks see pm_check; for Cook's distances of mixed models CookD.

References

Dunn, P. K. and Smyth, G. K. (1996), Randomized quantile residuals, Journal of Computational and Graphical Statistics, 5, 236-244.

See also

Author

Dongwen Luo, Siva Ganesh and John Koolaard

Examples

library(predictmeans)
Oats$nitro <- factor(Oats$nitro)
fm <- lme(yield ~ nitro*Variety, random=~1|Block/Variety, data=Oats)
residplot(fm, re_level = 2)    # lme: re_level = 2 for random effect "Block:Variety"


fm <- lmer(yield ~ nitro*Variety+(1|Block/Variety), data=Oats)
residplot(fm, group = "Variety") # lmer: re_level = 1 for random effect "Block:Variety"


# a Poisson GLM: randomized quantile residuals
gm <- glm(breaks ~ wool * tension, family = poisson, data = warpbreaks)
p <- residplot(gm, plot = FALSE)
p[[2]]                               # one of the plots

# interactive plots (hover for each point's row)
if (interactive() && requireNamespace("plotly", quietly = TRUE)) residplot(fm, interactive = TRUE)