Generating a variety of diagnostic plots for a mixed-effects regression (lmer) model
diag_lmer.Rd
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, orvariance
of the residuals. Default =NULL
. (levels ordered alphabetically). Ignored iffacet_by
=NULL
.- order
Command to arrange plots in
ascending
(default) ordescending
order of facet_by. Ignored iffacet_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 iffacet_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:
Fitted vs observed values (all records, or faceted).
Normal probability plot (all records, or faceted).
Residuals vs fitted (all records or faceted).
Histogram of residuals (all records) or boxplot of residuals (faceted).
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)