Skip to contents

This is mainly a wrapper for the BoxCox transformation from the forecast R package. The box_cox_vec() function performs the transformation. The box_cox_inv_vec() inverts the transformation. The auto_lambda() helps in selecting the optimal lambda value.

Usage

box_cox_vec(x, lambda = "auto", silent = FALSE)

box_cox_inv_vec(x, lambda)

auto_lambda(
  x,
  method = c("guerrero", "loglik"),
  lambda_lower = -1,
  lambda_upper = 2
)

Arguments

x

A numeric vector.

lambda

The box cox transformation parameter. If set to "auto", performs automated lambda selection using auto_lambda().

silent

Whether or not to report the automated lambda selection as a message.

method

The method used for automatic lambda selection. Either "guerrero" or "loglik".

lambda_lower

A lower limit for automatic lambda selection

lambda_upper

An upper limit for automatic lambda selection

Details

The Box Cox transformation is a power transformation that is commonly used to reduce variance of a time series.

Automatic Lambda Selection

If desired, the lambda argument can be selected using auto_lambda(), a wrapper for the Forecast R Package's forecast::BoxCox.lambda() function. Use either of 2 methods:

  1. "guerrero" - Minimizes the non-seasonal variance

  2. "loglik" - Maximizes the log-likelihood of a linear model fit to x

References

See also

Other common transformations to reduce variance: log(), log1p() and sqrt()

Examples

library(dplyr)
library(timetk)

d10_daily <- m4_daily %>% filter(id == "D10")

# --- VECTOR ----

value_bc <- box_cox_vec(d10_daily$value)
#> box_cox_vec(): Using value for lambda: 1.25119350454964
value    <- box_cox_inv_vec(value_bc, lambda = 1.25119350454964)

# --- MUTATE ----

m4_daily %>%
    group_by(id) %>%
    mutate(value_bc = box_cox_vec(value))
#> box_cox_vec(): Using value for lambda: 1.25119350454964
#> box_cox_vec(): Using value for lambda: 0.0882021886505848
#> box_cox_vec(): Using value for lambda: 1.99992424816297
#> box_cox_vec(): Using value for lambda: 0.401716085353735
#> # A tibble: 9,743 × 4
#> # Groups:   id [4]
#>    id    date       value value_bc
#>    <fct> <date>     <dbl>    <dbl>
#>  1 D10   2014-07-03 2076.   11303.
#>  2 D10   2014-07-04 2073.   11284.
#>  3 D10   2014-07-05 2049.   11116.
#>  4 D10   2014-07-06 2049.   11117.
#>  5 D10   2014-07-07 2006.   10829.
#>  6 D10   2014-07-08 2018.   10905.
#>  7 D10   2014-07-09 2019.   10915.
#>  8 D10   2014-07-10 2007.   10836.
#>  9 D10   2014-07-11 2010    10854.
#> 10 D10   2014-07-12 2002.   10796.
#> # … with 9,733 more rows
#> # ℹ Use `print(n = ...)` to see more rows