calculate_residuals: Calculate Predictions and Residuals From a Model

View source: R/residuals.R

calculate_residualsR Documentation

Calculate Predictions and Residuals From a Model

Description

From a model, calculate the predicted values from a set of data and then calculate the residuals of those predictions.

Usage

calculate_residuals(object, data = NULL)

## S3 method for class 'lm'
calculate_residuals(object, data = NULL)

Arguments

object

A model object (such as a fitted lm object).

data

(Optional) a data set used to create predictions (if omitted, the fitted values are used).

Details

Known Limitations

This function will not work if transformations to variables are done in the model formula (e.g., lm(mpg ~ wt + as.factor(vs), data = mtcars)). Variable transformations should be done to the data set before creating the model formula.

Value

A tibble with columns .pred and .resid.

Examples

library(tidytest)

#> `lm` Method
mod_lm_fit <- lm(mpg ~ disp + wt + hp, data = mtcars)

calculate_residuals(mod_lm_fit)
calculate_residuals(mod_lm_fit, mtcars[1:15, ])


RobbyLankford/tidytest documentation built on Jan. 27, 2024, 7:36 a.m.