Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

control outcome transformations #8

Open
topepo opened this issue Dec 18, 2024 · 1 comment
Open

control outcome transformations #8

topepo opened this issue Dec 18, 2024 · 1 comment

Comments

@topepo
Copy link
Member

topepo commented Dec 18, 2024

The extract_* functions do different things when there is an outcome transformation in a formula:

library(tidymodels)
library(important)
tidymodels_prefer()
theme_set(theme_bw())
options(pillar.advice = FALSE, pillar.min_title_chars = Inf)
# In-line function in preprocessor formula

ames <- ames %>% select(Sale_Price, Lot_Area, Neighborhood)

lm_wflow <- workflow(log(Sale_Price) ~ . , linear_reg())
lm_fit <- fit(lm_wflow, ames)

# Different variable names and units
important:::extract_data_original(lm_fit, head(ames)) %>% 
    select(contains("Sale"))
#> # A tibble: 6 × 1
#>   Sale_Price
#>        <int>
#> 1     215000
#> 2     105000
#> 3     172000
#> 4     244000
#> 5     189900
#> 6     195500
important:::extract_data_derived(lm_fit, head(ames)) %>% 
    select(contains("Sale"))
#> # A tibble: 6 × 1
#>   `log(Sale_Price)`
#>               <dbl>
#> 1              12.3
#> 2              11.6
#> 3              12.1
#> 4              12.4
#> 5              12.2
#> 6              12.2
# In-line function in extra formula from add_model()

lm_form_wflow <- 
    workflow() %>% 
    add_formula(Sale_Price~ . ) %>% 
    add_model(linear_reg(), formula = log(Sale_Price) ~ .)

lm_form_fit <- fit(lm_form_wflow, ames)

# Same variable names and units
important:::extract_data_original(lm_form_fit, head(ames)) %>% 
    select(contains("Sale"))
#> # A tibble: 6 × 1
#>   Sale_Price
#>        <int>
#> 1     215000
#> 2     105000
#> 3     172000
#> 4     244000
#> 5     189900
#> 6     195500
important:::extract_data_derived(lm_form_fit, head(ames)) %>% 
    select(contains("Sale"))
#> # A tibble: 6 × 1
#>   Sale_Price
#>        <int>
#> 1     215000
#> 2     105000
#> 3     172000
#> 4     244000
#> 5     189900
#> 6     195500

This may be intended/helpful in some cases but definitely not in others. We should perhaps make an option for this.

@topepo
Copy link
Member Author

topepo commented Dec 18, 2024

library(tidymodels)
library(important)
tidymodels_prefer()
theme_set(theme_bw())
options(pillar.advice = FALSE, pillar.min_title_chars = Inf)
# In-line function in preprocessor formula

ames <- ames %>% select(Sale_Price, Lot_Area, Neighborhood)

lm_wflow <- workflow(log(Sale_Price) ~ . , linear_reg())
lm_fit <- fit(lm_wflow, ames)

# Maybe detect when a formula preprocessor is used, look for transformations, 
# then return just the column name when this happens. 

lm_fit |> 
    hardhat::extract_mold() |> 
    purrr::pluck("blueprint") %>% 
    pluck("formula") %>% 
    rlang::f_lhs() %>% 
    all.vars()
#> [1] "Sale_Price"

Created on 2024-12-18 with reprex v2.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant