Update and repeform cross-validation forecasting and conformal prediction
Source:R/update.R
update.cpforecast.Rd
Update conformal prediction intervals and other information by applying the
cvforecast
and conformal
functions.
Usage
# S3 method for class 'cpforecast'
update(object, new_data, forecastfun, new_xreg = NULL, ...)
Arguments
- object
An object of class
"cpforecast"
.- new_data
A vector of newly available data.
- forecastfun
Function to return an object of class
"forecast"
. Its first argument must be a univariate time series, and it must have an argumenth
for the forecast horizon and an argumentlevel
for the confidence level for prediction intervals. If exogenous predictors are used, then it must also havexreg
andnewxreg
arguments corresponding to the training and test periods, respectively.- new_xreg
Newly available exogenous predictor variables passed to
forecastfun
if required. The number of rows should match the length ofnew_data
, and the number of columns should match the dimensions of thexreg
argument inobject
.- ...
Other arguments are passed to
forecastfun
.
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
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)
# Classical conformal prediction with equal weights
scpfc <- conformal(fc, method = "scp", symmetric = FALSE, ncal = 100, rolling = TRUE)
# Update conformal prediction using newly available data
scpfc_update <- update(scpfc, forecastfun = far2, new_data = c(1.5, 0.8, 2.3))
print(scpfc_update)
#> SCP
#>
#> Call:
#> scp(object = object, symmetric = FALSE, ncal = 100, rolling = TRUE,
#> update = TRUE)
#>
#> cp_times = 802 (the forward step included)
#>
#> Forecasts of the forward step:
#> Cross-validation
#>
#> Call:
#> scp(object = object, symmetric = FALSE, ncal = 100, rolling = TRUE,
#> update = TRUE)
#>
#> fit_times = (the forward step included)
#>
#> Forecasts of the forward step:
#> Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
#> 1004 1.4440040 0.2074404 2.6554323 -0.507129 3.645638
#> 1005 0.3029272 -1.1852330 1.6128637 -2.355492 2.739304
#> 1006 -0.2613931 -1.8622027 0.9570921 -2.845167 2.387893
summary(scpfc_update)
#> SCP
#>
#> Call:
#> scp(object = object, symmetric = FALSE, ncal = 100, rolling = TRUE,
#> update = TRUE)
#>
#> cp_times = 802 (the forward step included)
#>
#> Forecasts of the forward step:
#> Cross-validation
#>
#> Call:
#> scp(object = object, symmetric = FALSE, ncal = 100, rolling = TRUE,
#> update = TRUE)
#>
#> fit_times = (the forward step included)
#>
#> Forecasts of the forward step:
#> Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
#> 1004 1.4440040 0.2074404 2.6554323 -0.507129 3.645638
#> 1005 0.3029272 -1.1852330 1.6128637 -2.355492 2.739304
#> 1006 -0.2613931 -1.8622027 0.9570921 -2.845167 2.387893
#>
#> Cross-validation error measures:
#> ME MAE MSE RMSE MPE MAPE MASE RMSSE Winkler_95 MSIS_95
#> CV 0.002 0.994 1.599 1.122 -50.052 602.464 0.934 0.842 6.218 5.849