Learn R Programming

timetk (version 2.6.1)

step_box_cox: Box-Cox Transformation using Forecast Methods

Description

step_box_cox creates a specification of a recipe step that will transform data using a Box-Cox transformation. This function differs from recipes::step_BoxCox by adding multiple methods including Guerrero lambda optimization and handling for negative data used in the Forecast R Package.

Usage

step_box_cox(
  recipe,
  ...,
  method = c("guerrero", "loglik"),
  limits = c(-1, 2),
  role = NA,
  trained = FALSE,
  lambdas_trained = NULL,
  skip = FALSE,
  id = rand_id("box_cox")
)

# S3 method for step_box_cox tidy(x, ...)

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 which variables are affected by the step. See selections() for more details. For the tidy method, these are not currently used.

method

One of "guerrero" or "loglik"

limits

A length 2 numeric vector defining the range to compute the transformation parameter lambda.

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.

lambdas_trained

A numeric vector of transformation values. This is NULL until computed by prep().

skip

A logical. Should the step be skipped when the recipe is baked by bake.recipe()? While all operations are baked when prep.recipe() 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.

x

A step_box_cox object.

Value

An updated version of recipe with the new step added to the sequence of existing steps (if any). For the tidy method, a tibble with columns terms (the selectors or variables selected) and value (the lambda estimate).

Details

The step_box_cox() function is designed specifically to handle time series using methods implemented in the Forecast R Package.

Negative Data

This function can be applied to Negative Data.

Lambda Optimization Methods

This function uses 2 methods for optimizing the lambda selection from the Forecast R Package:

  1. method = "guerrero": Guerrero's (1993) method is used, where lambda minimizes the coefficient of variation for subseries of x.

  2. method = loglik: the value of lambda is chosen to maximize the profile log likelihood of a linear model fitted to x. For non-seasonal data, a linear time trend is fitted while for seasonal data, a linear time trend with seasonal dummy variables is used.

References

  1. Guerrero, V.M. (1993) Time-series analysis supported by power transformations. Journal of Forecasting, 12, 37<U+2013>48.

  2. Box, G. E. P. and Cox, D. R. (1964) An analysis of transformations. JRSS B 26 211<U+2013>246.

See Also

Time Series Analysis:

Transformations to reduce variance:

  • recipes::step_log() - Log transformation

  • recipes::step_sqrt() - Square-Root Power Transformation

Recipe Setup and Application:

  • recipes::recipe()

  • recipes::prep()

  • recipes::bake()

Examples

Run this code
# NOT RUN {
library(tidyverse)
library(tidyquant)
library(recipes)
library(timetk)

FANG_wide <- FANG %>%
select(symbol, date, adjusted) %>%
    pivot_wider(names_from = symbol, values_from = adjusted)

recipe_box_cox <- recipe(~ ., data = FANG_wide) %>%
    step_box_cox(FB, AMZN, NFLX, GOOG) %>%
    prep()

recipe_box_cox %>% bake(FANG_wide)

recipe_box_cox %>% tidy(1)

# }

Run the code above in your browser using DataLab