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
via forecast::BoxCox.lambda(), using the task frequency for the "guerrero" method so seasonality is accounted
for. The estimated (or supplied) lambda is stored and reused at predict time and for inversion.
Box-Cox and log transformations require strictly positive target values; non-positive values produce NaN or an
error from forecast::BoxCox().
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. Inside
RecursiveForecaster the transformation would entangle with the iterative lag/rolling feedback, which reads the
original-scale backend, producing a train/predict scale mismatch (rejected at construction). Use inside a plain
mlr3pipelines::GraphLearner via ppl("targettrafo", ...) for batch prediction, 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
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)
#>
#> ── <PredictionRegr> for 29 observations: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
#> row_ids truth response
#> 116 505 409.4023
#> 117 404 360.7524
#> 118 359 303.4957
#> --- --- ---
#> 142 461 360.4986
#> 143 390 340.1683
#> 144 432 356.7078