View source: R/compute_on_forecast.R
| transform_forecast | R Documentation |
transform_forecast() takes a univariate time series forecast fitted on a
transformed (model) scale and produces a new forecast object on the
original data scale. This is done by simulating future paths on the
model scale, transforming each path with a user-supplied function, and
then computing pointwise means and prediction intervals on the
transformed scale.
transform_forecast(fc_object, trans_fun, nsim = 2000L, level = 95, y0 = 1)
fc_object |
A |
trans_fun |
A function of the form |
nsim |
Integer; number of simulated future paths to use. Larger values give smoother prediction intervals but take longer to compute. |
level |
Numeric; prediction interval coverage in percent. |
y0 |
Numeric; starting value on the output scale used to
reconstruct the historical series from |
The function assumes that fc_object is a "forecast" object as
produced by the forecast package, and that fc_object$model
supports simulate() with arguments nsim and future.
The transformation function trans_fun must have the form
trans_fun(x, y0), where x is a numeric vector representing a path on
the model scale (for example log-values or percentage changes), and
y0 is a scalar "starting value" on the output scale. The function must
return a numeric vector of the same length as x giving the
corresponding path on the output scale.
Internally, transform_forecast() first reconstructs a historical series on
the output scale by applying trans_fun() to fc_object$x with the
supplied y0. It then simulates nsim future paths from
fc_object$model on the model scale, transforms each path to the
output scale using trans_fun() with y0 equal to the last value of
the reconstructed historical series, and finally computes the pointwise
mean and prediction intervals (of nominal coverage level) across
the simulated paths. The result is returned as a new "forecast" object
with x, mean, lower, and upper on the output scale.
A "forecast" object similar to fc_object, but with the components
x, mean, lower, and upper defined on the output (data) scale.
forecast, auto.arima,
simulate, ts.
## Example 1: Log-transform of the Canadian lynx data
if (requireNamespace("forecast", quietly = TRUE)) {
llynx <- log(lynx)
fit_log <- forecast::auto.arima(llynx)
fc_log <- forecast::forecast(fit_log, h = 20)
forecast::autoplot(fc_log)
## transformation: log -> original scale
trans_log <- function(z, y0) {
exp(z)
}
fc_lynx <- transform_forecast(fc_log, trans_fun = trans_log,
nsim = 20, level = 95, y0 = 1)
plot(fc_lynx)
forecast::autoplot(fc_lynx) + ggplot2::theme_minimal()
}
## Not run:
if (requireNamespace("forecast", quietly = TRUE)) {
## Example 2 (variation): CO2 series, log-transform
lco2 <- log(co2)
fit_co2 <- forecast::auto.arima(lco2)
fc_log_co2 <- forecast::forecast(fit_co2, h = 24)
forecast::autoplot(fc_log_co2)
trans_log <- function(z, y0) exp(z)
fc_co2 <- transform_forecast(fc_log_co2, trans_fun = trans_log,
nsim = 20, level = 95, y0 = 1)
forecast::autoplot(fc_co2) + ggplot2::theme_minimal()
}
# ## Example 3: Percentage change in income (uschange$Income)
if (requireNamespace("forecast", quietly = TRUE) &&
requireNamespace("fpp2", quietly = TRUE)) {
income <- uschange[, "Income"] # quarterly percentage changes (%)
## transformation: pct change -> index with base 1
trans_pct <- function(r, y0) {
y0 * cumprod(1 + r / 100)
}
fit_pct <- forecast::auto.arima(income)
fc_pct <- forecast::forecast(fit_pct, h = 24)
forecast::autoplot(fc_pct)
fc_idx <- transform_forecast(fc_pct, trans_fun = trans_pct,
nsim = 200, level = 95, y0 = 1)
plot(fc_idx)
forecast::autoplot(fc_idx) + ggplot2::theme_minimal()
}
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.