View source: R/prediction_intervals.R
| avgWidth | R Documentation |
This is a wrapper for the function conformalForecast::width.
Calculates the mean width of prediction intervals on the validation set. If
window is not NULL, a matrix of the rolling means of interval
width is also returned. If includemedian is TRUE, the
information of the median interval width will be returned.
avgWidth(
object,
level = 95,
includemedian = FALSE,
window = NULL,
na.rm = FALSE
)
object |
An object of class |
level |
Target confidence level for prediction intervals. |
includemedian |
If |
window |
If not |
na.rm |
A logical indicating whether |
A list of class width with the following components:
width |
Forecast interval width as a multivariate time series, where the
|
mean |
Mean interval width across the validation set. |
rollmean |
If |
median |
Median interval width across the validation set. |
rollmedian |
If |
library(dplyr)
library(tibble)
library(tidyr)
library(tsibble)
# Simulate data
n = 1055
set.seed(123)
sim_data <- tibble(x_lag_000 = runif(n)) |>
mutate(
# Add x_lags
x_lag = lag_matrix(x_lag_000, 5)) |>
unpack(x_lag, names_sep = "_") |>
mutate(
# Response variable
y = (0.9*x_lag_000 + 0.6*x_lag_001 + 0.45*x_lag_003)^3 + rnorm(n, sd = 0.1),
# Add an index to the data set
inddd = seq(1, n)) |>
drop_na() |>
select(inddd, y, starts_with("x_lag")) |>
# Make the data set a `tsibble`
as_tsibble(index = inddd)
# Training set
sim_train <- sim_data[1:1000, ]
# Test set
sim_test <- sim_data[1001:1050, ]
# Index variables
index.vars <- colnames(sim_data)[3:8]
# Model fitting
pprModel <- model_ppr(data = sim_train,
yvar = "y",
index.vars = index.vars)
# Conformal bootstrap prediction intervals (2-steps-ahead interval forecasts)
set.seed(12345)
pprModel_cb <- cb_cvforecast(object = pprModel,
data = sim_data,
yvar = "y",
predictor.vars = index.vars,
h = 2,
ncal = 30,
num.futures = 100,
window = 1000)
# Mean width of generated 95% conformal bootstrap prediction intervals
width_data <- avgWidth(object = pprModel_cb)
width_data$mean
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.