check.resid: Residual Diagnostics for Linear, Multilevel and Mixed-Effects...

View source: R/check.resid.R

check.residR Documentation

Residual Diagnostics for Linear, Multilevel and Mixed-Effects Models

Description

This function performs residual diagnostics for linear models estimated by using the lm() function and for multilevel and linear mixed-effects models estimated by using the lmer() function from the lme4 package to detect nonlinearity (partial residual or component-plus-residual plots), nonconstant error variance (predicted values vs. residuals plot), and non-normality of residuals (Q-Q plot and histogram with density curves).

Usage

check.resid(model, type = c("linear", "homo", "normal"),
            resid = c("unstand", "stand", "student"), plot = TRUE,
            geom = c("all", "none", "lm", "loess", "ci.lm", "ci.loess",
                     "hline", "qq", "hist"),
            filename = NULL, width = NA, height = NA, dpi = 600, check = TRUE, ...)

Arguments

model

a fitted model of class "lm", "lmerMod", or "lmerModLmerTest".

type

a character string specifying the type of the plot, i.e., "linear" for partial (component-plus-residual) plots, "homo" (default) for predicted values vs. residuals plot, and "normal" for Q-Q plot and histogram with a density plot. Note that partial residual or component-plus-residual plots are not available for models with interaction terms

resid

a character string specifying the type of residual used for the partial (component-plus-residual) plots or Q-Q plot and histogram, i.e., "unstand" for unstandardized residuals "stand" for standardized residuals, and "student" for studentized residuals. By default, studentized residuals are used for predicted values vs. residuals plot and unstandardized residuals are used for Q-Q plot and histogram. Note that studentized residuals are not available for multilevel and linear mixed-effects models when requesting Q-Q plots and histograms.

plot

logical: if TRUE (default), a plot is drawn.

geom

a character vector indicating which plot elements to draw, i.e., "all" for drawing all plot elements, "none" for drawing no regression or smoothing curve, "lm" for drawing the regression line, "loess" for drawing the LOESS smoothing curve, "ci.lm" for drawing confidence bands around the regression line, "ci.loess" for drawing confidence bands around the LOESS smoothing curve, "hline" or drawing a horizontal line at 0, "qq" for plotting Q-Q plots, and "hist" for plotting histograms. Note that the arguments "lm", "loess", "ci.lm", and "ci.less" are applicable when type = "linear", the arguments "hline", "loess", and "ci.loess" are applicable when type = "homo", and the arguments "lqq" and "hist" are applicable when type = "norm".

filename

a character string indicating the filename argument including the file extension in the ggsave function. Note that one of ".eps", ".ps", ".tex", ".pdf" (default), ".jpeg", ".tiff", ".png", ".bmp", ".svg" or ".wmf" needs to be specified as file extension in the file argument. Note that plots can only be saved when plot = TRUE.

width

a numeric value indicating the width argument (default is the size of the current graphics device) for the ggsave function.

height

a numeric value indicating the height argument (default is the size of the current graphics device) for the ggsave function.

dpi

a numeric value indicating the dpi argument (default is 600) for the ggsave function.

check

logical: if TRUE (default), argument specification is checked.

...

additional plotting arguments from the plot.misty.object() function, see 'Details in the plot.misty.object function.

Details

Nonlinearity

The violation of the assumption of linearity implies that the model cannot accurately capture the systematic pattern of the relationship between the outcome and predictor variables. In other words, the specified regression surface does not accurately represent the relationship between the conditional mean values of Y and the Xs. That means the average error E(\varepsilon) is not 0 at every point on the regression surface (Fox, 2015).

In multiple regression, plotting the outcome variable Y against each predictor variable X can be misleading because it does not reflect the partial relationship between Y and X (i.e., statistically controlling for the other Xs), but rather the marginal relationship between Y and X (i.e., ignoring the other Xs). Partial residual plots or component-plus-residual plots should be used to detect nonlinearity in multiple regression. The partial residual for the jth predictor variable is defined as

e_i^{(j)} = b_jX_{ij} + e_i

The linear component of the partial relationship between Y and X_j is added back to the least-squares residuals, which may include an unmodeled nonlinear component. Then, the partial residual e_i^{(j)} is plotted against the predictor variable X_j. Nonlinearity may become apparent when a non-parametric regression smoother is applied.

By default, the function plots each predictor against the partial residuals, and draws the linear regression and the LOESS smoothing curve to the partial residual plots.

Nonconstant Error Variance

The violation of the assumption of constant error variance, often referred to as heteroscedasticity, implies that the variance of the outcome variable around the regression surface is not the same at every point on the regression surface (Fox, 2015).

Plotting residuals against the outcome variable Y instead of the predicted values \hat{Y} is not recommended because Y = \hat{Y} + e. Consequently, the linear correlation between the outcome variable Y and the residuals e is \sqrt{1 - R^2} where R is the multiple correlation coefficient. In contrast, plotting residuals against the predicted values \hat{Y} is much easier to examine for evidence of nonconstant error variance as the correlation between \hat{Y} and e is 0. Note that the least-squares residuals generally have unequal variance Var(e_i) = \sigma^2 / (1 - h_i) where h is the leverage of observation i, even if errors have constant variance \sigma^2. The studentized residuals e^*_i, however, have a constant variance under the assumption of the regression model. Residuals are studentized by dividing them by \sigma^2_i(\sqrt{(1 - h_i)} where \sigma^2_i is the estimate of \sigma^2 obtained after deleting the ith observation, and h_i is the leverage of observation i (Meuleman et al, 2015).

By default, the function plots the predicted values against the studentized residuals. It also draws a horizontal line at 0, a LOESS smoothing curve for all residuals as well as separate LOESS smoothing curves for positive and negative residuals.

Non-normality of Residuals

Statistical inference under the violation of the assumption of normally distributed errors is approximately valid in all but small samples. However, the efficiency of least squares is not robust because the least-squares estimator is the most efficient and unbiased estimator only when the errors are normally distributed. For instance, when error distributions have heavy tails, the least-squares estimator becomes much less efficient compared to robust estimators. In addition, error distributions with heavy-tails result in outliers and compromise the interpretation of conditional means because the mean is not an accurate measure of central tendency in a highly skewed distribution. Moreover, a multimodal error distribution suggests the omission of one or more discrete explanatory variables that naturally divide the data into groups (Fox, 2016).

By default, the function plots a Q-Q plot of the unstandardized residuals, and a histogram of the unstandardized residuals and a density plot. Note that studentized residuals follow a t-distribution with n - k - 2 degrees of freedom where n is the sample size and k is the number of predictors. However, the normal and t-distribution are nearly identical unless the sample size is small. Moreover, even if the model is correct, the studentized residuals are not an independent random sample from t_{n - k - 2}. Residuals are correlated with each other depending on the configuration of the predictor values. The correlation is generally negligible unless the sample size is small.

Value

Returns an object of class misty.object, which is a list with following entries:

call

function call

type

type of analysis

model

model specified in model

args

specification of function arguments

plot

ggplot2 object for plotting the residuals

Note

This function uses a modified copy of the partial() and calc_ranef() function in the remef package by Sven Hohenstein and Reinhold Kliegl (2025) when requesting partial residual plots for linear mixed-effects models.

Author(s)

Takuya Yanagida

References

Fox, J. (2016). Applied regression analysis and generalized linear models (3rd ed.). Sage Publications, Inc.

Hohenstein, S., & Kliegl, R. (2025). remef: Remove Partial Effects. R package version 1.0.7, https://github.com/hohenstein/remef

Meuleman, B., Loosveldt, G., & Emonds, V. (2015). Regression analysis: Assumptions and diagnostics. In H. Best & C. Wolf (Eds.), The SAGE handbook of regression analysis and causal inference (pp. 83-110). Sage.

See Also

check.collin, check.outlier

Examples

#————————————————————————————————————————————————————————————————————————————
# Linear Model

# Estimate linear model
mod.lm <- lm(Ozone ~ Solar.R + Wind + Temp, data = airquality)

#——————————————————————————————————————
## Nonlinearity

# Example 1a: Partial (component-plus-residual) plots
check.resid(mod.lm, type = "linear")

# Example 1b: Plot with smoothing curve only
check.resid(mod.lm, type = "linear", geom = "loess")

# Example 1c: Additional arguments
# see Details in the help page of the function plot.misty.object
check.resid(mod.lm, type = "linear", point.size = 1, point.shape = 2)

#——————————————————————————————————————
## Nonconstant Error Variance

# Example 2a: Predicted values vs. residuals plot
check.resid(mod.lm, type = "homo")

# Example 2b: Plot with horizontal line only
check.resid(mod.lm, type = "homo", geom = "hline")

# Example 2c: Additional arguments
# see Details in the help page of the function plot.misty.object
check.resid(mod.lm, type = "homo", point.shape = 2, linetype = "solid", line.col = "blue")

#——————————————————————————————————————
## Non-Normality of Residuals

# Example 3a: Q-Q plot and histogram with density plot
check.resid(mod.lm, type = "normal")

# Example 3b: Q-Q plot only
check.resid(mod.lm, type = "normal", geom = "qq")

# Example 2c: Additional arguments
# see Details in the help page of the function plot.misty.object
check.resid(mod.lm, type = "normal", point.shape = 2, abline.col = "red")

## Not run: 
#————————————————————————————————————————————————————————————————————————————
# Multilevel and Linear Mixed-Effects Model

# Load lme4 package
library(lme4)

# Estimate two-level mixed-effects model
mod.lmer <- lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy)

#——————————————————————————————————————
## Nonlinearity

# Example 4a: Partial (component-plus-residual) plots
check.resid(mod.lmer, type = "linear")

# Example 4b: Plot with smoothing curve only
check.resid(mod.lmer, type = "linear", geom = "loess")

# Example 4c: Additional arguments
# see Details in the help page of the function plot.misty.object
check.resid(mod.lmer, type = "linear", point.size = 1, point.shape = 2)

#——————————————————————————————————————
## Nonconstant Error Variance

# Example 5a: Predicted values vs. residuals plot
check.resid(mod.lmer, type = "homo")

# Example 5b: Plot with horizontal line only
check.resid(mod.lmer, type = "homo", geom = "hline")

# Example 5c: Additional arguments
# see Details in the help page of the function plot.misty.object
check.resid(mod.lmer, type = "homo", point.shape = 2, linetype = "solid",
            line.col = "blue")

#——————————————————————————————————————
## Non-Normality of Residuals

# Example 6c: Q-Q plot and histogram with density plot
check.resid(mod.lmer, type = "normal")

# Example 6b: Q-Q plot only
check.resid(mod.lmer, type = "normal", geom = "qq")

# Example 6c: Additional arguments
# see Details in the help page of the function plot.misty.object
check.resid(mod.lmer, type = "normal", point.shape = 2, abline.col = "red")

#————————————————————————————————————————————————————————————————————————————
# Save Plot

# Example 7: Save plot as PNG file
check.resid(mod.lm, type = "linear", filename = "Linear.png", width = 15, height = 5)

## End(Not run)

misty documentation built on Nov. 24, 2026, 5:06 p.m.

Related to check.resid in misty...