# Creating Custom Step Functions

`recipes` contains a number of different steps included in the package:

    library(recipes)
    steps <- apropos("^step_")
    steps[!grepl("new$", steps)]
    #>  [1] "step_BoxCox"       "step_YeoJohnson"   "step_bagimpute"
    #>  [4] "step_bin2factor"   "step_center"       "step_classdist"
    #>  [7] "step_corr"         "step_date"         "step_depth"
    #> [10] "step_discretize"   "step_dummy"        "step_holiday"
    #> [13] "step_hyperbolic"   "step_ica"          "step_interact"
    #> [16] "step_intercept"    "step_invlogit"     "step_isomap"
    #> [19] "step_knnimpute"    "step_kpca"         "step_lincomb"
    #> [22] "step_log"          "step_logit"        "step_meanimpute"
    #> [25] "step_modeimpute"   "step_ns"           "step_nzv"
    #> [28] "step_ordinalscore" "step_other"        "step_pca"
    #> [31] "step_poly"         "step_range"        "step_ratio"
    #> [34] "step_regex"        "step_rm"           "step_scale"
    #> [37] "step_shuffle"      "step_spatialsign"  "step_sqrt"
    #> [40] "step_window"

You might want to make your own and this page describes how to do that. If you are looking for good examples of existing steps, I would suggest looking at the code for centering or PCA to start.

# A new step definition

At an example, let’s create a step that replaces the value of a variable with its percentile from the training set. The date that I’ll use is from the `recipes` package:

    data(biomass)
    str(biomass)
    #> 'data.frame':    536 obs. of  8 variables:
    #>  $ sample  : chr  "Akhrot Shell" "Alabama Oak Wood Waste" "Alder" "Alfalfa" ...
    #>  $ dataset : chr  "Training" "Training" "Training" "Training" ...
    #>  $ carbon  : num  49.8 49.5 47.8 45.1 46.8 ...
    #>  $ hydrogen: num  5.64 5.7 5.8 4.97 5.4 5.75 5.99 5.7 5.5 5.9 ...
    #>  $ oxygen  : num  42.9 41.3 46.2 35.6 40.7 ...
    #>  $ nitrogen: num  0.41 0.2 0.11 3.3 1 2.04 2.68 1.7 0.8 1.2 ...
    #>  $ sulfur  : num  0 0 0.02 0.16 0.02 0.1 0.2 0.2 0 0.1 ...
    #>  $ HHV     : num  20 19.2 18.3 18.2 18.4 ...

    biomass_tr <- biomass[biomass$dataset == "Training",]
    biomass_te <- biomass[biomass$dataset == "Testing",]

To illustrate the transformation with the `carbon` variable, the training set distribution of that variables is shown below with a vertical line for the first value of the test set.

    library(ggplot2)
    theme_set(theme_bw())
    ggplot(biomass_tr, aes(x = carbon)) +
      geom_histogram(binwidth = 5, col = "blue", fill = "blue", alpha = .5) +
      geom_vline(xintercept = biomass_te$carbon[1], lty = 2)

Based on the training set, 42.1% of the data are less than a value of 46.35. There are some applications where it might be advantageous to represent the predictor values are percentiles rather than their original values.

Our new step will do this computation for any numeric variables of interest. We will call this `step_percentile`. The code below is designed for illustration and not speed or best practices. I’ve left out a lot of error trapping that we would want in a real implementation.

# Create the initial function.

The user-exposed function `step_percentile` is just a simple wrapper around an internal function called `add_step`. This function takes the same arguments as your function and simply adds it to a new recipe. The `...` signfies the variable selectors that can be used.

    step_percentile <- function(recipe, ..., role = NA,
                                trained = FALSE, ref_dist = NULL,
                                approx = FALSE,
                                options = list(probs = (0:100)/100, names = TRUE)) {
    ## bake but do not evaluate the variable selectors with
    ## the `quos` function in `rlang`
      terms <- rlang::quos(...)
      if(length(terms) == 0)
        stop("Please supply at least one variable specification. See ?selections.")
      add_step(
        recipe,
        step_percentile_new(
          terms = terms,
          trained = trained,
          role = role,
          ref_dist = ref_dist,
          approx = approx,
          options = options))
    }

You should always keep the first four arguments (`recipe` though `trained`) the same as listed above. Some notes:

- the `role` argument is used when you either 1) create new variables and want their role to be pre-set or 2) replace the existing variables with new values. The latter is what we will be doing and using `role = NA` will leave the existing role intact.
- `trained` is set by the package when the estimation step has been run. You should default your function definition’s argument to `FALSE`.

I’ve added extra arguments specific to this step. In order to calculate the percentile, the training data for the relevant columns will need to be saved. This data will be saved in the `ref_dist` object. However, this might be problematic if the data set is large. `approx` would be used when you want to save a grid of pre-computed percentiles from the training set and use these to estimate the percentile for a new data point. If `approx = TRUE`, the argument `ref_dist` will contain the grid for each variable.

We will use the `stats::quantile` to compute the grid. However, we might also want to have control over the granularity of this grid, so the `options` argument will be used to define how that calculations is done. We could just use the ellipses (aka `...`) so that any options passed to `step_percentile` that are not one of its arguments will then be passed to `stats::quantile`. We recommend making a seperate list object with the options and use these inside the function.

# Initialization of new objects

Next, you can utilize the internal function `step` that sets the class of new objects. Using `subclass = "percentile"` will set the class of new objects to `“step_percentile”.

    step_percentile_new <- function(terms = NULL, role = NA, trained = FALSE,
                                    ref_dist = NULL, approx = NULL, options = NULL) {
      step(
        subclass = "percentile",
        terms = terms,
        role = role,
        trained = trained,
        ref_dist = ref_dist,
        approx = approx,
        options = options
      )
    }

# Define the estimation procedure

You will need to create a new `prep` method for your step’s class. To do this, three arguments that the method should have:

    function(x, training, info = NULL)

where

- `x` will be the `step_percentile` object
- `training` will be a tibble that has the training set data
- `info` will also be a tibble that has information on the current set of data available. This information is updated as each step is evaluated by its specific `prep` method so it may not have the variables from the original data. The columns in this tibble are `variable` (the variable name), `type` (currently either “numeric” or “nominal”), `role` (defining the variable’s role), and `source` (either “original” or “derived” depending on where it originated).

You can define other options.

The first thing that you might want to do in the `prep` function is to translate the specification listed in the `terms` argument to column names in the current data. There is an internal function called `terms_select` that can be used to obtain this.

    prep.step_percentile <- function(x, training, info = NULL, ...) {
      col_names <- terms_select(terms = x$terms, info = info)
    }

Once we have this, we can either save the original data columns or estimate the approximation grid. For the grid, we will use a helper function that enables us to run `do.call` on a list of arguments that include the `options` list.

    get_pctl <- function(x, args) {
      args$x <- x
      do.call("quantile", args)
    }

    prep.step_percentile <- function(x, training, info = NULL, ...) {
      col_names <- terms_select(terms = x$terms, info = info)
      ## You can add error trapping for non-numeric data here and so on.
      ## We'll use the names later so
      if(x$options$names == FALSE)
        stop("`names` should be set to TRUE")

      if(!x$approx) {
        x$ref_dist <- training[, col_names]
      } else {
        pctl <- lapply(
          training[, col_names],
          get_pctl,
          args = x$options
        )
        x$ref_dist <- pctl
      }
      ## Always return the updated step
      x
    }

# Create the `bake` method

Remember that the `prep` function does not apply the step to the data; it only estimates any required values such as `ref_dist`. We will need to create a new method for our `step_percentile` class. The minimum arguments for this are

    function(object, newdata, ...)

where `object` is the updated step function that has been through the corresponding `prep` code and `newdata` is a tibble of data to be preprocessingcessed.

Here is the code to convert the new data to percentiles. Two initial helper functions handle the two cases (approximation or not). We always return a tibble as the output.

    ## Two helper functions
    pctl_by_mean <- function(x, ref) mean(ref <= x)

    pctl_by_approx <- function(x, ref) {
      ## go from 1 column tibble to vector
      x <- getElement(x, names(x))
      ## get the percentiles values from the names (e.g. "10%")
      p_grid <- as.numeric(gsub("%$", "", names(ref)))
      approx(x = ref, y = p_grid, xout = x)$y/100
    }

    bake.step_percentile <- function(object, newdata, ...) {
      require(tibble)
      ## For illustration (and not speed), we will loop through the affected variables
      ## and do the computations
      vars <- names(object$ref_dist)

      for(i in vars) {
        if(!object$approx) {
          ## We can use `apply` since tibbles do not drop dimensions:
          newdata[, i] <- apply(newdata[, i], 1, pctl_by_mean,
                                ref = object$ref_dist[, i])
        } else
          newdata[, i] <- pctl_by_approx(newdata[, i], object$ref_dist[[i]])
      }
      ## Always convert to tibbles on the way out
      as_tibble(newdata)
    }

# Running the example

Let’s use the example data to make sure that it works:

    rec_obj <- recipe(HHV ~ ., data = biomass_tr[, -(1:2)])
    rec_obj <- rec_obj %>%
      step_percentile(all_predictors(), approx = TRUE)

    rec_obj <- prep(rec_obj, training = biomass_tr)
    #> step 1 percentile training

    percentiles <- bake(rec_obj, biomass_te)
    percentiles
    #> # A tibble: 80 x 5
    #>    carbon hydrogen oxygen nitrogen sulfur
    #>     <dbl>    <dbl>  <dbl>    <dbl>  <dbl>
    #>  1 0.4209   0.4500 0.9026    0.215  0.735
    #>  2 0.1800   0.3850 0.9217    0.928  0.839
    #>  3 0.1561   0.3850 0.9447    0.900  0.805
    #>  4 0.4233   0.7750 0.2800    0.845  0.902
    #>  5 0.6662   0.8667 0.6314    0.155  0.090
    #>  6 0.2175   0.3850 0.5363    0.495  0.700
    #>  7 0.0803   0.2713 0.9859    0.695  0.903
    #>  8 0.1395   0.1260 0.1604    0.606  0.700
    #>  9 0.0226   0.1035 0.1312    0.126  0.996
    #> 10 0.0178   0.0821 0.0987    0.972  0.974
    #> # ... with 70 more rows

The plot below shows how the original data line up with the percentiles for each split of the data for one of the predictors: