Title: | Panels and Interactive Versions of Diagnostic Plots using 'ggplot2' |
---|---|
Description: | An R package for creating diagnostic plots for models. The package allows for the creation of panels of plots and interactive plots. |
Authors: | Katherine Goode [aut, cre], Kathleen Rey [aut], Greenwood Mark [ctb] |
Maintainer: | Katherine Goode <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.3.0.9000 |
Built: | 2025-02-17 06:32:29 UTC |
Source: | https://github.com/goodekat/ggresidpanel |
A dataset that contains information from a study done on dives of emperor penguins by Jessica Meir and others. The scientists were interested in understanding how the heart rate of the penguins relates to the depth and duration of the dive. The study involved attaching a device to penguins that recorded the heart rate of the bird during a dive. The dataset contains multiple observations recorded from 9 penguins. The dataset has 125 observations and 4 variables.
penguins
penguins
A data.frame.
The variables in the dataset are as follows.
heartrate |
Heart rate of the penguin during the dive (beats per minute) |
depth |
Depth of the dive (meters) |
duration |
Duration of the dive (minutes) |
bird |
The id number associated with a penguin |
Creates a panel of residual diagnostic plots given inputs of residuals and fitted values.
resid_auxpanel( residuals, predicted, plots = "default", bins = 30, smoother = FALSE, qqline = TRUE, qqbands = FALSE, scale = 1, theme = "bw", axis.text.size = 10, title.text.size = 12, title.opt = TRUE, nrow = NULL, alpha = 0.6 )
resid_auxpanel( residuals, predicted, plots = "default", bins = 30, smoother = FALSE, qqline = TRUE, qqbands = FALSE, scale = 1, theme = "bw", axis.text.size = 10, title.text.size = 12, title.opt = TRUE, nrow = NULL, alpha = 0.6 )
residuals |
The residuals from the model. |
predicted |
The fitted values from the model. |
plots |
Plots chosen to include in the panel of plots. The default panel includes a residual plot, a normal quantile plot, an index plot, and a histogram of the residuals. (See details for the options available.) |
bins |
Number of bins for histogram of the residuals. Default is set to 30. |
smoother |
Indicates whether or not to include a smoother on the residual plot and/or index plot. Specify TRUE or FALSE. Default is set to FALSE. |
qqline |
Indicates whether to include a 1-1 line on the qq-plot. Specify TRUE or FALSE. Default is set to TRUE. |
qqbands |
Indicates whether to include confidence bands on the qq-plot. Specify TRUE or FALSE. Default is set to FALSE. |
scale |
Scales the size of the graphs in a panel. Takes values in (0,1]. |
theme |
ggplot2 theme to be used. Options are |
axis.text.size |
Specifies the size of the text for the axis labels of all plots. |
title.text.size |
Specifies the size of the text for the titles of all plots. |
title.opt |
Indicates whether or not to include a title on the plots. Specify TRUE or FALSE. Default is set to TRUE. |
nrow |
Sets the number of rows in the panel. |
alpha |
Sets the alpha level for displays with points. Default is set to 0.6. |
The following grid options can be chosen for the plots
argument.
"all": This creates a panel of all plot types included in the package
that are available for resid_auxpanel
. (See plot descriptions under
individual options.)
"default": This creates a panel with a residual plot, a normal quantile plot of the residuals, an index plot of the residuals, and a histogram of the residuals.
"SAS": This creates a panel of a residual plot, a normal quantile plot of the residuals, a histogram of the residuals, and a boxplot of the residuals. This was modeled after the residpanel option in proc mixed from SAS version 9.4.
A vector of individual plots can also be specified. For example, one
can specify plots = c("boxplot", "hist")
or plots = "qq"
. The
individual plot options are as follows.
"boxplot"
: A boxplot of residuals
"hist"
: A histogram of residuals
"index"
: A plot of residuals versus observation number
"qq"
: A normal quantile plot of residuals
"resid"
: A plot of residuals versus predicted values
Details on the creation of the plots can be found in the details section of
the help file for resid_panel
.
A panel of residual diagnostic plots containing plots specified.
# Fit a regression tree to the penguins data penguin_tree <- rpart::rpart(heartrate ~ depth + duration, data = penguins) # Obtain the predictions from the model on the observed data penguin_tree_pred <- predict(penguin_tree) # Obtain the residuals from the model penguin_tree_resid <- penguins$heartrate - penguin_tree_pred # Create a panel with the residual and index plot resid_auxpanel(residuals = penguin_tree_resid, predicted = penguin_tree_pred, plots = c("resid", "index", "yvp"))
# Fit a regression tree to the penguins data penguin_tree <- rpart::rpart(heartrate ~ depth + duration, data = penguins) # Obtain the predictions from the model on the observed data penguin_tree_pred <- predict(penguin_tree) # Obtain the residuals from the model penguin_tree_resid <- penguins$heartrate - penguin_tree_pred # Create a panel with the residual and index plot resid_auxpanel(residuals = penguin_tree_resid, predicted = penguin_tree_pred, plots = c("resid", "index", "yvp"))
Used to calibrate expectations for simulation variability when assumptions are true (see simulate
),
to compare to the actual observed residuals in any of a suite of diagnostic plots.
This function is based on the resid_compare
function and requires the fitted model and the data set.
resid_calibrate( model, plots = "default", nsim = 1, identify = TRUE, shuffle = FALSE, type = NA, bins = 30, smoother = TRUE, qqline = TRUE, qqbands = FALSE, scale = 1, theme = "bw", axis.text.size = 10, title.text.size = 12, title.opt = TRUE, nrow = NULL, alpha = 0.6 )
resid_calibrate( model, plots = "default", nsim = 1, identify = TRUE, shuffle = FALSE, type = NA, bins = 30, smoother = TRUE, qqline = TRUE, qqbands = FALSE, scale = 1, theme = "bw", axis.text.size = 10, title.text.size = 12, title.opt = TRUE, nrow = NULL, alpha = 0.6 )
model |
Model fit using either |
plots |
Plots chosen to include in the panel of plots. The default panel includes a residual plot, a normal quantile plot, an index plot, and a histogram of the residuals. (See details for the options available.) |
nsim |
Number of simulated models, defaults to 1 |
identify |
TRUE to label true residuals, FALSE to hide |
shuffle |
TRUE to shuffle the order of the residuals, FALSE to have real data last |
type |
Type of residuals to use in the plot. If not specified, the default residual type for each model type is used. (See details for the options available.) |
bins |
Number of bins to use when creating a histogram of the residuals. Default is set to 30. |
smoother |
Indicates whether or not to include a smoother on the residual vs fitted and index plots. Specify TRUE or FALSE. Default is set to TRUE. |
qqline |
Indicates whether to include a 1-1 line on the qq-plot. Specify TRUE or FALSE. Default is set to TRUE. |
qqbands |
Indicates whether to include confidence bands on the qq-plot. Specify TRUE or FALSE. Default is set to FALSE. |
scale |
Scales the size of the graphs in the panel. Takes values in (0,1]. |
theme |
ggplot2 theme to be used. Current options are |
axis.text.size |
Specifies the size of the text for the axis labels of all plots in the panel. |
title.text.size |
Specifies the size of the text for the titles of all plots in the panel. |
title.opt |
Indicates whether or not to include a title on the plots in the panel. Specify TRUE or FALSE. Default is set to TRUE. |
nrow |
Sets the number of rows in the panel. |
alpha |
Sets the alpha level for displays with points. Default is set to 0.6. |
Creates a panel of residual diagnostic plots the actual model and simulated responses from that model. Currently accepts models of type "lm" (future versions will accept "glm", "lmerMod", "lmerModLmerTest", and "glmerMod").
The first two sections below contain information on the available input
options for the plots
and type
arguments in resid_compare
.
The third section contains details relating to the creation of the plots.
Options for Plots
The following options can be chosen for the plots
argument.
"all": This creates a panel of all plot types included in the package
that are available for the model type input into residpanel
. (See note
below.)
"default": This creates a panel with a residual plot, a normal quantile plot of the residuals, an index plot of the residuals, and a histogram of the residuals.
"R": This creates a panel with a residual plot, a normal
quantile plot of the residuals, a location-scale plot, and a leverage versus
residuals plot. This was modeled after the plots shown in R if the
plot()
base function is applied to an lm
model. This option can
only be used with an lm
or glm
model.
"SAS": This creates a panel with a residual plot, a normal quantile plot of the residuals, a histogram of the residuals, and a boxplot of the residuals. This was modeled after the residpanel option in proc mixed from SAS version 9.4.
A vector of individual plots can also be specified.
For example, one can specify plots = c("boxplot", "hist")
or
plots = "qq"
. The individual plot options are as follows.
"boxplot"
: A boxplot of residuals
"cookd"
: A plot of Cook's D values versus observation numbers
"hist"
: A histogram of residuals
"index"
: A plot of residuals versus observation numbers
"ls"
: A location scale plot of the residuals
"qq"
: A normal quantile plot of residuals
"lev"
: A plot of leverage values versus residuals
"resid"
: A plot of residuals versus predicted values
"yvp":
: A plot of observed response values versus predicted values
Note: "cookd"
, "ls"
, and "lev"
are only available for "lm"
and "glm" models.
Options for Type
Several residual types are available to be requested based on the model type
that is input into resid_panel
. These currently are as follows.
lm
residual options
"pearson"
:The Pearson residuals
"response"
: The raw residuals (Default for "lm")
"standardized"
: The standardized raw residuals
glm
residual options
"pearson"
: The Pearson residuals
"deviance"
: The deviance residuals (Default for "glm")
"response"
: The raw residuals
"stand.deviance"
: The standardized deviance residuals
"stand.pearson"
: The standardized Pearson residuals
lmer
, lmerTest
, and lme
residual options
"pearson"
: The Pearson residuals (Default for "lmer", "lmerTest", and "lme")
"response"
: The raw residuals
glmer
residual options
"pearson"
: The Pearson residuals
"deviance"
: The deviance residuals (Default for "glmer")
"response"
: The raw residuals
Note: The plots of "ls"
and "lev"
only accept standardized residuals.
Details on the Creation of Plots
boxplot
)Boxplot of the residuals.
cookd
) The horizontal line represents a cut-off to identify
highly influential points. The horizontal line is placed at 4/n where n is
the number of data points used in the model
.
hist
)Plots a histogram of the residuals. The density curve overlaid has mean equal to zero and standard deviation equal to the standard deviation of the residuals.
index
)Plots the residuals on the y-axis and the observation number associated with the residual on the x-axis.
lev
)Plots the standardized residuals on the y-axis
and the leverage values on the x-axis. A lowess curve is overlaid, and Cook's
D contours are included for and
.
ls
)Plots the square root of the absolute value
of the standardized residuals on the y-axis and the predicted values on the
x-axis. The predicted values are plotted on the original scale for glm
and glmer
models. A lowess curve is overlaid.
qq
)Makes use of the R
package qqplotr
for
creating a normal quantile plot of the residuals.
resid
)Plots the residuals on the y-axis and the
predicted values on the x-axis. The predicted values are plotted on the
original scale for glm
and glmer
models.
yvp
)Plots the response variable from the
model on the y-axis and the predicted values on the x-axis. Both response
variable and predicted values are plotted on the original scale for
glm
and glmer
models.
A panel of residual diagnostic plots containing plots specified for each model.
# Fit a model to the penguins data penguin_model <- lm(heartrate ~ depth + duration, data = penguins) resid_calibrate( model = penguin_model, plots = "qq", nsim = 3, shuffle = TRUE, identify = TRUE )
# Fit a model to the penguins data penguin_model <- lm(heartrate ~ depth + duration, data = penguins) resid_calibrate( model = penguin_model, plots = "qq", nsim = 3, shuffle = TRUE, identify = TRUE )
Creates a panel of residual diagnostic plots given a list of models. Currently accepts models of type "lm", "glm", "lmerMod", "lmerModLmerTest", and "glmerMod".
resid_compare( models, plots = "default", type = NA, bins = 30, smoother = TRUE, qqline = TRUE, qqbands = FALSE, scale = 1, theme = "bw", axis.text.size = 10, title.text.size = 12, title.opt = TRUE, nrow = NULL, alpha = 0.6 )
resid_compare( models, plots = "default", type = NA, bins = 30, smoother = TRUE, qqline = TRUE, qqbands = FALSE, scale = 1, theme = "bw", axis.text.size = 10, title.text.size = 12, title.opt = TRUE, nrow = NULL, alpha = 0.6 )
models |
List of models fit using either |
plots |
Plots chosen to include in the panel of plots. The default panel includes a residual plot, a normal quantile plot, an index plot, and a histogram of the residuals. (See details for the options available.) |
type |
Type of residuals to use in the plot. If not specified, the default residual type for each model type is used. (See details for the options available.) |
bins |
Number of bins to use when creating a histogram of the residuals. Default is set to 30. |
smoother |
Indicates whether or not to include a smoother on the residual vs fitted and index plots. Specify TRUE or FALSE. Default is set to TRUE. |
qqline |
Indicates whether to include a 1-1 line on the qq-plot. Specify TRUE or FALSE. Default is set to TRUE. |
qqbands |
Indicates whether to include confidence bands on the qq-plot. Specify TRUE or FALSE. Default is set to FALSE. |
scale |
Scales the size of the graphs in the panel. Takes values in (0,1]. |
theme |
ggplot2 theme to be used. Current options are |
axis.text.size |
Specifies the size of the text for the axis labels of all plots in the panel. |
title.text.size |
Specifies the size of the text for the titles of all plots in the panel. |
title.opt |
Indicates whether or not to include a title on the plots in the panel. Specify TRUE or FALSE. Default is set to TRUE. |
nrow |
Sets the number of rows in the panel. |
alpha |
Sets the alpha level for displays with points. Default is set to 0.6. |
The first two sections below contain information on the available input
options for the plots
and type
arguments in resid_compare
.
The third section contains details relating to the creation of the plots.
Options for Plots
The following options can be chosen for the plots
argument.
"all": This creates a panel of all plot types included in the package
that are available for the model type input into residpanel
. (See note
below.)
"default": This creates a panel with a residual plot, a normal quantile plot of the residuals, an index plot of the residuals, and a histogram of the residuals.
"R": This creates a panel with a residual plot, a normal
quantile plot of the residuals, a location-scale plot, and a leverage versus
residuals plot. This was modeled after the plots shown in R if the
plot()
base function is applied to an lm
model. This option can
only be used with an lm
or glm
model.
"SAS": This creates a panel with a residual plot, a normal quantile plot of the residuals, a histogram of the residuals, and a boxplot of the residuals. This was modeled after the residpanel option in proc mixed from SAS version 9.4.
A vector of individual plots can also be specified.
For example, one can specify plots = c("boxplot", "hist")
or
plots = "qq"
. The individual plot options are as follows.
"boxplot"
: A boxplot of residuals
"cookd"
: A plot of Cook's D values versus observation numbers
"hist"
: A histogram of residuals
"index"
: A plot of residuals versus observation numbers
"ls"
: A location scale plot of the residuals
"qq"
: A normal quantile plot of residuals
"lev"
: A plot of leverage values versus residuals
"resid"
: A plot of residuals versus predicted values
"yvp":
: A plot of observed response values versus predicted values
Note: "cookd"
, "ls"
, and "lev"
are only available for "lm"
and "glm" models.
Options for Type
Several residual types are available to be requested based on the model type
that is input into resid_panel
. These currently are as follows.
lm
residual options
"pearson"
:The Pearson residuals
"response"
: The raw residuals (Default for "lm")
"standardized"
: The standardized raw residuals
glm
residual options
"pearson"
: The Pearson residuals
"deviance"
: The deviance residuals (Default for "glm")
"response"
: The raw residuals
"stand.deviance"
: The standardized deviance residuals
"stand.pearson"
: The standardized Pearson residuals
lmer
, lmerTest
, and lme
residual options
"pearson"
: The Pearson residuals (Default for "lmer", "lmerTest", and "lme")
"response"
: The raw residuals
glmer
residual options
"pearson"
: The Pearson residuals
"deviance"
: The deviance residuals (Default for "glmer")
"response"
: The raw residuals
Note: The plots of "ls"
and "lev"
only accept standardized residuals.
Details on the Creation of Plots
boxplot
)Boxplot of the residuals.
cookd
) The horizontal line represents a cut-off to identify
highly influential points. The horizontal line is placed at 4/n where n is
the number of data points used in the model
.
hist
)Plots a histogram of the residuals. The density curve overlaid has mean equal to zero and standard deviation equal to the standard deviation of the residuals.
index
)Plots the residuals on the y-axis and the observation number associated with the residual on the x-axis.
lev
)Plots the standardized residuals on the y-axis
and the leverage values on the x-axis. A lowess curve is overlaid, and Cook's
D contours are included for and
.
ls
)Plots the square root of the absolute value
of the standardized residuals on the y-axis and the predicted values on the
x-axis. The predicted values are plotted on the original scale for glm
and glmer
models. A lowess curve is overlaid.
qq
)Makes use of the R
package qqplotr
for
creating a normal quantile plot of the residuals.
resid
)Plots the residuals on the y-axis and the
predicted values on the x-axis. The predicted values are plotted on the
original scale for glm
and glmer
models.
yvp
)Plots the response variable from the
model on the y-axis and the predicted values on the x-axis. Both response
variable and predicted values are plotted on the original scale for
glm
and glmer
models.
A panel of residual diagnostic plots containing plots specified for each model.
# Fit two models to the penguins data penguin_model <- lme4::lmer(heartrate ~ depth + duration + (1|bird), data = penguins) penguin_model_log2 <- lme4::lmer(log(heartrate) ~ depth + duration + I(duration^2) + (1|bird), data = penguins) # Compare the residuals from the model resid_compare(list(penguin_model, penguin_model_log2)) # Adjust some options in the panel of plots resid_compare(list(penguin_model, penguin_model_log2), plots = c("resid", "yvp"), smoother = TRUE, theme = "grey")
# Fit two models to the penguins data penguin_model <- lme4::lmer(heartrate ~ depth + duration + (1|bird), data = penguins) penguin_model_log2 <- lme4::lmer(log(heartrate) ~ depth + duration + I(duration^2) + (1|bird), data = penguins) # Compare the residuals from the model resid_compare(list(penguin_model, penguin_model_log2)) # Adjust some options in the panel of plots resid_compare(list(penguin_model, penguin_model_log2), plots = c("resid", "yvp"), smoother = TRUE, theme = "grey")
Creates a panel of interactive residual diagnostic plots given a model. Currently accepts models of type "lm", "glm", "lmerMod", "lmerModLmerTest", "lme", and "glmerMod".
resid_interact( model, plots = "default", type = NA, bins = 30, smoother = TRUE, qqline = TRUE, scale = 0.9, theme = "bw", axis.text.size = 10, title.text.size = 12, title.opt = TRUE, nrow = NULL, alpha = 0.6 )
resid_interact( model, plots = "default", type = NA, bins = 30, smoother = TRUE, qqline = TRUE, scale = 0.9, theme = "bw", axis.text.size = 10, title.text.size = 12, title.opt = TRUE, nrow = NULL, alpha = 0.6 )
model |
Model fit using either |
plots |
Plots chosen to include in the panel of plots. The default panel
includes a residual plot, a normal quantile plot, an index plot,
and a histogram of the residuals. (See details in the help file
for |
type |
Type of residuals to use in the plot. If not specified, the
default residual type for each model type is used. (See details in the help file
for |
bins |
Number of bins to use when creating a histogram of the residuals. Default is set to 30. |
smoother |
Indicates whether or not to include a smoother on the residual vs fitted and index plots. Specify TRUE or FALSE. Default is set to TRUE. |
qqline |
Indicates whether to include a 1-1 line on the qq-plot. Specify
TRUE or FALSE. Default is set to TRUE. (The option of |
scale |
Scales the size of the graphs in the panel. Takes values in (0,1]. |
theme |
ggplot2 theme to be used. Current options are |
axis.text.size |
Specifies the size of the text for the axis labels of all plots in the panel. |
title.text.size |
Specifies the size of the text for the titles of all plots in the panel. |
title.opt |
Indicates whether or not to include a title on the plots in the panel. Specify TRUE or FALSE. Default is set to TRUE. |
nrow |
Sets the number of rows in the panel. |
alpha |
Sets the alpha level for displays with points. Default is set to 0.6. |
Details on the creation of the plots can be found in the details section of
the help file for resid_panel
.
A panel of interactive residual diagnostic plots containing plots specified.
# Fit a model to the penguin data penguin_model <- lme4::lmer(heartrate ~ depth + duration + (1|bird), data = penguins) # Create the default interactive panel resid_interact(penguin_model) # Select only the residual plot and qq-plot to be included in the panel, # set the number of rows to 2, change the theme to classic resid_interact(penguin_model, plots = c("resid", "qq"), nrow = 2, theme = "classic")
# Fit a model to the penguin data penguin_model <- lme4::lmer(heartrate ~ depth + duration + (1|bird), data = penguins) # Create the default interactive panel resid_interact(penguin_model) # Select only the residual plot and qq-plot to be included in the panel, # set the number of rows to 2, change the theme to classic resid_interact(penguin_model, plots = c("resid", "qq"), nrow = 2, theme = "classic")
Creates a panel of residual diagnostic plots given a model. Currently accepts models of type "lm", "glm", "lmerMod", "lmerModLmerTest", "lme", and "glmerMod".
resid_panel( model, plots = "default", type = NA, bins = 30, smoother = TRUE, qqline = TRUE, qqbands = FALSE, scale = 1, theme = "bw", axis.text.size = 10, title.text.size = 12, title.opt = TRUE, nrow = NULL, alpha = 0.6 )
resid_panel( model, plots = "default", type = NA, bins = 30, smoother = TRUE, qqline = TRUE, qqbands = FALSE, scale = 1, theme = "bw", axis.text.size = 10, title.text.size = 12, title.opt = TRUE, nrow = NULL, alpha = 0.6 )
model |
Model fit using either |
plots |
Plots chosen to include in the panel of plots. The default panel includes a residual plot, a normal quantile plot, an index plot, and a histogram of the residuals. (See details for the options available.) |
type |
Type of residuals to use in the plot. If not specified, the default residual type for each model type is used. (See details for the options available.) |
bins |
Number of bins to use when creating a histogram of the residuals. Default is set to 30. |
smoother |
Indicates whether or not to include a smoother on the residual vs fitted and index plots. Specify TRUE or FALSE. Default is set to TRUE. |
qqline |
Indicates whether to include a 1-1 line on the qq-plot. Specify TRUE or FALSE. Default is set to TRUE. |
qqbands |
Indicates whether to include confidence bands on the qq-plot. Specify TRUE or FALSE. Default is set to FALSE. |
scale |
Scales the size of the graphs in the panel. Takes values in (0,1]. |
theme |
ggplot2 theme to be used. Current options are |
axis.text.size |
Specifies the size of the text for the axis labels of all plots in the panel. |
title.text.size |
Specifies the size of the text for the titles of all plots in the panel. |
title.opt |
Indicates whether or not to include a title on the plots in the panel. Specify TRUE or FALSE. Default is set to TRUE. |
nrow |
Sets the number of rows in the panel. |
alpha |
Sets the alpha level for displays with points. Default is set to 0.6. |
The first two sections below contain information on the available input
options for the plots
and type
arguments in resid_panel
.
The third section contains details relating to the creation of the plots.
Options for Plots
The following options can be chosen for the plots
argument.
"all": This creates a panel of all plot types included in the package
that are available for the model type input into resid_panel
. (See note
below.)
"default": This creates a panel with a residual plot, a normal quantile plot of the residuals, an index plot of the residuals, and a histogram of the residuals.
"R": This creates a panel with a residual plot, a normal
quantile plot of the residuals, a location-scale plot, and a residuals versus leverage
plot. This was modeled after the plots shown in R if the plot()
base function
is applied to an lm
model. This option can only be used with an lm
or
glm
model.
"SAS": This creates a panel with a residual plot, a normal quantile plot of the residuals, a histogram of the residuals, and a boxplot of the residuals. This was modeled after the residualpanel option in proc mixed from SAS version 9.4.
A vector of individual plots can also be specified.
For example, one can specify plots = c("boxplot", "hist")
or
plots = "qq"
. The individual plot options are as follows.
"boxplot"
: A boxplot of residuals
"cookd"
: A plot of Cook's D values versus observation numbers
"hist"
: A histogram of residuals
"index"
: A plot of residuals versus observation numbers
"ls"
: A location scale plot of the residuals
"qq"
: A normal quantile plot of residuals
"lev"
: A plot of standardized residuals versus leverage values
"resid"
: A plot of residuals versus predicted values
"yvp":
: A plot of observed response values versus predicted values
Note: "cookd"
, "ls"
, and "lev"
are only available for "lm" and
"glm" models.
Options for Type
Several residual types are available to be requested based on the model type
that is input into resid_panel
. These currently are as follows.
lm
residual options
"pearson"
:The Pearson residuals
"response"
: The raw residuals (Default for "lm")
"standardized"
: The standardized raw residuals
glm
residual options
"pearson"
: The Pearson residuals
"deviance"
: The deviance residuals (Default for "glm")
"response"
: The raw residuals
"stand.deviance"
: The standardized deviance residuals
"stand.pearson"
: The standardized Pearson residuals
lmer
, lmerTest
, and lme
residual options
"pearson"
: The Pearson residuals (Default for "lmer", "lmerTest", and "lme")
"response"
: The raw residuals
glmer
residual options
"pearson"
: The Pearson residuals
"deviance"
: The deviance residuals (Default for "glmer")
"response"
: The raw residuals
Note: The plots of "ls"
and "lev"
only accept standardized residuals.
Details on the Creation of Plots
boxplot
)Boxplot of the residuals.
cookd
) The horizontal line represents a cut-off to identify
highly influential points. The horizontal line is placed at 4/n where n is
the number of data points used in the model
.
hist
)Plots a histogram of the residuals. The density curve overlaid has mean equal to zero and standard deviation equal to the standard deviation of the residuals.
index
)Plots the residuals on the y-axis and the observation number associated with the residual on the x-axis.
lev
)Plots the standardized residuals on the y-axis and the leverage values on the x-axis with a loess curve is overlaid. Cook's D contour lines (which are a function of leverage and standardized residuals) are plotted as the red dashed lines for Cook's D values of 0.5 and 1.
ls
)Plots the square root of the absolute value
of the standardized residuals on the y-axis and the predicted values on the
x-axis. The predicted values are plotted on the original scale for glm
and glmer
models. A loess curve is overlaid.
qq
)Makes use of the R
package qqplotr
for
creating a normal quantile plot of the residuals.
resid
)Plots the residuals on the y-axis and the
predicted values on the x-axis. The predicted values are plotted on the
original scale for glm
and glmer
models.
yvp
)Plots the response variable from the
model on the y-axis and the predicted values on the x-axis. Both response
variable and predicted values are plotted on the original scale for
glm
and glmer
models.
A panel of residual diagnostic plots containing plots specified.
# Fit a model to the penguin data penguin_model <- lme4::lmer(heartrate ~ depth + duration + (1|bird), data = penguins) # Create the default panel resid_panel(penguin_model) # Select all plots to include in the panel and set the smoother option to TRUE resid_panel(penguin_model, plots = "all", smoother = TRUE) # Select only the residual plot and qq-plot to be included in the panel, # request confidence bands on the qq plot, and set the number of rows to 2 resid_panel(penguin_model, plots = c("resid", "qq"), qqbands = TRUE, nrow = 2) # Choose the SAS panel of plots, change the theme to classic, and remove the # titles of the plots resid_panel(penguin_model, plots = "SAS", theme = "classic", title.opt = FALSE)
# Fit a model to the penguin data penguin_model <- lme4::lmer(heartrate ~ depth + duration + (1|bird), data = penguins) # Create the default panel resid_panel(penguin_model) # Select all plots to include in the panel and set the smoother option to TRUE resid_panel(penguin_model, plots = "all", smoother = TRUE) # Select only the residual plot and qq-plot to be included in the panel, # request confidence bands on the qq plot, and set the number of rows to 2 resid_panel(penguin_model, plots = c("resid", "qq"), qqbands = TRUE, nrow = 2) # Choose the SAS panel of plots, change the theme to classic, and remove the # titles of the plots resid_panel(penguin_model, plots = "SAS", theme = "classic", title.opt = FALSE)
Creates a panel of plots of the residuals or response variable versus the predictor (x) variables in the model. Interactions between predictor variables are not included. Currently accepts models of type "lm", "glm", "lmerMod", "lmerModLmerTest", "lme", and "glmerMod".
resid_xpanel( model, yvar = "residual", type = NA, smoother = FALSE, scale = 1, theme = "bw", axis.text.size = 10, title.text.size = 12, title.opt = TRUE, nrow = NULL, jitter.width = 0, alpha = 0.6 )
resid_xpanel( model, yvar = "residual", type = NA, smoother = FALSE, scale = 1, theme = "bw", axis.text.size = 10, title.text.size = 12, title.opt = TRUE, nrow = NULL, jitter.width = 0, alpha = 0.6 )
model |
Model fit using either |
yvar |
Specifies the variable to put on the y-axis of the plots. Options are "residual" (default) or "response". |
type |
Type of residuals to use in the plot. If not specified, the default residual type for each model type is used. (See details for the options available.) |
smoother |
Indicates whether or not to include a smoother on the plots. Specify TRUE or FALSE. Default is set to FALSE. |
scale |
Scales the size of the graphs in the panel. Takes values in (0,1]. |
theme |
ggplot2 theme to be used. Current options are |
axis.text.size |
Specifies the size of the text for the axis labels of all plots in the panel. |
title.text.size |
Specifies the size of the text for the titles of all plots in the panel. |
title.opt |
Indicates whether or not to include a title on the plots in the panel. Specify TRUE or FALSE. Default is set to TRUE. |
nrow |
Sets the number of rows in the panel. |
jitter.width |
Specifies the amount of jitter to add in the plots of categorical variables. (Default is 0.) |
alpha |
Sets the alpha level for displays with points. Default is set to 0.6. |
Note that for x variables that are factors, the levels shown on the x-axis will be in the order that the levels are ordered in the dataframe. This can be adjusted by reordering the levels of the factor before the model is fit.
A panel of plots of the residuals or response variable versus the predictor variables. Violin plots are included with categorical variables.
# Fit a model to the penguin data penguin_model <- lme4::lmer(heartrate ~ depth + duration + (1|bird), data = penguins) # Create plots of the residuals versus the predictor variables resid_xpanel(penguin_model, theme = "classic", jitter.width = 0.1) # Create plots of the response variable versus the predictor variables resid_xpanel( model = penguin_model, yvar = "response", theme = "classic", smoother = TRUE, jitter.width = 0.1 )
# Fit a model to the penguin data penguin_model <- lme4::lmer(heartrate ~ depth + duration + (1|bird), data = penguins) # Create plots of the residuals versus the predictor variables resid_xpanel(penguin_model, theme = "classic", jitter.width = 0.1) # Create plots of the response variable versus the predictor variables resid_xpanel( model = penguin_model, yvar = "response", theme = "classic", smoother = TRUE, jitter.width = 0.1 )