Skip to contents

Calculate 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.

Usage

width(
  object,
  ...,
  level = 95,
  includemedian = FALSE,
  window = NULL,
  na.rm = FALSE
)

Arguments

object

An object of class "cvforecast" or "cpforecast".

...

Additional inputs if object is missing.

level

Target confidence level for prediction intervals.

includemedian

If TRUE, the median interval width will also be returned.

window

If not NULL, the rolling mean (and rolling median if applicable) matrix for interval width will also be returned.

na.rm

A logical indicating whether NA values should be stripped before the rolling mean and rolling median computation proceeds.

Value

A list of class "width" with the following components:

width

Forecast interval width as a multivariate time series, where the \(h\)th column holds the interval width for the forecast horizon \(h\). The time index corresponds to the period for which the forecast is produced.

mean

Mean interval width across the validation set.

rollmean

If window is not NULL, a matrix of the rolling means of interval width will be returned.

median

Median interval width across the validation set.

rollmedian

If window is not NULL, a matrix of the rolling medians of interval width will be returned.

Examples

# Simulate time series from an AR(2) model
library(forecast)
series <- arima.sim(n = 1000, list(ar = c(0.8, -0.5)), sd = sqrt(1))

# Cross-validation forecasting with a rolling window of length 100
far2 <- function(x, h, level) {
  Arima(x, order = c(2, 0, 0)) |>
    forecast(h = h, level)
}
fc <- cvforecast(series, forecastfun = far2, h = 3, level = c(80, 95),
                 forward = TRUE, initial = 1, window = 100)

# Mean and rolling mean width for interval forecasts on validation set
wid_fc <- width(fc, level = 95, window = 100)
str(wid_fc)
#> List of 3
#>  $ width   : Time-Series [1:903, 1:3] from 101 to 1003: 4.01 4 4.01 4.02 4.02 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : NULL
#>   .. ..$ : chr [1:3] "h=1" "h=2" "h=3"
#>  $ mean    : Named num [1:3] 3.94 5.03 5.08
#>   ..- attr(*, "names")= chr [1:3] "h=1" "h=2" "h=3"
#>  $ rollmean: Time-Series [1:804, 1:3] from 200 to 1003: 3.93 3.93 3.93 3.93 3.93 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : NULL
#>   .. ..$ : chr [1:3] "h=1" "h=2" "h=3"
#>  - attr(*, "class")= chr "width"