Skip to contents

Trains a separate model for each forecast horizon. For horizon h with base lags 1:p, model h uses lags h:(h+p-1), so that at prediction time only observed values are needed. Unlike RecursiveForecaster, predictions do not feed back into subsequent steps (no error accumulation).

Lag features are managed internally via lags. Do not add iterative feature PipeOps (property "fcst_iterative", e.g. PipeOpFcstLags, PipeOpFcstRolling), which are rejected at construction.

Super class

mlr3::Learner -> DirectForecaster

Active bindings

learner

(mlr3::Learner)
The base regression learner.

native_model

(named list())
The fitted models.

lags

(integer())
The base lags.

horizons

(integer())
The forecast horizons.

param_set

(paradox::ParamSet)
Set of hyperparameters.

marshaled

(logical(1))
Whether the learner's model is currently in marshaled form.

predict_type

(character(1))
Stores the currently active predict type.

Methods

Inherited methods


DirectForecaster$new()

Creates a new instance of this R6 class.

Usage

DirectForecaster$new(
  learner,
  lags,
  horizons,
  id = NULL,
  param_vals = list(),
  predict_type = NULL
)

Arguments

learner

(mlr3::Learner | mlr3pipelines::Graph | mlr3pipelines::PipeOp)
A regression learner or a graph/PipeOp (without PipeOpFcstLags).

lags

(integer())
The base lag values. Exposed in $param_set as lags, so it can be tuned via mlr3tuning::AutoTuner.

horizons

(integer())
Either a single integer H (expanded to 1:H) or an integer vector of specific horizons. One model is trained per horizon. At predict time each test row is routed to the model matching its step-distance from the end of training, so with specific horizons (e.g. c(2L, 4L, 6L)) the test set may only contain rows at those exact steps ahead.

id

(character(1) | NULL)
Identifier, default NULL (auto-generated from the learner id).

param_vals

(named list())
Hyperparameter values applied to every horizon model. Per-horizon hyperparameters are not currently supported.

predict_type

(character(1) | NULL)
The predict type, default NULL.


DirectForecaster$print()

Printer.

Usage

DirectForecaster$print(...)

Arguments

...

(ignored).


DirectForecaster$marshal()

Marshal the learner's model.

Usage

DirectForecaster$marshal(...)

Arguments

...

(any)
Additional arguments passed to mlr3::marshal_model().


DirectForecaster$unmarshal()

Unmarshal the learner's model.

Usage

DirectForecaster$unmarshal(...)

Arguments

...

(any)
Additional arguments passed to mlr3::unmarshal_model().


DirectForecaster$clone()

The objects of this class are cloneable with this method.

Usage

DirectForecaster$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

# \donttest{
library(mlr3pipelines)

task = tsk("airpassengers")
split = partition(task, ratio = 0.8)

# simple: one model per horizon
flrn = DirectForecaster$new(lrn("regr.rpart"), lags = 1:3, horizons = length(split$test))
flrn$train(task, split$train)
flrn$predict(task, split$test)
#> 
#> ── <PredictionFcst> for 29 observations: ───────────────────────────────────────
#>       month row_ids truth response
#>  1958-08-01     116   505 391.9375
#>  1958-09-01     117   404 324.7500
#>  1958-10-01     118   359 306.0000
#>         ---     ---   ---      ---
#>  1960-10-01     142   461 368.6875
#>  1960-11-01     143   390 364.9333
#>  1960-12-01     144   432 362.3571

# or use the direct_forecaster() helper
flrn = direct_forecaster(lrn("regr.rpart"), lags = 1:3, horizons = length(split$test))
flrn$train(task, split$train)
flrn$predict(task, split$test)
#> 
#> ── <PredictionFcst> for 29 observations: ───────────────────────────────────────
#>       month row_ids truth response
#>  1958-08-01     116   505 391.9375
#>  1958-09-01     117   404 324.7500
#>  1958-10-01     118   359 306.0000
#>         ---     ---   ---      ---
#>  1960-10-01     142   461 368.6875
#>  1960-11-01     143   390 364.9333
#>  1960-12-01     144   432 362.3571
# }