Home > other >  How to specify response using recipe package in Tidymodels
How to specify response using recipe package in Tidymodels

Time:10-26

I am creating a recipe so that I first create a calculated column called "response" as so:

rec <- recipe( ~., data = training) %>%
  step_mutate(response = as.integer(all(c('A', 'B') %in% Col4) & Col4 == 'A'))

I would like to now specify this new calculated column as the response variable in the recipe() function as shown below. I will be doing a series of operations on it such as this first one with step_naomit. How do I re-specify my response in recipe() to be the calculated column from my previous step (above) using recipes?

recipe <- recipe(response ~ ., data = training) %>%
          step_naomit(recipe, response)

CodePudding user response:

You can set the role for new columns in the step_mutate() function by explictly setting the role= parmaeter.

rec <- recipe( ~., data = iris) %>%
  step_mutate(SepalSquared= Sepal.Length ^ 2, role="outcome")

Then check that it worked with summary(prep(rec))

  variable     type    role      source  
  <chr>        <chr>   <chr>     <chr>   
1 Sepal.Length numeric predictor original
2 Sepal.Width  numeric predictor original
3 Petal.Length numeric predictor original
4 Petal.Width  numeric predictor original
5 Species      nominal predictor original
6 SepalSquared numeric outcome   derived 
  • Related