This function computes partial residuals based on a data grid,
where the data grid is usually a data frame from all combinations of factor
variables or certain values of numeric vectors. This data grid is usually used
newdata argument in
predict(), and can be created with
residualize_over_grid(grid, model, ...) # S3 method for data.frame residualize_over_grid(grid, model, pred_name, type, ...) # S3 method for ggeffects residualize_over_grid(grid, model, protect_names = TRUE, ...)
A data frame representing the data grid, or an object of class
The model for which to compute partial residuals. The data grid
Currently not used.
The name of the focal predictor, for which partial residuals are computed.
Deprecated. Formally was the residual type. Now is always
A data frame with residuals for the focal predictor.
For generalized linear models (glms), residualized scores are
inv.link(link(Y) + r) where
Y are the predicted
values on the response scale, and
r are the working residuals.
For (generalized) linear mixed models, the random effect are also partialled out.
Fox J, Weisberg S. Visualizing Fit and Lack of Fit in Complex Regression Models with Predictor Effect Plots and Partial Residuals. Journal of Statistical Software 2018;87.
library(ggeffects) set.seed(1234) x <- rnorm(200) z <- rnorm(200) # quadratic relationship y <- 2 * x + x^2 + 4 * z + rnorm(200) d <- data.frame(x, y, z) model <- lm(y ~ x + z, data = d) pr <- ggpredict(model, c("x [all]", "z")) head(residualize_over_grid(pr, model))#> x group predicted #> 53 -1.207 0.07 -1.797239 #> 402 0.277 1.08 4.888712 #> 518 1.084 0.07 3.232202 #> 9 -2.346 1.08 4.133561 #> 428 0.429 0.07 1.801594 #> 441 0.506 1.08 5.659527