Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@brshallo
Created April 13, 2021 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brshallo/e1426d745f301fc683a777555736d1be to your computer and use it in GitHub Desktop.
Save brshallo/e1426d745f301fc683a777555736d1be to your computer and use it in GitHub Desktop.
Useful for undoing transformation applied to predictions. See tidymodels/recipes#264 (https://github.com/tidymodels/recipes/issues/264) for discussion, though was closed without solution.
library(tidymodels)

rec_prep <- recipe(cty ~ ., data = mpg) %>% 
  step_YeoJohnson(cty) %>% 
  prep(data = mpg)

yj_estimate <- rec_prep %>% 
  tidy(1) %>% 
  pluck("value", 1)

rec_prep %>% 
  juice() %>% 
  mutate(cty_original = VGAM::yeo.johnson(cty, lambda = yj_estimate, inverse = TRUE)) %>% 
  select(contains('cty'))
#> # A tibble: 234 x 2
#>      cty cty_original
#>    <dbl>        <dbl>
#>  1  2.84         18. 
#>  2  2.98         21. 
#>  3  2.93         20.0
#>  4  2.98         21. 
#>  5  2.74         16.0
#>  6  2.84         18. 
#>  7  2.84         18. 
#>  8  2.84         18. 
#>  9  2.74         16.0
#> 10  2.93         20.0
#> # ... with 224 more rows

Created on 2021-04-13 by the reprex package (v1.0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment