Returns forecasts and prediction intervals for a generalized random walk model.
rwf() is a convenience function that combines rw_model() and forecast().
naive() is a wrapper to rwf() with drift=FALSE and lag=1, while
snaive() is a wrapper to rwf() with drift=FALSE and lag=frequency(y).
# S3 method for rw_model
forecast(
object,
h = 10,
level = c(80, 95),
fan = FALSE,
simulate = FALSE,
bootstrap = FALSE,
npaths = 5000,
innov = NULL,
lambda = object$lambda,
biasadj = FALSE,
...
)rwf(
y,
h = 10,
drift = FALSE,
level = c(80, 95),
fan = FALSE,
lambda = NULL,
biasadj = FALSE,
lag = 1,
...,
x = y
)
naive(
y,
h = 10,
level = c(80, 95),
fan = FALSE,
lambda = NULL,
biasadj = FALSE,
...,
x = y
)
snaive(
y,
h = 2 * frequency(x),
level = c(80, 95),
fan = FALSE,
lambda = NULL,
biasadj = FALSE,
...,
x = y
)
An object of class forecast.
An object of class rw_model returned by rw_model().
Number of periods for forecasting. Default value is twice the largest seasonal period (for seasonal data) or ten (for non-seasonal data).
Confidence levels for prediction intervals.
If TRUE, level is set to seq(51, 99, by = 3).
This is suitable for fan plots.
If TRUE, prediction intervals are produced by simulation rather
than using analytic formulae. Errors are assumed to be normally distributed.
If TRUE, then prediction intervals are produced by
simulation using resampled errors (rather than normally distributed errors). Ignored if innov is not NULL.
Number of sample paths used in computing simulated prediction intervals.
Optional matrix of future innovations to be used in
simulations. Ignored if simulate = FALSE. If provided, this overrides the bootstrap argument. The matrix
should have h rows and npaths columns.
Box-Cox transformation parameter. If lambda = "auto",
then a transformation is automatically selected using BoxCox.lambda.
The transformation is ignored if NULL. Otherwise,
data transformed before model is estimated.
Use adjusted back-transformed mean for Box-Cox
transformations. If transformed data is used to produce forecasts and fitted
values, a regular back transformation will result in median forecasts. If
biasadj is TRUE, an adjustment will be made to produce mean forecasts
and fitted values.
Additional arguments not used.
a numeric vector or univariate time series of class ts
Logical flag. If TRUE, fits a random walk with drift model.
Lag parameter. lag = 1 corresponds to a standard random walk (giving naive forecasts if drift = FALSE or drift forecasts if drift = TRUE),
while lag = m corresponds to a seasonal random walk where m is the seasonal period (giving seasonal naive forecasts if drift = FALSE).
Deprecated. Included for backwards compatibility.
An object of class forecast is a list usually containing at least
the following elements:
A list containing information about the fitted model
The name of the forecasting method as a character string
Point forecasts as a time series
Lower limits for prediction intervals
Upper limits for prediction intervals
The confidence values associated with the prediction intervals
The original time series.
Residuals from the fitted model. For models with additive errors, the residuals will be x minus the fitted values.
Fitted values (one-step forecasts)
The function summary can be used to obtain and print a summary of the
results, while the functions plot and autoplot produce plots of the forecasts and
prediction intervals. The generic accessors functions fitted.values and residuals
extract various useful features from the underlying model.
Rob J Hyndman
The model assumes that
$$Y_t = Y_{t-p} + c + \varepsilon_{t}$$
where \(p\) is the lag parameter, \(c\) is the drift parameter, and \(\varepsilon_t\sim N(0,\sigma^2)\) are iid.
The model without drift has \(c=0\). In the model with drift, \(c\) is estimated by the sample mean of the differences \(Y_t - Y_{t-p}\).
If \(p=1\), this is equivalent to an ARIMA(0,1,0) model with an optional drift coefficient. For \(p>1\), it is equivalent to an ARIMA(0,0,0)(0,1,0)p model.
The forecasts are given by
$$Y_{T+h|T}= Y_{T+h-p(k+1)} + ch$$
where \(k\) is the integer part of \((h-1)/p\). For a regular random walk, \(p=1\) and \(c=0\), so all forecasts are equal to the last observation. Forecast standard errors allow for uncertainty in estimating the drift parameter (unlike the corresponding forecasts obtained by fitting an ARIMA model directly).
The generic accessor functions stats::fitted() and stats::residuals()
extract useful features of the object returned.
rw_model(), Arima()
# Three ways to do the same thing
gold_model <- rw_model(gold)
gold_fc1 <- forecast(gold_model, h = 50)
gold_fc2 <- rwf(gold, h = 50)
gold_fc3 <- naive(gold, h = 50)
# Plot the forecasts
autoplot(gold_fc1)
# Drift forecasts
rwf(gold, drift = TRUE) |> autoplot()
# Seasonal naive forecasts
snaive(wineind) |> autoplot()
Run the code above in your browser using DataLab