R/recipe_scale_center.R

Defines functions recipe_scale_center

Documented in recipe_scale_center

#' Create a recipe to preprocess data
#'
#' Creates a recipe by scaling and centering the given data,
#' and creates a model using the given formula.
#'
#' @param dataframe A data frame or a tibble of the data.
#' @param formula The formula used to create the model (eg: target ~ predictor).
#'
#' @return A tidymodel recipe, specifying inputs, outputs, and steps
#' (scaling and centering) applied to all predictors.
#'
#' @export
#'
#' @examples
#' recipe_scale_center(mtcars, mpg ~ hp)
#' recipe_scale_center(mtcars, mpg ~ hp + cyl)
#' recipe_scale_center(mtcars, mpg ~ .)
recipe_scale_center <- function(dataframe, formula) {
  if (!is.data.frame(dataframe)) {
    stop("`recipe_scale_center` expects a data frame")
  }

  if (class(formula) != "formula") {
    stop("`recipe_scale_center` expects a formula in the form
         `target ~ predictor(s)")
  }

  if (ncol(dataframe) == 0) {
    stop("`recipe_scale_center` expects a non-empty data frame")
  }

  recipes::recipe(formula, data = dataframe) |>
    recipes::step_center(recipes::all_predictors()) |>
    recipes::step_scale(recipes::all_predictors())
}
DSCI-310/DSCI-310-Group-11-package documentation built on April 9, 2022, 12:32 a.m.