Skip to contents

Generates a variety of diagnostic plots for a mixed-effects regression (lmer) model.

Usage

diag_lmer(model, data, facet_by = NULL, colour_by = NULL, order_by = "mean", order = "ascending", ncol = 4, scales = "fixed", save = FALSE, save_dir = getwd(), ...)

Arguments

model

A fitted linear mixed-effects lmer model of class lmerMod.

data

A data frame or tibble containing the model calibration dataset.

facet_by

A factor by which to facet wrap the plots (e.g. site). Default = NULL.

colour_by

A factor by which to colour-code data points (e.g. season). Default = NULL.

order_by

Method specifying how to order the levels of the faceting factor. Two options: mean of the fitted values, or variance of the residuals. Default = NULL. (levels ordered alphabetically). Ignored if facet_by = NULL.

order

Command to arrange plots in ascending (default) or descending order of facet_by. Ignored if facet_by = NULL.

ncol

Number of columns to use in facet plots. Default = 4. Ignored if facet_by = NULL.

scales

When faceting, Specifies whether plotting scales should be fixed (fixed), free (free), or free in one dimension (free_x, free_y). Default = fixed. Ignored if facet_by = NULL.

save

Specifies if output plot should be saved as a png file. Default = FALSE.

save_dir

Path to folder where png file should be saved. Default = Current working directory.

...

Provision to include additional arguments.

Value

A list of five ggplot objects. These objects can optionally be saved as five separate png files.

Details

The following diagnostic plots are produced:

  1. Fitted vs observed values (all records, or faceted).

  2. Normal probability plot (all records, or faceted).

  3. Residuals vs fitted (all records or faceted).

  4. Histogram of residuals (all records) or boxplot of residuals (faceted).

  5. Residuals vs model fixed predictor(s) (all records only). This displays a linear model (red) for each fixed predictor vs the model's residuals. The blue line is a loess-smoothed line. The main purpose of this plot is to check whether or not the relationship between residuals and a predictor is linear.

Examples

## Example 1
# attach(lme4)
# sleepstudy$type <- rep(c("A","B","C"),60)

# run model
# mod <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)

# run diagnostic plots - with facet option
# diag_lmer(model = mod, data = sleepstudy, facet_by = "Subject", order_by = "mean", order = "ascending", ncol = 4, scales = "fixed", colour_by = "type")

## Example 2 - without facet option, and saving to png file
# diag_lmer(model = mod, data = sleepstudy, facet_by = NULL, order_by = "mean", order = "ascending", ncol = 4, scales = "fixed", colour_by = "type", save = TRUE)