residualize_over_grid: Compute partial residuals from a data grid

View source: R/residualize_over_grid.R

residualize_over_gridR Documentation

Compute partial residuals from a data grid


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 as newdata argument in predict(), and can be created with new_data().


residualize_over_grid(grid, model, ...)

## S3 method for class 'data.frame'
residualize_over_grid(grid, model, predictor_name, ...)

## S3 method for class 'ggeffects'
residualize_over_grid(grid, model, protect_names = TRUE, ...)



A data frame representing the data grid, or an object of class ggeffects, as returned by predict_response().


The model for which to compute partial residuals. The data grid grid should match to predictors in the model.


Currently not used.


The name of the focal predictor, for which partial residuals are computed.


Logical, if TRUE, preserves column names from the ggeffects objects that is used as grid.


A data frame with residuals for the focal predictor.

Partial Residuals

For generalized linear models (glms), residualized scores are computed as + 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.


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 <- predict_response(model, c("x [all]", "z"))
head(residualize_over_grid(pr, model))

strengejacke/ggeffects documentation built on June 20, 2024, 2:03 a.m.