plot_ts_pred_mv: Plot Multivariate Forecast Paths

View source: R/ts_plot_mv.R

plot_ts_pred_mvR Documentation

Plot Multivariate Forecast Paths

Description

Plot observed and forecast trajectories for the target series and auxiliary variables returned by the multivariate workflow.

Usage

plot_ts_pred_mv(
  history,
  future = NULL,
  prediction,
  variable = NULL,
  label_x = "",
  label_y = "Value",
  color = "black",
  color_adjust = "blue",
  color_prediction = "green"
)

Arguments

history

A ts_data_mv or data.frame with the observed history used as context for the plot.

future

Optional ts_data_mv or data.frame with the held-out aligned observations. When supplied, the observed future is shown together with the recursive predictions.

prediction

Multivariate forecast returned by predict() in the multivariate workflows. The target forecast is returned as a vector and the full system forecast is stored in attributes. Older list-based ts_mv_prediction objects are also accepted for compatibility.

variable

Optional character scalar. Name of a single variable to plot. When omitted, plots are returned for every variable in the prediction.

label_x

x-axis label.

label_y

y-axis label prefix. The variable name is appended when several plots are returned.

color

observed series color.

color_adjust

history color.

color_prediction

prediction color.

Details

plot_ts_pred_mv() extends the visual logic already used in the univariate examples. It reuses daltoolbox::plot_ts_pred() variable by variable and returns either:

  • one plot when variable is provided

  • a named list of plots when variable = NULL

The intended workflow is:

  • fit a ts_regsw_mv model

  • call predict(..., return_all = TRUE)

  • compare the predicted paths against the held-out aligned multivariate data

Value

A single ggplot object or a named list of ggplot objects.

Examples

library(daltoolbox)
data(tsd)
x1 <- c(tsd$y[-1], tail(tsd$y, 1))
x2 <- stats::filter(tsd$y, rep(1/3, 3), sides = 1)
x2[is.na(x2)] <- tsd$y[is.na(x2)]

mv <- ts_data_mv(data.frame(y = tsd$y, x1 = x1, x2 = as.numeric(x2)), y = "y")
samp <- ts_sample(mv, test_size = 5)

model <- ts_regsw_mv(
  model_y = ts_mv_spec(ts_mlp(ts_norm_gminmax(), input_size = 4), variables = c("y", "x1", "x2")),
  models_x = list(
    x1 = ts_mv_spec(ts_arima()),
    x2 = ts_mv_spec(ts_rf(ts_norm_gminmax(), input_size = 4, ntree = 10), variables = c("x2", "y"))
  ),
  window_size = 5
)
model <- daltoolbox::fit(model, samp$train)
pred <- predict(model, steps_ahead = 5, return_all = TRUE)
plots <- plot_ts_pred_mv(samp$train, samp$test, pred)

tspredit documentation built on Sept. 9, 2026, 9:08 a.m.