Skip to content

augmenting regression models should add the residual if the outcome is available #131

Closed
@topepo

Description

@topepo

Other augment() methods will add a residual column.

library(tidymodels)
#> Registered S3 method overwritten by 'tune':
#>   method                   from   
#>   required_pkgs.model_spec parsnip
tidymodels_prefer()

set.seed(4325)
data_split <- initial_split(mpg)
data_train <- training(data_split)
data_test  <- testing(data_split)

milage_recipe <- 
  recipe(hwy ~ manufacturer + year, data = data_train) %>%
  step_other(manufacturer, threshold = 0.1) %>%
  step_dummy(manufacturer) %>%
  step_interact(~ year:starts_with("manufacturer"))

wflow <- 
  workflow() %>%
  add_model(linear_reg() %>% set_engine("lm")) %>%
  add_recipe(milage_recipe)

wflow %>%
  fit(data_train) %>%
  augment(data_train)
#> # A tibble: 175 × 12
#>    manufacturer model    displ  year   cyl trans  drv     cty   hwy fl    class 
#>    <chr>        <chr>    <dbl> <int> <int> <chr>  <chr> <int> <int> <chr> <chr> 
#>  1 dodge        ram 150…   4.7  2008     8 auto(… 4        13    17 r     pickup
#>  2 toyota       corolla    1.8  2008     4 auto(… f        26    35 r     compa…
#>  3 nissan       altima     2.4  1999     4 manua… f        21    29 r     compa…
#>  4 volkswagen   passat     2    2008     4 manua… f        21    29 p     midsi…
#>  5 toyota       4runner…   3.4  1999     6 auto(… 4        15    19 r     suv   
#>  6 ford         mustang    4.6  1999     8 auto(… r        15    21 r     subco…
#>  7 volkswagen   passat     2    2008     4 auto(… f        19    28 p     midsi…
#>  8 toyota       toyota …   2.7  2008     4 manua… 4        17    22 r     pickup
#>  9 volkswagen   passat     2.8  1999     6 auto(… f        16    26 p     midsi…
#> 10 volkswagen   passat     1.8  1999     4 manua… f        21    29 p     midsi…
#> # … with 165 more rows, and 1 more variable: .pred <dbl>

Created on 2021-12-18 by the reprex package (v2.0.1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions