step_date_before: Time before Recurrent Date Time Event

View source: R/date_before.R

step_date_beforeR Documentation

Time before Recurrent Date Time Event

Description

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

Usage

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

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.

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.

date_before-1.pngdate_before-2.pngdate_before-3.pngdate_before-4.png

The naming of the resulting variables will be on the form

{variable name}_before_{name of rule}

Value

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

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_before(date, rules = rules)

rec_spec_preped <- prep(rec_spec)

bake(rec_spec_preped, new_data = NULL)

extrasteps documentation built on Oct. 4, 2024, 1:07 a.m.