| cvforecast | R Documentation |
Compute forecasts and other information by applying
forecastfun to subsets of the time series y using a
rolling forecast origin.
cvforecast(
y,
forecastfun,
h = 1,
level = c(80, 95),
forward = TRUE,
xreg = NULL,
initial = 1,
window = NULL,
...
)
y |
Univariate time series. |
forecastfun |
Function to return an object of class |
h |
Forecast horizon. |
level |
Confidence level for prediction intervals.
If |
forward |
If |
xreg |
Exogenous predictor variables passed to |
initial |
Initial period of the time series where no cross-validation forecasting is performed. |
window |
Length of the rolling window. If |
... |
Other arguments are passed to |
Let y denote the time series y_1,\dots,y_T and
let t_0 denote the initial period.
Suppose forward = TRUE. If window is NULL,
forecastfun is applied successively to the subset time series
y_{1},\dots,y_t, for t=t_0,\dots,T,
generating forecasts \hat{y}_{t+1|t},\dots,\hat{y}_{t+h|t}. If window is not
NULL and has a length of t_w, then forecastfun is applied
successively to the subset time series y_{t-t_w+1},\dots,y_{t},
for t=\max(t_0, t_w),\dots,T.
If forward is FALSE, the last observation used for training will
be y_{T-1}.
A list of class c("cvforecast", "forecast") with components:
x |
The original time series. |
series |
The name of the series |
xreg |
Exogenous predictor variables used in the model, if applicable. |
method |
A character string "cvforecast". |
fit_times |
The number of times the model is fitted in cross-validation. |
MEAN |
Point forecasts as a multivariate time series, where the |
ERROR |
Forecast errors given by
|
LOWER |
A list containing lower bounds for prediction intervals for
each |
UPPER |
A list containing upper bounds for prediction intervals for
each |
level |
The confidence values associated with the prediction intervals. |
call |
The matched call. |
forward |
Whether |
If forward is TRUE, the components mean, lower,
upper, and model will also be returned, showing the information
about the final fitted model and forecasts using all available observations, see
e.g. forecast.ets for more details.
# Simulate time series from an AR(2) model
library(forecast)
series <- arima.sim(n = 200, list(ar = c(0.8, -0.5)), sd = sqrt(1))
# Example with a rolling window
far2 <- function(x, h, level) {
Arima(x, order = c(2, 0, 0)) |>
forecast(h = h, level)
}
fc <- cvforecast(series, forecastfun = far2, h = 3, level = 95,
forward = TRUE, initial = 1, window = 50)
print(fc)
summary(fc)
# Example with exogenous predictors
far2_xreg <- function(x, h, level, xreg, newxreg) {
Arima(x, order=c(2, 0, 0), xreg = xreg) |>
forecast(h = h, level = level, xreg = newxreg)
}
fc_xreg <- cvforecast(series, forecastfun = far2_xreg, h = 3, level = 95,
forward = TRUE, xreg = matrix(rnorm(406), ncol = 2, nrow = 203),
initial = 1, window = 50)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.