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 with horizon-shifted offsets via lags – do not include any iterative feature PipeOp (property "fcst_iterative", e.g. PipeOpFcstLags, PipeOpFcstRolling) in the learner or graph. Such ops cannot yet be horizon-offset and would leak future information for horizons > 1, so they are rejected at construction.

Super class

mlr3::Learner -> DirectForecaster

Active bindings

learner

(mlr3::Learner)
The base regression learner.

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.

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

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)
#> 
#> ── <PredictionRegr> for 29 observations: ───────────────────────────────────────
#>  row_ids truth response      month
#>      116   505 391.9375 1958-08-01
#>      117   404 324.7500 1958-09-01
#>      118   359 306.0000 1958-10-01
#>      ---   ---      ---        ---
#>      142   461 368.6875 1960-10-01
#>      143   390 364.9333 1960-11-01
#>      144   432 362.3571 1960-12-01

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