Learn R Programming

modelr (version 0.1.1)

add_residuals: Add residuals to a data frame

Description

Add residuals to a data frame

Usage

add_residuals(data, model, var = "resid")

spread_residuals(data, ...)

gather_residuals(data, ..., .resid = "resid", .model = "model")

Arguments

data

A data frame used to generate the residuals

model, var

add_residuals takes a single model; the output column will be called pred

...

gather_residuals and spread_residuals take multiple models. The name will be taken from either the argument name of the name of the model.

.resid, .model

The variable names used by gather_residuals.

Value

A data frame. add_prediction adds a single new column, .pred, to the input data. spread_residuals adds one column for each model. gather_prections adds two columns .model and .pred, and repeats the input rows for each model.

Examples

Run this code
# NOT RUN {
df <- tibble::data_frame(
  x = sort(runif(100)),
  y = 5 * x + 0.5 * x ^ 2 + 3 + rnorm(length(x))
)
plot(df)

m1 <- lm(y ~ x, data = df)
df %>% add_residuals(m1)

m2 <- lm(y ~ poly(x, 2), data = df)
df %>% spread_residuals(m1, m2)
df %>% gather_residuals(m1, m2)
# }

Run the code above in your browser using DataLab