function to add saturation trasnformations to a recipe

step_saturation(
  recipe,
  ...,
  role = NA,
  trained = FALSE,
  options = list(names = TRUE),
  skip = FALSE,
  asymptote = 100,
  saturation_speed = 0.05,
  hills = NULL,
  id = rand_id("saturation")
)

Arguments

recipe

a recipes::recipe

...

variables in recipe to be effected by the step

role

defaults to NA, set to a string to change the role of the transformed variables

asymptote

defaults to 100, the asymptotic value of the saturation transformed variable

saturation_speed

defaults to .05, controls how quickly the asymptote is approached

id

default value will work most often, but can be set manually for use with tuning fuctions

Value

a recipe with the new saturation step appended

Examples

#not run!
suppressMessages(suppressWarnings(library(tidyverse)))
suppressMessages(suppressWarnings(library(recipes)))
suppressMessages(suppressWarnings(library(tune)))
suppressMessages(suppressWarnings(library(dials)))
suppressMessages(suppressWarnings(library(mostlytidyMMM)))

#create two datasets
mktdata<-rbind(tibble(prod='brand',store='store1',
                      sales=c(100,100,100,100,100),tv=c(10,100,0,0,100),
                      search=c(0,10,20,50,50)) ,
               tibble(prod='brand',store='store2',
                      sales=c(10,10,10,10,10),tv=c(0,0,0,0,0),
                      search=c(0,2,2,0,0) ) ) |> 
  group_by(prod,store)

mktdata2<-tibble(prod='brand',store='all',
                 sales=100,tv=1000,search=1000) |> group_by(prod,store)

#creating a recipe with a saturaiton step in it
rec_obj <-
 recipe(sales ~ ., data = mktdata) |>
 step_saturation(c(tv,search),asymptote=200,saturation_speed=.003 ) |>
 prep(training = mktdata)

#showing what a tunable recipe looks liek:
rec_obj2<-  recipe(sales ~ ., data = mktdata) |>
  step_saturation(c(tv,search),asymptote=tune(),saturation_speed=tune() )

extract_parameter_set_dials(rec_obj2)
#> Collection of 2 parameters for tuning
#> 
#>        identifier             type    object
#>         asymptote        asymptote nparam[+]
#>  saturation_speed saturation_speed nparam[+]
#> 

 #custom print shows values of saturation hyperparameters:
 print(rec_obj)
#> Recipe
#> 
#> Inputs:
#> 
#>       role #variables
#>    outcome          1
#>  predictor          4
#> 
#> Training data contained 10 data points and no missing data.
#> 
#> Operations:
#> 
#> Saturation (asymptote= 200 saturation_speed= 0.003 Transformation ontv, search [trained]
 #or shows that they are set to be tuned:
 print(rec_obj2)
#> Recipe
#> 
#> Inputs:
#> 
#>       role #variables
#>    outcome          1
#>  predictor          4
#> 
#> Operations:
#> 
#> Saturation (asymptote= tune saturation_speed= tune Transformation onc(tv, search)
 #producing the transformed dataset:
 bake(rec_obj,mktdata)
#> # A tibble: 10 x 5
#> # Groups:   prod, store [2]
#>    prod  store     tv search sales
#>    <fct> <fct>  <dbl>  <dbl> <dbl>
#>  1 brand store1  5.91   0      100
#>  2 brand store1 51.8    5.91   100
#>  3 brand store1  0     11.6    100
#>  4 brand store1  0     27.9    100
#>  5 brand store1 51.8   27.9    100
#>  6 brand store2  0      0       10
#>  7 brand store2  0      1.20    10
#>  8 brand store2  0      1.20    10
#>  9 brand store2  0      0       10
#> 10 brand store2  0      0       10