Description Usage Arguments Value Examples
View source: R/calculate_intervals.R
The residuals from model training/fit are sampled i.i.d. for (a) each direct forecast horizon for a single time series and (b) each combination of direct forecast horizon and group for multiple time series.
1 2 3 4 5 6 7 8 9 10 11 |
forecasts |
A data.frame of forecasts. |
residuals |
A data.frame of residuals (e.g., |
index |
Optional for forecasts from |
outcome |
Optional for forecasts from |
keys |
Optional. For grouped time series, a character vector giving the column name(s) of the group columns. The key identifies unique time series of residuals for bootstrap sampling. For direct forecasting, a single time series will have one group per direct forecast horizon. |
levels |
A numeric vector with 1 or more forecast prediction intervals. A level of .95, for example, will return the 0.25 and .975 quantiles of the bootstrapped forecast distribution at each forecast horizon. |
times |
Integer. The number of bootstrap samples. |
weights |
Not implemented. |
keep_samples |
Boolean. If |
If forecasts
is an object of class 'forecast_results', a forecast_results
object
with a new column for each lower- and upper-bound forecast in levels
. If forecasts
is a
data.frame, the function return will be the same but without forecastML
attributes. If,
keep_samples
is TRUE
, a named list of length 2 is returned with 'forecasts' and 'samples'.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | ## Not run:
data("data_seatbelts", package = "forecastML")
data_train <- create_lagged_df(data_seatbelts, type = "train", method = "direct",
outcome_col = 1, lookback = 1:15,
horizons = c(1, 6, 12))
windows <- create_windows(data_train, window_length = 0)
model_fn <- function(data) {
model <- lm(DriversKilled ~ ., data)
}
model_results <- train_model(data_train, windows, model_name = "OLS",
model_function = model_fn)
predict_fn <- function(model, data) {
data_pred <- as.data.frame(predict(model, data))
}
data_fit <- predict(model_results, prediction_function = list(predict_fn), data = data_train)
residuals <- residuals(data_fit)
data_forecast <- create_lagged_df(data_seatbelts, type = "forecast",
method = "direct", outcome_col = 1,
lookback = 1:15, horizons = c(1, 6, 12))
data_forecasts <- predict(model_results, prediction_function = list(predict_fn),
data = data_forecast)
data_forecasts <- combine_forecasts(data_forecasts)
data_forecasts <- calculate_intervals(data_forecasts, residuals, times = 30)
plot(data_forecasts)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.