Skip to contents

step_date_after() creates a specification of a recipe step that will create new columns indicating the time after an recurrent event.

Usage

step_date_after(
  recipe,
  ...,
  role = "predictor",
  trained = FALSE,
  rules = list(),
  transform = "identity",
  columns = NULL,
  skip = FALSE,
  id = rand_id("date_after")
)

Arguments

recipe

A recipe object. The step will be added to the sequence of operations for this recipe.

...

One or more selector functions to choose variables for this step. See selections() for more details.

role

Not used by this step since no new variables are created.

trained

A logical to indicate if the quantities for preprocessing have been estimated.

rules

Named list of almanac rules.

transform

A function or character indication a function used oon the resulting variables. See details for allowed names and their functions.

columns

A character string of variables that will be used as inputs. This field is a placeholder and will be populated once recipes::prep.recipe() is used.

skip

A logical. Should the step be skipped when the recipe is baked by bake()? While all operations are baked when prep() is run, some operations may not be able to be conducted on new data (e.g. processing the outcome variable(s)). Care should be taken when using skip = TRUE as it may affect the computations for subsequent operations.

id

A character string that is unique to this step to identify it.

Value

An updated version of recipe with the new check added to the sequence of any existing operations.

Details

The transform argument can be function that takes a numeric vector and returns a numeric vector of the same length. It can also be a character vector, below is the supported vector names. Some functions come with offset to avoid Inf.

"identity"
function(x) x

"inverse"
function(x) 1 / (x + 0.5)

"exp"
function(x) exp(x)

"log"
function(x) log(x + 0.5)

The effect of transform is illustrated below.

The naming of the resulting variables will be on the form

{variable name}_after_{name of rule}

Examples

library(recipes)
library(extrasteps)
library(almanac)
library(modeldata)

data(Chicago)

on_easter <- yearly() %>% recur_on_easter()
on_weekend <- weekly() %>% recur_on_weekends()

rules <- list(easter = on_easter, weekend = on_weekend)

rec_spec <- recipe(ridership ~ date, data = Chicago) %>%
  step_date_after(date, rules = rules)

rec_spec_preped <- prep(rec_spec)

bake(rec_spec_preped, new_data = NULL)
#> # A tibble: 5,698 × 3
#>    ridership date_after_easter date_after_weekend
#>        <dbl>             <int>              <int>
#>  1     15.7                274                  1
#>  2     15.8                275                  2
#>  3     15.9                276                  3
#>  4     15.9                277                  4
#>  5     15.4                278                  5
#>  6      2.42               279                  0
#>  7      1.47               280                  0
#>  8     15.5                281                  1
#>  9     15.9                282                  2
#> 10     15.9                283                  3
#> # ℹ 5,688 more rows