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 singlemodel
; the output column will be calledresid
- ...
gather_residuals
andspread_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_residuals
adds a single new column,
.resid
, to the input data
. spread_residuals
adds
one column for each model. gather_predictions
adds two columns
.model
and .resid
, and repeats the input rows for
each model.
Examples
df <- tibble::tibble(
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)
#> # A tibble: 100 × 3
#> x y resid
#> <dbl> <dbl> <dbl>
#> 1 0.0130 2.05 -0.636
#> 2 0.0248 4.16 1.40
#> 3 0.0256 3.72 0.961
#> 4 0.0267 1.98 -0.788
#> 5 0.0312 3.60 0.804
#> 6 0.0356 4.77 1.95
#> 7 0.0383 2.25 -0.580
#> 8 0.0397 1.16 -1.68
#> 9 0.0573 2.34 -0.607
#> 10 0.0672 1.99 -1.02
#> # … with 90 more rows
m2 <- lm(y ~ poly(x, 2), data = df)
df %>% spread_residuals(m1, m2)
#> # A tibble: 100 × 4
#> x y m1 m2
#> <dbl> <dbl> <dbl> <dbl>
#> 1 0.0130 2.05 -0.636 -0.816
#> 2 0.0248 4.16 1.40 1.24
#> 3 0.0256 3.72 0.961 0.796
#> 4 0.0267 1.98 -0.788 -0.951
#> 5 0.0312 3.60 0.804 0.646
#> 6 0.0356 4.77 1.95 1.80
#> 7 0.0383 2.25 -0.580 -0.729
#> 8 0.0397 1.16 -1.68 -1.83
#> 9 0.0573 2.34 -0.607 -0.733
#> 10 0.0672 1.99 -1.02 -1.14
#> # … with 90 more rows
df %>% gather_residuals(m1, m2)
#> # A tibble: 200 × 4
#> model x y resid
#> <chr> <dbl> <dbl> <dbl>
#> 1 m1 0.0130 2.05 -0.636
#> 2 m1 0.0248 4.16 1.40
#> 3 m1 0.0256 3.72 0.961
#> 4 m1 0.0267 1.98 -0.788
#> 5 m1 0.0312 3.60 0.804
#> 6 m1 0.0356 4.77 1.95
#> 7 m1 0.0383 2.25 -0.580
#> 8 m1 0.0397 1.16 -1.68
#> 9 m1 0.0573 2.34 -0.607
#> 10 m1 0.0672 1.99 -1.02
#> # … with 190 more rows