Box-Cox Transform the Target Variable
Source:R/PipeOpTargetTrafoBoxCox.R
mlr_pipeops_fcst.targetboxcox.RdApplies a Box-Cox transformation to the target variable to stabilize the variance, producing the new target
BoxCox(y, lambda). The transformation is pointwise and monotonic, so no rows are dropped and predictions are
inverted via forecast::InvBoxCox(). lambda = 0 is the log transformation. When lambda is NULL (default) it
is estimated from the training data, per series on keyed tasks. Predicting a series not seen during training is an
error.
Box-Cox and log transformations require strictly positive target values. Non-positive values produce NaN or an
error. A negative estimated lambda can make forecast::InvBoxCox() return NA for upper quantiles. Set
lower = 0 to avoid this.
Parameters
The parameters are the parameters inherited from mlr3pipelines::PipeOpTargetTrafo, as well as the following:
lambda::numeric(1)|NULL
Box-Cox transformation parameter.NULL(default) estimates it from the training data,0is the log transformation, any other numeric is used as a fixed value.method::character(1)
Method used to estimatelambdawhenlambda = NULL, one of"guerrero"(default) or"loglik". Seeforecast::BoxCox.lambda().lower::numeric(1)
Lower bound for the estimatedlambda. Default-1.upper::numeric(1)
Upper bound for the estimatedlambda. Default2.
Limitations
This PipeOp must not be placed inside a RecursiveForecaster or DirectForecaster graph and is rejected at
construction. Use it inside a plain mlr3pipelines::GraphLearner via ppl("targettrafo", ...), or wrap the
forecaster itself with ppl("targettrafo", ...) so all horizons are inverted together.
Super classes
mlr3pipelines::PipeOp -> mlr3pipelines::PipeOpTargetTrafo -> PipeOpTargetTrafoBoxCox
Methods
PipeOpTargetTrafoBoxCox$new()
Initializes a new instance of this Class.
Usage
PipeOpTargetTrafoBoxCox$new(id = "fcst.targetboxcox", param_vals = list())Examples
# \donttest{
library(mlr3pipelines)
task = tsk("airpassengers")
split = partition(task, ratio = 0.8)
flrn = as_learner(ppl("targettrafo",
graph = DirectForecaster$new(lrn("regr.rpart"), lags = 1:3, horizons = length(split$test)),
trafo_pipeop = po("fcst.targetboxcox")
))
flrn$train(task, split$train)
flrn$predict(task, split$test)
#>
#> ── <PredictionFcst> for 29 observations: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
#> month row_ids truth response
#> 1958-08-01 116 505 409.4023
#> 1958-09-01 117 404 360.7524
#> 1958-10-01 118 359 303.4957
#> --- --- --- ---
#> 1960-10-01 142 461 360.4986
#> 1960-11-01 143 390 340.1683
#> 1960-12-01 144 432 356.7078
# }