partial_residuals {regressinator} | R Documentation |
Augment a model fit with partial residuals for all terms
Description
Construct a data frame containing the model data, partial residuals for all quantitative predictors, and predictor effects, for use in residual diagnostic plots and other analyses. The result is in tidy form (one row per predictor per observation), allowing it to be easily manipulated for plots and simulations.
Usage
partial_residuals(fit, predictors = everything())
Arguments
fit |
The model to obtain residuals for. This can be a model fit with
|
predictors |
Predictors to calculate partial residuals for. Defaults to
all predictors, skipping factors. Predictors can be specified using
tidyselect syntax; see |
Value
Data frame (tibble) containing the model data and residuals in tidy form. There is one row per selected predictor per observation. All predictors are included as columns, plus the following additional columns:
.obs |
Row number of this observation in the original model data frame. |
.predictor_name |
Name of the predictor this row gives the partial residual for. |
.predictor_value |
Value of the predictor this row gives the partial residual for. |
.partial_resid |
Partial residual for this predictor for this observation. |
.predictor_effect |
Predictor effect |
Predictors and regressors
To define partial residuals, we must distinguish between the predictors, the measured variables we are using to fit our model, and the regressors, which are calculated from them. In a simple linear model, the regressors are equal to the predictors. But in a model with polynomials, splines, or other nonlinear terms, the regressors may be functions of the predictors.
For example, in a regression with a single predictor , the regression
model
has one regressor,
. But if we
choose a polynomial of degree 3, the model is
, and the regressors are
.
Similarly, if we have predictors and
and form a model
with main effects and an interaction, the regressors are
.
Partial residuals are defined in terms of the predictors, not the regressors, and are intended to allow us to see the shape of the relationship between a particular predictor and the response, and to compare it to how we have chosen to model it with regressors. Partial residuals are not useful for categorical (factor) predictors, and so these are omitted.
Linear models
Consider a linear model where . The mean function
is a linear combination of
regressors. Let
be the fitted model and
be its intercept.
Choose a predictor , the focal predictor, to calculate partial
residuals for. Write the mean function as
, where
is the value of the focal predictor, and
represents all
other predictors.
If is the residual for observation
, the partial residual is
Setting means setting all other numeric predictors to 0; factor
predictors are set to their first (baseline) level.
Generalized linear models
Consider a generalized linear model where , where
is a link function. Let
be the fitted model and
be
its intercept.
Let be the working residual for observation
, defined to
be
Choose a predictor , the focal predictor, to calculate partial
residuals for. Write
as
, where
is the
value of the focal predictor, and
represents all other predictors.
Hence
gives the model's prediction on the link scale.
The partial residual is again
Interpretation
In linear regression, because the residuals should have mean zero
in a well-specified model, plotting the partial residuals against
should produce a shape matching the modeled relationship
. If the
model is wrong, the partial residuals will appear to deviate from the fitted
relationship. Provided the regressors are uncorrelated or approximately
linearly related to each other, the plotted trend should approximate the true
relationship between
and the response.
In generalized linear models, this is approximately true if the link function
is approximately linear over the range of observed
values.
Additionally, the function can be used to show the
relationship between the focal predictor and the response. In a linear model,
the function is linear; with polynomial or spline regressors, it is
nonlinear. This function is the predictor effect function, and the
estimated predictor effects
are
included in this function's output.
Limitations
Factor predictors (as factors, logical, or character vectors) are detected
automatically and omitted. However, if a numeric variable is converted to
factor in the model formula, such as with y ~ factor(x)
, the function
cannot determine the appropriate type and will raise an error. Create factors
as needed in the source data frame before fitting the model to avoid this
issue.
References
R. Dennis Cook (1993). "Exploring Partial Residual Plots", Technometrics, 35:4, 351-362. doi:10.1080/00401706.1993.10485350
Cook, R. Dennis, and Croos-Dabrera, R. (1998). "Partial Residual Plots in Generalized Linear Models." Journal of the American Statistical Association 93, no. 442: 730–39. doi:10.2307/2670123
Fox, J., & Weisberg, S. (2018). "Visualizing Fit and Lack of Fit in Complex Regression Models with Predictor Effect Plots and Partial Residuals." Journal of Statistical Software, 87(9). doi:10.18637/jss.v087.i09
See Also
binned_residuals()
for the related binned residuals;
augment_longer()
for a similarly formatted data frame of ordinary
residuals; vignette("linear-regression-diagnostics")
,
vignette("logistic-regression-diagnostics")
, and
vignette("other-glm-diagnostics")
for examples of plotting and
interpreting partial residuals
Examples
fit <- lm(mpg ~ cyl + disp + hp, data = mtcars)
partial_residuals(fit)
# You can select predictors with tidyselect syntax:
partial_residuals(fit, c(disp, hp))
# Predictors with multiple regressors are supported:
fit2 <- lm(mpg ~ poly(disp, 2), data = mtcars)
partial_residuals(fit2)
# Allowing an interaction by number of cylinders is fine, but partial
# residuals are not generated for the factor. Notice the factor must be
# created first, not in the model formula:
mtcars$cylinders <- factor(mtcars$cyl)
fit3 <- lm(mpg ~ cylinders * disp + hp, data = mtcars)
partial_residuals(fit3)