Skip to content

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 resid

...

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_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.0144  3.43  0.452 
#>  2 0.0222  2.56 -0.465 
#>  3 0.0277  3.97  0.922 
#>  4 0.0288  3.30  0.240 
#>  5 0.0317  3.40  0.331 
#>  6 0.0748  4.05  0.740 
#>  7 0.0755  3.21 -0.0949
#>  8 0.0859  4.33  0.960 
#>  9 0.0861  1.93 -1.44  
#> 10 0.109   4.06  0.565 
#> # ℹ 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.0144  3.43  0.452   0.156 
#>  2 0.0222  2.56 -0.465  -0.746 
#>  3 0.0277  3.97  0.922   0.652 
#>  4 0.0288  3.30  0.240  -0.0275
#>  5 0.0317  3.40  0.331   0.0687
#>  6 0.0748  4.05  0.740   0.557 
#>  7 0.0755  3.21 -0.0949 -0.277 
#>  8 0.0859  4.33  0.960   0.796 
#>  9 0.0861  1.93 -1.44   -1.60  
#> 10 0.109   4.06  0.565   0.439 
#> # ℹ 90 more rows
df %>% gather_residuals(m1, m2)
#> # A tibble: 200 × 4
#>    model      x     y   resid
#>    <chr>  <dbl> <dbl>   <dbl>
#>  1 m1    0.0144  3.43  0.452 
#>  2 m1    0.0222  2.56 -0.465 
#>  3 m1    0.0277  3.97  0.922 
#>  4 m1    0.0288  3.30  0.240 
#>  5 m1    0.0317  3.40  0.331 
#>  6 m1    0.0748  4.05  0.740 
#>  7 m1    0.0755  3.21 -0.0949
#>  8 m1    0.0859  4.33  0.960 
#>  9 m1    0.0861  1.93 -1.44  
#> 10 m1    0.109   4.06  0.565 
#> # ℹ 190 more rows