Title: | Linear Model Forecasting |
---|---|
Description: | Introduces in-sample, out-of-sample, pseudo out-of-sample, and benchmark model forecast tests and a new class for working with forecast data, Forecast. |
Authors: | Nelson Rayl [aut, cre] |
Maintainer: | Nelson Rayl <[email protected]> |
License: | GPL-3 |
Version: | 1.0.0 |
Built: | 2024-10-31 22:15:13 UTC |
Source: | https://github.com/nelson-n/lmforc |
[]
takes a Forecast
object and subsets it.
## S4 method for signature 'Forecast' x[i, j, ..., drop = TRUE]
## S4 method for signature 'Forecast' x[i, j, ..., drop = TRUE]
x |
ANY |
i |
ANY |
j |
ANY |
... |
ANY |
drop |
ANY |
Forecast |
Forecast object. |
Subsetted Forecast
object.
autoreg_forc
takes a vector of realized values, an integer number of
periods ahead to forecast, an integer number of lags to include in the
autoregressive model, a period to end the initial model estimation and
begin forecasting, an optional vector of time data associated with the
realized values, and an optional integer number of past periods to estimate
the model over. An AR(ar_lags
) autoregressive model is originally estimated
with realized values up to estimation_end
minus the number of periods specified
in estimation_window
. If estimation_window
is left NULL
then the autoregressive model is estimated with all realized values up to
estimation_end
. The AR(ar_lags
) model is estimated by regressing the
realized values on the same realized values that have been lagged by
one to ar_lags
steps. The AR coefficients of this model are multiplied by
lagged values and the present period realized value to create a forecast for
one period ahead. If h_ahead
is greater than one, this process of
forecasting one period ahead is iteratively repeated so that the two period
ahead forecast conditions on the one period ahead forecasted value and so
on until a h_ahead
forecast is obtained. This forecasting process is
repeated for each period after estimation_end
with AR model coefficients
updating as more information would have become available to the forecaster.
Optionally returns the coefficients used to create each forecast.
Returns an autoregression forecast based on information that would
have been available at the forecast origin and replicates the forecasts that an
AR model would have produced in real-time.
autoreg_forc( realized_vec, h_ahead, ar_lags, estimation_end, time_vec = NULL, estimation_window = NULL, return_betas = FALSE )
autoreg_forc( realized_vec, h_ahead, ar_lags, estimation_end, time_vec = NULL, estimation_window = NULL, return_betas = FALSE )
realized_vec |
Vector of realized values. This is the series that is being forecasted. |
h_ahead |
Integer representing the number of periods ahead that is being forecasted. |
ar_lags |
Integer representing the number of lags included in the AR model. |
estimation_end |
Value of any class representing when to end the initial coefficient estimation period and begin forecasting. |
time_vec |
Vector of any class that is equal in length to the
|
estimation_window |
Integer representing the number of past periods that the autoregressive model should be estimated over in each period. |
return_betas |
Boolean, selects whether the coefficients used in each period to create the forecast are returned. If TRUE, a data frame of betas is returned to the Global Environment. |
Forecast
object that contains the autoregression
forecast.
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30", "2012-09-30", "2012-12-31")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99, 1.31, 2.33) data <- data.frame(date, y) autoreg_forc( realized_vec = data$y, h_ahead = 1L, ar_lags = 2L, estimation_end = as.Date("2011-06-30"), time_vec = data$date, estimation_window = 4L, return_betas = FALSE ) autoreg_forc( realized_vec = data$y, h_ahead = 4L, ar_lags = 2L, estimation_end = 4L, time_vec = NULL, estimation_window = NULL )
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30", "2012-09-30", "2012-12-31")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99, 1.31, 2.33) data <- data.frame(date, y) autoreg_forc( realized_vec = data$y, h_ahead = 1L, ar_lags = 2L, estimation_end = as.Date("2011-06-30"), time_vec = data$date, estimation_window = 4L, return_betas = FALSE ) autoreg_forc( realized_vec = data$y, h_ahead = 4L, ar_lags = 2L, estimation_end = 4L, time_vec = NULL, estimation_window = NULL )
conditional_forc
takes a linear model call, a vector of time data
associated with the linear model, and a forecast for each covariate in the
linear model. The linear model is estimated once over the entire sample
period and the coefficients are multiplied by the forecasts of each
covariate. Returns a forecast conditional on forecasts of each covariate.
Used to create a forecast for the present period or replicate a forecast made
at a specific period in the past.
conditional_forc(lm_call, time_vec, ...)
conditional_forc(lm_call, time_vec, ...)
lm_call |
Linear model call of the class lm. |
time_vec |
Vector of any class that is equal in length to the data
in |
... |
One or more forecasts of class Forecast, one forecast for each covariate in the linear model. |
Forecast
object that contains the conditional forecast.
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
x1_forecast <- Forecast( origin = as.Date(c("2012-06-30", "2012-06-30", "2012-06-30", "2012-06-30")), future = as.Date(c("2012-09-30", "2012-12-31", "2013-03-31", "2013-06-30")), forecast = c(4.14, 4.04, 4.97, 5.12), realized = NULL, h_ahead = NULL ) x2_forecast <- Forecast( origin = as.Date(c("2012-06-30", "2012-06-30", "2012-06-30", "2012-06-30")), future = as.Date(c("2012-09-30", "2012-12-31", "2013-03-31", "2013-06-30")), forecast = c(6.01, 6.05, 6.55, 7.45), realized = NULL, h_ahead = NULL ) date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99) x1 <- c(4.22, 3.86, 4.27, 5.60, 5.11, 4.31, 4.92, 5.80, 6.30, 4.17) x2 <- c(10.03, 10.49, 10.85, 10.47, 9.09, 10.91, 8.68, 9.91, 7.87, 6.63) data <- data.frame(date, y, x1, x2) conditional_forc( lm_call = lm(y ~ x1 + x2, data), time_vec = data$date, x1_forecast, x2_forecast )
x1_forecast <- Forecast( origin = as.Date(c("2012-06-30", "2012-06-30", "2012-06-30", "2012-06-30")), future = as.Date(c("2012-09-30", "2012-12-31", "2013-03-31", "2013-06-30")), forecast = c(4.14, 4.04, 4.97, 5.12), realized = NULL, h_ahead = NULL ) x2_forecast <- Forecast( origin = as.Date(c("2012-06-30", "2012-06-30", "2012-06-30", "2012-06-30")), future = as.Date(c("2012-09-30", "2012-12-31", "2013-03-31", "2013-06-30")), forecast = c(6.01, 6.05, 6.55, 7.45), realized = NULL, h_ahead = NULL ) date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99) x1 <- c(4.22, 3.86, 4.27, 5.60, 5.11, 4.31, 4.92, 5.80, 6.30, 4.17) x2 <- c(10.03, 10.49, 10.85, 10.47, 9.09, 10.91, 8.68, 9.91, 7.87, 6.63) data <- data.frame(date, y, x1, x2) conditional_forc( lm_call = lm(y ~ x1 + x2, data), time_vec = data$date, x1_forecast, x2_forecast )
conditional_forc_general
takes a model function, a prediction function,
input data for estimating the model, and a vector of time data associated with the
model. The model is estimated once over the entire sample period and the model
parameters are then combined with the input forecasts to generate a forecast.
Returns a forecast conditional on forecasts of each parameter. Used to create a
forecast for the present period or replicate a forecast made at a specific period
in the past.
conditional_forc_general( model_function, prediction_function, data, time_vec, ... )
conditional_forc_general( model_function, prediction_function, data, time_vec, ... )
model_function |
Function that estimates a model using the |
prediction_function |
Function that generates model predictions using |
data |
Input data for estimating the model. |
time_vec |
Vector of any class that represents time and is equal in length to
the length of |
... |
Set of forecasts of class Forecast, one forecast for each parameter in the linear model. |
Forecast
object that contains the out-of-sample
forecast.
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
# Estimation Data. date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30", "2012-09-30", "2012-12-31", "2013-03-31", "2013-06-30", "2013-09-30", "2013-12-31")) y <- c(1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0) x1 <- c(8.22, 3.86, 4.27, 3.37, 5.88, 3.34, 2.92, 1.80, 3.30, 7.17, 3.22, 3.86, 4.27, 3.37, 5.88, 3.34) x2 <- c(4.03, 2.46, 2.04, 2.44, 6.09, 2.91, 1.68, 2.91, 3.87, 1.63, 4.03, 2.46, 2.04, 2.44, 6.09, 2.91) dataLogit <- data.frame(date, y, x1, x2) # Parameter Forecasts. x1_forecastLogit <- Forecast( origin = as.Date(c("2013-12-31", "2013-12-31", "2013-12-31", "2013-12-31")), future = as.Date(c("2014-03-31", "2014-06-30", "2014-09-30", "2014-12-31")), forecast = c(2.11, 6.11, 6.75, 4.30), realized = NULL, h_ahead = NULL ) x2_forecastLogit <- Forecast( origin = as.Date(c("2013-12-31", "2013-12-31", "2013-12-31", "2013-12-31")), future = as.Date(c("2014-03-31", "2014-06-30", "2014-09-30", "2014-12-31")), forecast = c(1.98, 7.44, 7.86, 5.98), realized = NULL, h_ahead = NULL ) # Forecasting Function. conditional_forc_general( model_function = function(data) {glm(y ~ x1 + x2, data = data, family = binomial)}, prediction_function = function(model_function, data) { names(data) <- c("x1", "x2") as.vector(predict(model_function, data, type = "response")) }, data = dataLogit, time_vec = dataLogit$date, x1_forecastLogit, x2_forecastLogit )
# Estimation Data. date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30", "2012-09-30", "2012-12-31", "2013-03-31", "2013-06-30", "2013-09-30", "2013-12-31")) y <- c(1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0) x1 <- c(8.22, 3.86, 4.27, 3.37, 5.88, 3.34, 2.92, 1.80, 3.30, 7.17, 3.22, 3.86, 4.27, 3.37, 5.88, 3.34) x2 <- c(4.03, 2.46, 2.04, 2.44, 6.09, 2.91, 1.68, 2.91, 3.87, 1.63, 4.03, 2.46, 2.04, 2.44, 6.09, 2.91) dataLogit <- data.frame(date, y, x1, x2) # Parameter Forecasts. x1_forecastLogit <- Forecast( origin = as.Date(c("2013-12-31", "2013-12-31", "2013-12-31", "2013-12-31")), future = as.Date(c("2014-03-31", "2014-06-30", "2014-09-30", "2014-12-31")), forecast = c(2.11, 6.11, 6.75, 4.30), realized = NULL, h_ahead = NULL ) x2_forecastLogit <- Forecast( origin = as.Date(c("2013-12-31", "2013-12-31", "2013-12-31", "2013-12-31")), future = as.Date(c("2014-03-31", "2014-06-30", "2014-09-30", "2014-12-31")), forecast = c(1.98, 7.44, 7.86, 5.98), realized = NULL, h_ahead = NULL ) # Forecasting Function. conditional_forc_general( model_function = function(data) {glm(y ~ x1 + x2, data = data, family = binomial)}, prediction_function = function(model_function, data) { names(data) <- c("x1", "x2") as.vector(predict(model_function, data, type = "response")) }, data = dataLogit, time_vec = dataLogit$date, x1_forecastLogit, x2_forecastLogit )
Given a list of forecasts with homogenous origin or future values, converts the forecasts to h_ahead format based on the index passed to the index argument. Subsets all forecasts at the index value and aggregates these forecasts into an h_ahead Forecast object with h_ahead equal to the value passed to the h_aheads argument.
convert_byh(forcs, index, h_aheads)
convert_byh(forcs, index, h_aheads)
forcs |
List of Forecast objects with the same number of observations. |
index |
Numeric or logical value or vector. |
h_aheads |
Value or vector of h_ahead values that is equal in length to the index argument. |
Single Forecast object or list of Forecast objects in h_ahead format.
# The following forecasts are in time format. Each forecast was made at a # different time and represents a forecast for a number of h_ahead periods # ahead. forc1_t1 <- Forecast( origin = as.Date(c("2010-02-17", "2010-02-17", "2010-02-17")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31")), forecast = c(4.27, 3.77, 3.52), realized = c(4.96, 4.17, 4.26), h_ahead = NA ) forc1_t2 <- Forecast( origin = as.Date(c("2010-05-14", "2010-05-14", "2010-05-14")), future = as.Date(c("2010-09-30", "2010-12-31", "2011-03-31")), forecast = c(3.36, 3.82, 4.22), realized = c(4.17, 4.26, 4.99), h_ahead = NA ) forc1_t3 <- Forecast( origin = as.Date(c("2010-07-22", "2010-07-22", "2010-07-22")), future = as.Date(c("2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(4.78, 4.53, 5.03), realized = c(4.26, 4.99, 5.33), h_ahead = NA ) forc1_t4 <- Forecast( origin = as.Date(c("2010-12-22", "2010-12-22", "2010-12-22")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30")), forecast = c(5.45, 4.89, 5.78), realized = c(4.99, 5.33, 5.21), h_ahead = NA ) forcs <- list(forc1_t1, forc1_t2, forc1_t3, forc1_t4) convert_byh(forcs, index = 1L, h_aheads = 1) convert_byh(forcs, index = 1:2, h_aheads = c(1, 2))
# The following forecasts are in time format. Each forecast was made at a # different time and represents a forecast for a number of h_ahead periods # ahead. forc1_t1 <- Forecast( origin = as.Date(c("2010-02-17", "2010-02-17", "2010-02-17")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31")), forecast = c(4.27, 3.77, 3.52), realized = c(4.96, 4.17, 4.26), h_ahead = NA ) forc1_t2 <- Forecast( origin = as.Date(c("2010-05-14", "2010-05-14", "2010-05-14")), future = as.Date(c("2010-09-30", "2010-12-31", "2011-03-31")), forecast = c(3.36, 3.82, 4.22), realized = c(4.17, 4.26, 4.99), h_ahead = NA ) forc1_t3 <- Forecast( origin = as.Date(c("2010-07-22", "2010-07-22", "2010-07-22")), future = as.Date(c("2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(4.78, 4.53, 5.03), realized = c(4.26, 4.99, 5.33), h_ahead = NA ) forc1_t4 <- Forecast( origin = as.Date(c("2010-12-22", "2010-12-22", "2010-12-22")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30")), forecast = c(5.45, 4.89, 5.78), realized = c(4.99, 5.33, 5.21), h_ahead = NA ) forcs <- list(forc1_t1, forc1_t2, forc1_t3, forc1_t4) convert_byh(forcs, index = 1L, h_aheads = 1) convert_byh(forcs, index = 1:2, h_aheads = c(1, 2))
Given a list of forecasts with different h_ahead values, converts the forecasts to time format based on the time object passed to the values argument. Converts Forecast objects that have have homogenous h_ahead values to Forecast objects with homogenous origin or future values.
convert_bytime(forcs, values, slot)
convert_bytime(forcs, values, slot)
forcs |
List of Forecast objects. |
values |
Single time object or a vector of time objects. |
slot |
Character representing whether the list of Forecasts will be converted to homogenous origin or future values. Must be either "origin" or "future". |
Single Forecast object or list of Forecast objects in time format.
# The following forecasts are in h_ahead format. All forecasts come from the # same source (forc1) and have the same origin values. However, the forecasts # are for different periods ahead. forc1_1h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31")), forecast = c(4.27, 3.36, 4.78, 5.45), realized = c(4.96, 4.17, 4.26, 4.99), h_ahead = 1 ) forc1_2h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22")), future = as.Date(c("2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(3.77, 3.82, 4.53, 4.89), realized = c(4.17, 4.26, 4.99, 5.33), h_ahead = 2 ) forc1_3h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22")), future = as.Date(c("2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30")), forecast = c(3.52, 4.22, 5.03, 5.78), realized = c(4.26, 4.99, 5.33, 5.21), h_ahead = 3 ) forcs <- list(forc1_1h, forc1_2h, forc1_3h) convert_bytime(forcs, value = as.Date("2010-05-14"), slot = "origin") convert_bytime( forcs, value = as.Date(c("2010-07-22", "2010-12-22")), slot = "origin" )
# The following forecasts are in h_ahead format. All forecasts come from the # same source (forc1) and have the same origin values. However, the forecasts # are for different periods ahead. forc1_1h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31")), forecast = c(4.27, 3.36, 4.78, 5.45), realized = c(4.96, 4.17, 4.26, 4.99), h_ahead = 1 ) forc1_2h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22")), future = as.Date(c("2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(3.77, 3.82, 4.53, 4.89), realized = c(4.17, 4.26, 4.99, 5.33), h_ahead = 2 ) forc1_3h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22")), future = as.Date(c("2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30")), forecast = c(3.52, 4.22, 5.03, 5.78), realized = c(4.26, 4.99, 5.33, 5.21), h_ahead = 3 ) forcs <- list(forc1_1h, forc1_2h, forc1_3h) convert_bytime(forcs, value = as.Date("2010-05-14"), slot = "origin") convert_bytime( forcs, value = as.Date(c("2010-07-22", "2010-12-22")), slot = "origin" )
forc
takes a Forecast
object and gets the
forecast vector of the forecast.
forc(Forecast)
forc(Forecast)
Forecast |
Forecast object. |
Vector of forecast values stored in the Forecast object.
## Not run: forc(Forecast) ## End(Not run)
## Not run: forc(Forecast) ## End(Not run)
forc
takes a Forecast
object and gets the
forecast vector of the forecast.
## S4 method for signature 'Forecast' forc(Forecast)
## S4 method for signature 'Forecast' forc(Forecast)
Forecast |
Forecast object. |
Vector of forecast values stored in the Forecast object.
## Not run: forc(Forecast) ## End(Not run)
## Not run: forc(Forecast) ## End(Not run)
forc
takes a Forecast
object and sets the forecast
vector of the forecast.
forc(Forecast) <- value
forc(Forecast) <- value
Forecast |
Forecast object. |
value |
Vector of values assigned to the forecast slot of the Forecast. |
Forecast
object that contains the new forecast vector.
## Not run: forc(Forecast) <- c(2.45, 2.76, 3.31) ## End(Not run)
## Not run: forc(Forecast) <- c(2.45, 2.76, 3.31) ## End(Not run)
forc
takes a Forecast
object and sets the forecast
vector of the forecast.
## S4 replacement method for signature 'Forecast' forc(Forecast) <- value
## S4 replacement method for signature 'Forecast' forc(Forecast) <- value
Forecast |
Forecast object. |
value |
Vector of values assigned to the forecast slot of the Forecast. |
Forecast
object that contains the new forecast vector.
## Not run: forc(Forecast) <- c(2.45, 2.76, 3.31) ## End(Not run)
## Not run: forc(Forecast) <- c(2.45, 2.76, 3.31) ## End(Not run)
forc2df
takes one or more objects of the Forecast class and collects
them into a data frame. Returns a data frame with all of the information that
was stored in the Forecast objects. If multiple forecasts are being
collected, all forecasts must have identical future and realized values.
forc2df(...)
forc2df(...)
... |
One or multiple forecasts of the class Forecast. |
data.frame
object that contains forecast information.
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
## Not run: forc2df(x1_forecast) forc2df(x1_forecast, x2_forecast) ## End(Not run)
## Not run: forc2df(x1_forecast) forc2df(x1_forecast, x2_forecast) ## End(Not run)
An S4 class for storing forecasts. An object of the Forecast class has equal length vectors that contain the time the forecast was made, the future time being forecasted, the forecast, and realized values if available. Optionally includes the number of periods ahead being forecasted.
Forecast(origin, future, forecast, realized = NULL, h_ahead = NULL)
Forecast(origin, future, forecast, realized = NULL, h_ahead = NULL)
origin |
A vector of any class representing the time when the forecast was made. |
future |
A vector of any class representing the time that is being forecasted, i.e. when the forecast will be realized. |
forecast |
A numeric vector of forecasts. |
realized |
Optional numeric vector of realized values, i.e. the true value at the future time. |
h_ahead |
Optional length-one object representing the number of periods ahead being forecasted. |
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
my_forecast <- Forecast( origin = c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31"), future = c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31"), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) origin(my_forecast) <- c("2010-04-01", "2010-07-01", "2010-10-01", "2011-01-01") future(my_forecast) <- c("2012-04-01", "2012-07-01", "2012-10-01", "2013-01-01") forc(my_forecast) <- c(8.87, 7.61, 7.56, 5.96) realized(my_forecast) <- c(6.64, 6.10, 6.33, 6.67) h_ahead(my_forecast) <- 8L origin(my_forecast) future(my_forecast) forc(my_forecast) realized(my_forecast) h_ahead(my_forecast)
my_forecast <- Forecast( origin = c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31"), future = c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31"), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) origin(my_forecast) <- c("2010-04-01", "2010-07-01", "2010-10-01", "2011-01-01") future(my_forecast) <- c("2012-04-01", "2012-07-01", "2012-10-01", "2013-01-01") forc(my_forecast) <- c(8.87, 7.61, 7.56, 5.96) realized(my_forecast) <- c(6.64, 6.10, 6.33, 6.67) h_ahead(my_forecast) <- 8L origin(my_forecast) future(my_forecast) forc(my_forecast) realized(my_forecast) h_ahead(my_forecast)
An S4 class for storing forecasts. An object of the Forecast class has equal length vectors that contain the time the forecast was made, the future time being forecasted, the forecast, and realized values if available. Optionally includes the number of periods ahead being forecasted.
origin
A vector of any class representing the time when the forecast was made.
future
A vector of any class representing the time that is being forecasted, i.e. when the forecast will be realized.
forecast
A numeric vector of forecasts.
realized
Optional numeric vector of realized values, i.e. the true value at the future time.
h_ahead
Optional length-one object representing the number of periods ahead being forecasted.
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
future
takes a Forecast
object and gets the future
vector of the forecast.
future(Forecast)
future(Forecast)
Forecast |
object. |
Vector of future values stored in the Forecast object.
## Not run: future(Forecast) ## End(Not run)
## Not run: future(Forecast) ## End(Not run)
future
takes a Forecast
object and gets the future
vector of the forecast.
## S4 method for signature 'Forecast' future(Forecast)
## S4 method for signature 'Forecast' future(Forecast)
Forecast |
object. |
Vector of future values stored in the Forecast object.
## Not run: future(Forecast) ## End(Not run)
## Not run: future(Forecast) ## End(Not run)
future
takes a Forecast
object and sets the future
vector of the forecast.
future(Forecast) <- value
future(Forecast) <- value
Forecast |
Forecast object. |
value |
Vector of values assigned to the future slot of the Forecast. |
Forecast
object that contains the new future vector.
## Not run: future(Forecast) <- c("2015-03-01", "2015-03-02", "2015-03-03") ## End(Not run)
## Not run: future(Forecast) <- c("2015-03-01", "2015-03-02", "2015-03-03") ## End(Not run)
future
takes a Forecast
object and sets the future
vector of the forecast.
## S4 replacement method for signature 'Forecast' future(Forecast) <- value
## S4 replacement method for signature 'Forecast' future(Forecast) <- value
Forecast |
Forecast object. |
value |
Vector of values assigned to the future slot of the Forecast. |
Forecast
object that contains the new future vector.
## Not run: future(Forecast) <- c("2015-03-01", "2015-03-02", "2015-03-03") ## End(Not run)
## Not run: future(Forecast) <- c("2015-03-01", "2015-03-02", "2015-03-03") ## End(Not run)
h_ahead
takes a Forecast
object and gets the h_ahead
vector of the forecast.
h_ahead(Forecast)
h_ahead(Forecast)
Forecast |
Forecast object. |
Vector of h_ahead values stored in the Forecast object.
## Not run: h_ahead(Forecast) ## End(Not run)
## Not run: h_ahead(Forecast) ## End(Not run)
h_ahead
takes a Forecast
object and gets the h_ahead
vector of the forecast.
## S4 method for signature 'Forecast' h_ahead(Forecast)
## S4 method for signature 'Forecast' h_ahead(Forecast)
Forecast |
Forecast object. |
Vector of h_ahead values stored in the Forecast object.
## Not run: h_ahead(Forecast) ## End(Not run)
## Not run: h_ahead(Forecast) ## End(Not run)
h_ahead
takes a Forecast
object and sets the h_ahead
vector of the forecast.
h_ahead(Forecast) <- value
h_ahead(Forecast) <- value
Forecast |
Forecast object. |
value |
Vector of values assigned to the h_ahead slot of the Forecast. |
Forecast
object that contains the new h_ahead vector.
## Not run: h_ahead(Forecast) <- 4L ## End(Not run)
## Not run: h_ahead(Forecast) <- 4L ## End(Not run)
h_ahead
takes a Forecast
object and sets the h_ahead
vector of the forecast.
## S4 replacement method for signature 'Forecast' h_ahead(Forecast) <- value
## S4 replacement method for signature 'Forecast' h_ahead(Forecast) <- value
Forecast |
Forecast object. |
value |
Vector of values assigned to the h_ahead slot of the Forecast. |
Forecast
object that contains the new h_ahead vector.
## Not run: h_ahead(Forecast) <- 4L ## End(Not run)
## Not run: h_ahead(Forecast) <- 4L ## End(Not run)
historical_average_forc
takes an average function, a vector of realized
values, an integer number of periods ahead to forecast, a period to end the initial
average estimation and begin forecasting, an optional vector of time data
associated with the realized values, and an optional integer number of past
periods to estimate the average over. The historical average is originally
calculated with realized values up to estimation_end
minus the number
of periods specified in estimation_window
. If estimation_window
is left NULL
then the historical average is calculated with all available
realized values up to estimation_end
. In each period the historical
average is set as the h_ahead
period ahead forecast. This process is
iteratively repeated for each period after estimation_end
with the
historical average updating in each period as more information would have
become available to the forecaster. Returns a historical average forecast where
the h_ahead
period ahead forecast is simply the historical average or
rolling window average of the series being forecasted.
historical_average_forc( avg_function, realized_vec, h_ahead, estimation_end, time_vec = NULL, estimation_window = NULL )
historical_average_forc( avg_function, realized_vec, h_ahead, estimation_end, time_vec = NULL, estimation_window = NULL )
avg_function |
Character, either "mean" or "median". Selects whether forecasts are made using the historical mean or historical median of the series. |
realized_vec |
Vector of realized values. This is the series that is being forecasted. |
h_ahead |
Integer representing the number of periods ahead that is being forecasted. |
estimation_end |
Value of any class representing when to end the initial average estimation period and begin forecasting. |
time_vec |
Vector of any class that is equal in length to the
|
estimation_window |
Integer representing the number of past periods that the historical average should be estimated over in each period. |
Forecast
object that contains the historical average
forecast.
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99) data <- data.frame(date, y) historical_average_forc( avg_function = "mean", realized_vec = data$y, h_ahead = 2L, estimation_end = as.Date("2011-03-31"), time_vec = data$date, estimation_window = 4L ) historical_average_forc( avg_function = "median", realized_vec = data$y, h_ahead = 4L, estimation_end = 4L )
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99) data <- data.frame(date, y) historical_average_forc( avg_function = "mean", realized_vec = data$y, h_ahead = 2L, estimation_end = as.Date("2011-03-31"), time_vec = data$date, estimation_window = 4L ) historical_average_forc( avg_function = "median", realized_vec = data$y, h_ahead = 4L, estimation_end = 4L )
is_forc
takes a linear model call and an optional vector of time
data associated with the linear model. The linear model is estimated once
over the entire sample period and the coefficients are multiplied by the
realized values in each period of the sample. Returns an in-sample forecast
conditional on realized values.
is_forc(lm_call, time_vec = NULL)
is_forc(lm_call, time_vec = NULL)
lm_call |
Linear model call of the class lm. |
time_vec |
Vector of any class that is equal in length to the data
in |
Forecast
object that contains the in-sample forecast.
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99) x1 <- c(4.22, 3.86, 4.27, 5.60, 5.11, 4.31, 4.92, 5.80, 6.30, 4.17) x2 <- c(10.03, 10.49, 10.85, 10.47, 9.09, 10.91, 8.68, 9.91, 7.87, 6.63) data <- data.frame(date, y, x1, x2) is_forc( lm_call = lm(y ~ x1 + x2, data), time_vec = data$date ) is_forc( lm_call = lm(y ~ x1 + x2, data) )
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99) x1 <- c(4.22, 3.86, 4.27, 5.60, 5.11, 4.31, 4.92, 5.80, 6.30, 4.17) x2 <- c(10.03, 10.49, 10.85, 10.47, 9.09, 10.91, 8.68, 9.91, 7.87, 6.63) data <- data.frame(date, y, x1, x2) is_forc( lm_call = lm(y ~ x1 + x2, data), time_vec = data$date ) is_forc( lm_call = lm(y ~ x1 + x2, data) )
is_forc_general
takes a model function, a prediction function, input
data for estimating the model, realized values of the dependent variable,
and an optional vector of time data associated with the model. The model is
estimated once over the entire sample period using the input data and model function.
Model parameters are then combined with the input data using the prediction function
to generate in-sample forecasts. Returns an in-sample forecast conditional on realized values.
is_forc_general(model_function, prediction_function, data, realized, time_vec)
is_forc_general(model_function, prediction_function, data, realized, time_vec)
model_function |
Function that estimates a model using the |
prediction_function |
Function that generates model predictions using |
data |
Input data for estimating the model. |
realized |
Vector of realized values of the dependent variable equal in length to the data in |
time_vec |
Vector of any class that represents time and is equal in length to the length of |
Forecast
object that contains the in-sample forecast.
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) y <- c(1, 0, 0, 0, 1, 1, 0, 0, 0, 1) x1 <- c(8.22, 3.86, 4.27, 3.37, 5.88, 3.34, 2.92, 1.80, 3.30, 7.17) x2 <- c(4.03, 2.46, 2.04, 2.44, 6.09, 2.91, 1.68, 2.91, 3.87, 1.63) dataLogit <- data.frame(date, y, x1, x2) is_forc_general( model_function = function(data) {glm(y ~ x1 + x2, data = data, family = binomial)}, prediction_function = function(model_function, data) { as.vector(predict(model_function, data, type = "response")) }, data = dataLogit, realized = dataLogit$y, time_vec = dataLogit$date )
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) y <- c(1, 0, 0, 0, 1, 1, 0, 0, 0, 1) x1 <- c(8.22, 3.86, 4.27, 3.37, 5.88, 3.34, 2.92, 1.80, 3.30, 7.17) x2 <- c(4.03, 2.46, 2.04, 2.44, 6.09, 2.91, 1.68, 2.91, 3.87, 1.63) dataLogit <- data.frame(date, y, x1, x2) is_forc_general( model_function = function(data) {glm(y ~ x1 + x2, data = data, family = binomial)}, prediction_function = function(model_function, data) { as.vector(predict(model_function, data, type = "response")) }, data = dataLogit, realized = dataLogit$y, time_vec = dataLogit$date )
mae
takes a Forecast
object and returns the MAE of the
forecast. MAE is calculated as:
1/length(forecast) * sum(abs(forecast - realized))
mae(Forecast)
mae(Forecast)
Forecast |
Forecast object. |
MAE value.
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) mae(my_forecast)
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) mae(my_forecast)
mae
takes a Forecast
object and returns the MAE of the
forecast. MAE is calculated as:
1/length(forecast) * sum(abs(forecast - realized))
## S4 method for signature 'Forecast' mae(Forecast)
## S4 method for signature 'Forecast' mae(Forecast)
Forecast |
Forecast object. |
MAE value.
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) mae(my_forecast)
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) mae(my_forecast)
mape
takes a Forecast
object and returns the MAPE of the
forecast. MAPE is calculated as:
1/length(forecast) * sum(abs(realized - forecast) / realized)
mape(Forecast)
mape(Forecast)
Forecast |
Forecast object. |
MAPE value.
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) mape(my_forecast)
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) mape(my_forecast)
mape
takes a Forecast
object and returns the MAPE of the
forecast. MAPE is calculated as:
1/length(forecast) * sum(abs(realized - forecast) / realized)
## S4 method for signature 'Forecast' mape(Forecast)
## S4 method for signature 'Forecast' mape(Forecast)
Forecast |
Forecast object. |
MAPE value.
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) mape(my_forecast)
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) mape(my_forecast)
mse
takes a Forecast
object and returns the MSE of the
forecast. MSE is calculated as:
1/length(forecast) * sum((realized - forecast)^2)
mse(Forecast)
mse(Forecast)
Forecast |
Forecast object. |
MSE value.
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) mse(my_forecast)
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) mse(my_forecast)
mse
takes a Forecast
object and returns the MSE of the
forecast. MSE is calculated as:
1/length(forecast) * sum((realized - forecast)^2)
## S4 method for signature 'Forecast' mse(Forecast)
## S4 method for signature 'Forecast' mse(Forecast)
Forecast |
Forecast object. |
MSE value.
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) mse(my_forecast)
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) mse(my_forecast)
oos_lag_forc
takes a linear model call, an integer number of
periods ahead to forecast, a period to end the initial coefficient estimation
and begin forecasting, an optional vector of time data associated with
the linear model, and an optional integer number of past periods to estimate
the linear model over. Linear model data is lagged by h_ahead
periods
and the linear model is re-estimated with data up to estimation_end
minus the number of periods specified in estimation_window
to create
a lagged linear model. If estimation_window
is left NULL
then the linear model is estimated with all available data up to
estimation_end
. Coefficients are multiplied by present period
realized values of the covariates to create a forecast for h_ahead
periods ahead. This process is iteratively repeated for each period after
estimation_end
with coefficients updating in each period. Returns an
out-of-sample forecast conditional on realized values that would
have been available at the forecast origin. Optionally returns the coefficients
used to create each forecast. Tests the out-of-sample performance of a linear
model had it been lagged and conditioned on available information.
oos_lag_forc( lm_call, h_ahead, estimation_end, time_vec = NULL, estimation_window = NULL, return_betas = FALSE )
oos_lag_forc( lm_call, h_ahead, estimation_end, time_vec = NULL, estimation_window = NULL, return_betas = FALSE )
lm_call |
Linear model call of the class lm. |
h_ahead |
Integer representing the number of periods ahead that is being forecasted. |
estimation_end |
Value of any class representing when to end the initial coefficient estimation period and begin forecasting. |
time_vec |
Vector of any class that is equal in length to the data
in |
estimation_window |
Integer representing the number of past periods that the linear model should be estimated over in each period. |
return_betas |
Boolean, selects whether the coefficients used in each period to create the forecast are returned. If TRUE, a data frame of betas is returned to the Global Environment. |
Forecast
object that contains the out-of-sample
forecast.
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99) x1 <- c(4.22, 3.86, 4.27, 5.60, 5.11, 4.31, 4.92, 5.80, 6.30, 4.17) x2 <- c(10.03, 10.49, 10.85, 10.47, 9.09, 10.91, 8.68, 9.91, 7.87, 6.63) data <- data.frame(date, y, x1, x2) oos_lag_forc( lm_call = lm(y ~ x1 + x2, data), h_ahead = 2L, estimation_end = as.Date("2011-03-31"), time_vec = data$date, estimation_window = NULL, return_betas = FALSE ) oos_lag_forc( lm_call = lm(y ~ x1 + x2, data), h_ahead = 2L, estimation_end = 6L )
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99) x1 <- c(4.22, 3.86, 4.27, 5.60, 5.11, 4.31, 4.92, 5.80, 6.30, 4.17) x2 <- c(10.03, 10.49, 10.85, 10.47, 9.09, 10.91, 8.68, 9.91, 7.87, 6.63) data <- data.frame(date, y, x1, x2) oos_lag_forc( lm_call = lm(y ~ x1 + x2, data), h_ahead = 2L, estimation_end = as.Date("2011-03-31"), time_vec = data$date, estimation_window = NULL, return_betas = FALSE ) oos_lag_forc( lm_call = lm(y ~ x1 + x2, data), h_ahead = 2L, estimation_end = 6L )
oos_realized_forc
takes a linear model call, an integer number of
periods ahead to forecast, a period to end the initial coefficient estimation
and begin forecasting, an optional vector of time data associated with
the linear model, and an optional integer number of past periods to estimate
the linear model over. The linear model is originally estimated with data up
to estimation_end
minus the number of periods specified in
estimation_window
. If estimation_window
is left NULL
then the linear model is estimated with all available data up to
estimation_end
. Coefficients are multiplied by realized values of the
covariates h_ahead
periods ahead to create an h_ahead
period
ahead forecast. This process is iteratively repeated for each period after
estimation_end
with coefficients updating in each period. Returns an
out-of-sample forecast conditional on realized values that would not
have been available at the forecast origin. Optionally returns the coefficients
used to create each forecast. Tests the out-of-sample performance of a linear
model had it been conditioned on perfect information.
oos_realized_forc( lm_call, h_ahead, estimation_end, time_vec = NULL, estimation_window = NULL, return_betas = FALSE )
oos_realized_forc( lm_call, h_ahead, estimation_end, time_vec = NULL, estimation_window = NULL, return_betas = FALSE )
lm_call |
Linear model call of the class lm. |
h_ahead |
Integer representing the number of periods ahead that is being forecasted. |
estimation_end |
Value of any class representing when to end the initial coefficient estimation period and begin forecasting. |
time_vec |
Vector of any class that is equal in length to the data
in |
estimation_window |
Integer representing the number of past periods that the linear model should be estimated over in each period. |
return_betas |
Boolean, selects whether the coefficients used in each period to create the forecast are returned. If TRUE, a data frame of betas is returned to the Global Environment. |
Forecast
object that contains the out-of-sample
forecast.
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99) x1 <- c(4.22, 3.86, 4.27, 5.60, 5.11, 4.31, 4.92, 5.80, 6.30, 4.17) x2 <- c(10.03, 10.49, 10.85, 10.47, 9.09, 10.91, 8.68, 9.91, 7.87, 6.63) data <- data.frame(date, y, x1, x2) oos_realized_forc( lm_call = lm(y ~ x1 + x2, data), h_ahead = 2L, estimation_end = as.Date("2011-03-31"), time_vec = data$date, estimation_window = NULL, return_betas = FALSE )
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99) x1 <- c(4.22, 3.86, 4.27, 5.60, 5.11, 4.31, 4.92, 5.80, 6.30, 4.17) x2 <- c(10.03, 10.49, 10.85, 10.47, 9.09, 10.91, 8.68, 9.91, 7.87, 6.63) data <- data.frame(date, y, x1, x2) oos_realized_forc( lm_call = lm(y ~ x1 + x2, data), h_ahead = 2L, estimation_end = as.Date("2011-03-31"), time_vec = data$date, estimation_window = NULL, return_betas = FALSE )
oos_realized_forc
takes a model function, a prediction function, input
data for estimating the model, realized values of the dependent variable, an
integer number of periods ahead to forecast, a period to end the initial coefficient
estimation and begin forecasting, a vector of time data associated with the model,
and an optional integer number of past periods to estimate the model over. The
model is originally estimated using the input data and model function with data
up to estimation_end
minus the the number of periods specified in estimation_window
.
If estimation_window
is left NULL
then the model is estimated with all
available data up to estimation_end
. Model parameters are then combined with realized
values of the input data h_ahead
periods ahead to generate an h_ahead
period
ahead forecast. This process is iteratively repeated for each period after estimation_end
with model parameters updating in each period. Returns an out-of-sample forecast
conditional on realized values that would not have been available at the forecast origin.
Tests the out-of-sample performance of a model had it been conditioned on perfect information.
oos_realized_forc_general( model_function, prediction_function, data, realized, h_ahead, estimation_end, time_vec, estimation_window = NULL )
oos_realized_forc_general( model_function, prediction_function, data, realized, h_ahead, estimation_end, time_vec, estimation_window = NULL )
model_function |
Function that estimates a model using the |
prediction_function |
Function that generates model predictions using |
data |
Input data for estimating the model. |
realized |
Vector of realized values of the dependent variable equal in length
to the data in |
h_ahead |
Integer representing the number of periods ahead that is being forecasted. |
estimation_end |
Value of any class representing when to end the initial coefficient estimation period and begin forecasting. |
time_vec |
Vector of any class that represents time and is equal in length to
the length of |
estimation_window |
Integer representing the number of past periods that the linear model should be estimated over in each period. |
Forecast
object that contains the out-of-sample
forecast.
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30", "2012-09-30", "2012-12-31", "2013-03-31", "2013-06-30", "2013-09-30", "2013-12-31")) y <- c(1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0) x1 <- c(8.22, 3.86, 4.27, 3.37, 5.88, 3.34, 2.92, 1.80, 3.30, 7.17, 3.22, 3.86, 4.27, 3.37, 5.88, 3.34) x2 <- c(4.03, 2.46, 2.04, 2.44, 6.09, 2.91, 1.68, 2.91, 3.87, 1.63, 4.03, 2.46, 2.04, 2.44, 6.09, 2.91) dataLogit <- data.frame(date, y, x1, x2) forc <- oos_realized_forc_general( model_function = function(data) {glm(y ~ x1 + x2, data = data, family = binomial)}, prediction_function = function(model_function, data) { as.vector(predict(model_function, data, type = "response")) }, data = dataLogit, realized = dataLogit$y, h_ahead = 2L, estimation_end = as.Date("2012-06-30"), time_vec = dataLogit$date, estimation_window = NULL )
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30", "2012-09-30", "2012-12-31", "2013-03-31", "2013-06-30", "2013-09-30", "2013-12-31")) y <- c(1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0) x1 <- c(8.22, 3.86, 4.27, 3.37, 5.88, 3.34, 2.92, 1.80, 3.30, 7.17, 3.22, 3.86, 4.27, 3.37, 5.88, 3.34) x2 <- c(4.03, 2.46, 2.04, 2.44, 6.09, 2.91, 1.68, 2.91, 3.87, 1.63, 4.03, 2.46, 2.04, 2.44, 6.09, 2.91) dataLogit <- data.frame(date, y, x1, x2) forc <- oos_realized_forc_general( model_function = function(data) {glm(y ~ x1 + x2, data = data, family = binomial)}, prediction_function = function(model_function, data) { as.vector(predict(model_function, data, type = "response")) }, data = dataLogit, realized = dataLogit$y, h_ahead = 2L, estimation_end = as.Date("2012-06-30"), time_vec = dataLogit$date, estimation_window = NULL )
oos_vintage_forc
takes a linear model call, a vector of time data
associated with the linear model, a forecast for each covariate in the
linear model, and an optional integer number of past periods to estimate the
linear model over. For each period in the vintage forecasts, coefficients are
estimated with data up to the current period minus the number of periods
specified in estimation_window
. If estimation_window
is left
NULL
then the linear model is estimated with all available data up to
the current period. Coefficients are then multiplied by vintage forecast values.
Returns an out-of-sample forecast conditional on vintage forecasts that
would have been available at the forecast origin. Optionally returns
the coefficients used to create each forecast. Replicates the forecasts that a
linear model would have produced in real time.
oos_vintage_forc( lm_call, time_vec, ..., estimation_window = NULL, return_betas = FALSE )
oos_vintage_forc( lm_call, time_vec, ..., estimation_window = NULL, return_betas = FALSE )
lm_call |
Linear model call of the class lm. |
time_vec |
Vector of any class that is equal in length to the data
in |
... |
Set of forecasts of class Forecast, one forecast for each covariate in the linear model. |
estimation_window |
Integer representing the number of past periods that the linear model should be estimated over in each period. |
return_betas |
Boolean, selects whether the coefficients used in each period to create the forecast are returned. If TRUE, a data frame of betas is returned to the Global Environment. |
Forecast
object that contains the out-of-sample
forecast.
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99) x1 <- c(4.22, 3.86, 4.27, 5.60, 5.11, 4.31, 4.92, 5.80, 6.30, 4.17) x2 <- c(10.03, 10.49, 10.85, 10.47, 9.09, 10.91, 8.68, 9.91, 7.87, 6.63) data <- data.frame(date, y, x1, x2) x1_forecast_vintage <- Forecast( origin = as.Date(c("2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), future = as.Date(c("2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")), forecast = c(6.30, 4.17, 5.30, 4.84), realized = c(4.92, 5.80, 6.30, 4.17), h_ahead = 4L ) x2_forecast_vintage <- Forecast( origin = as.Date(c("2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), future = as.Date(c("2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")), forecast = c(7.32, 6.88, 6.82, 6.95), realized = c(8.68, 9.91, 7.87, 6.63), h_ahead = 4L ) oos_vintage_forc( lm_call = lm(y ~ x1 + x2, data), time_vec = data$date, x1_forecast_vintage, x2_forecast_vintage, estimation_window = 4L, return_betas = FALSE ) oos_vintage_forc( lm_call = lm(y ~ x1 + x2, data), time_vec = data$date, x1_forecast_vintage, x2_forecast_vintage )
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99) x1 <- c(4.22, 3.86, 4.27, 5.60, 5.11, 4.31, 4.92, 5.80, 6.30, 4.17) x2 <- c(10.03, 10.49, 10.85, 10.47, 9.09, 10.91, 8.68, 9.91, 7.87, 6.63) data <- data.frame(date, y, x1, x2) x1_forecast_vintage <- Forecast( origin = as.Date(c("2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), future = as.Date(c("2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")), forecast = c(6.30, 4.17, 5.30, 4.84), realized = c(4.92, 5.80, 6.30, 4.17), h_ahead = 4L ) x2_forecast_vintage <- Forecast( origin = as.Date(c("2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), future = as.Date(c("2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")), forecast = c(7.32, 6.88, 6.82, 6.95), realized = c(8.68, 9.91, 7.87, 6.63), h_ahead = 4L ) oos_vintage_forc( lm_call = lm(y ~ x1 + x2, data), time_vec = data$date, x1_forecast_vintage, x2_forecast_vintage, estimation_window = 4L, return_betas = FALSE ) oos_vintage_forc( lm_call = lm(y ~ x1 + x2, data), time_vec = data$date, x1_forecast_vintage, x2_forecast_vintage )
oos_vintage_forc_general
takes a model function, a prediction function,
input data for estimating the model, realized values of the dependent variable,
a vector of time data associated with the model, a forecast for each parameter
in the model, and an optional integer number of past periods to estimate the
model over. For each period in the vintage forecasts, model parametes are
estimated with data up to the current period minus the number of periods
specified in estimation_window
. If estimation_window
is left
NULL
then the model is estimated with all available data up to the
current period. Model parameters are then combined with vintage forecast values
to generate a forecast. Returns an out-of-sample forecast conditional on vintage
forecasts that would have been available at the forecast origin. Replicates
the forecasts that a conditional forecasting model would have produced in real time.
oos_vintage_forc_general( model_function, prediction_function, data, realized, time_vec, ..., estimation_window = NULL )
oos_vintage_forc_general( model_function, prediction_function, data, realized, time_vec, ..., estimation_window = NULL )
model_function |
Function that estimates a model using the |
prediction_function |
Function that generates model predictions using |
data |
Input data for estimating the model. |
realized |
Vector of realized values of the dependent variable equal in length
to the data in |
time_vec |
Vector of any class that represents time and is equal in length to
the length of |
... |
Set of forecasts of class Forecast, one forecast for each parameter in the linear model. |
estimation_window |
Integer representing the number of past periods that the linear model should be estimated over in each period. |
Forecast
object that contains the out-of-sample
forecast.
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
# Estimation Data. date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30", "2012-09-30", "2012-12-31", "2013-03-31", "2013-06-30", "2013-09-30", "2013-12-31")) y <- c(1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0) x1 <- c(8.22, 3.86, 4.27, 3.37, 5.88, 3.34, 2.92, 1.80, 3.30, 7.17, 3.22, 3.86, 4.27, 3.37, 5.88, 3.34) x2 <- c(4.03, 2.46, 2.04, 2.44, 6.09, 2.91, 1.68, 2.91, 3.87, 1.63, 4.03, 2.46, 2.04, 2.44, 6.09, 2.91) dataLogit <- data.frame(date, y, x1, x2) # Vintage Forecasts. x1_forecast_vintageLogit <- Forecast( origin = as.Date(c("2012-09-30", "2012-12-31", "2013-03-31", "2013-06-30")), future = as.Date(c("2013-09-30", "2013-12-31", "2014-03-31", "2014-06-30")), forecast = c(6.34, 4.17, 2.98, 1.84), realized = c(5.88, 3.34, 2.92, 1.80), h_ahead = 4L ) x2_forecast_vintageLogit <- Forecast( origin = as.Date(c("2012-09-30", "2012-12-31", "2013-03-31", "2013-06-30")), future = as.Date(c("2013-09-30", "2013-12-31", "2014-03-31", "2014-06-30")), forecast = c(7.32, 3.22, 2.21, 2.65), realized = c(6.09, 2.91, 1.68, 2.91), h_ahead = 4L ) # Forecasting function. oos_vintage_forc_general( model_function = function(data) {glm(y ~ x1 + x2, data = data, family = binomial)}, prediction_function = function(model_function, data) { names(data) <- c("x1", "x2") as.vector(predict(model_function, data, type = "response")) }, data = dataLogit, realized = dataLogit$y, time_vec = dataLogit$date, x1_forecast_vintageLogit, x2_forecast_vintageLogit, estimation_window = NULL )
# Estimation Data. date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30", "2012-09-30", "2012-12-31", "2013-03-31", "2013-06-30", "2013-09-30", "2013-12-31")) y <- c(1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0) x1 <- c(8.22, 3.86, 4.27, 3.37, 5.88, 3.34, 2.92, 1.80, 3.30, 7.17, 3.22, 3.86, 4.27, 3.37, 5.88, 3.34) x2 <- c(4.03, 2.46, 2.04, 2.44, 6.09, 2.91, 1.68, 2.91, 3.87, 1.63, 4.03, 2.46, 2.04, 2.44, 6.09, 2.91) dataLogit <- data.frame(date, y, x1, x2) # Vintage Forecasts. x1_forecast_vintageLogit <- Forecast( origin = as.Date(c("2012-09-30", "2012-12-31", "2013-03-31", "2013-06-30")), future = as.Date(c("2013-09-30", "2013-12-31", "2014-03-31", "2014-06-30")), forecast = c(6.34, 4.17, 2.98, 1.84), realized = c(5.88, 3.34, 2.92, 1.80), h_ahead = 4L ) x2_forecast_vintageLogit <- Forecast( origin = as.Date(c("2012-09-30", "2012-12-31", "2013-03-31", "2013-06-30")), future = as.Date(c("2013-09-30", "2013-12-31", "2014-03-31", "2014-06-30")), forecast = c(7.32, 3.22, 2.21, 2.65), realized = c(6.09, 2.91, 1.68, 2.91), h_ahead = 4L ) # Forecasting function. oos_vintage_forc_general( model_function = function(data) {glm(y ~ x1 + x2, data = data, family = binomial)}, prediction_function = function(model_function, data) { names(data) <- c("x1", "x2") as.vector(predict(model_function, data, type = "response")) }, data = dataLogit, realized = dataLogit$y, time_vec = dataLogit$date, x1_forecast_vintageLogit, x2_forecast_vintageLogit, estimation_window = NULL )
origin
takes a Forecast
object and gets the origin
vector of the forecast.
origin(Forecast)
origin(Forecast)
Forecast |
Forecast object. |
Vector of origin values stored in the Forecast object.
## Not run: origin(Forecast) ## End(Not run)
## Not run: origin(Forecast) ## End(Not run)
origin
takes a Forecast
object and gets the origin
vector of the forecast.
## S4 method for signature 'Forecast' origin(Forecast)
## S4 method for signature 'Forecast' origin(Forecast)
Forecast |
Forecast object. |
Vector of origin values stored in the Forecast object.
## Not run: origin(Forecast) ## End(Not run)
## Not run: origin(Forecast) ## End(Not run)
origin
takes a Forecast
object and sets the origin
vector of the forecast.
origin(Forecast) <- value
origin(Forecast) <- value
Forecast |
Forecast object. |
value |
Vector of values assigned to the origin slot of the Forecast. |
Forecast
object that contains the new origin vector.
## Not run: origin(Forecast) <- c("2015-01-01", "2015-01-02", "2015-01-03") ## End(Not run)
## Not run: origin(Forecast) <- c("2015-01-01", "2015-01-02", "2015-01-03") ## End(Not run)
origin
takes a Forecast
object and sets the origin
vector of the forecast.
## S4 replacement method for signature 'Forecast' origin(Forecast) <- value
## S4 replacement method for signature 'Forecast' origin(Forecast) <- value
Forecast |
Forecast object. |
value |
Vector of values assigned to the origin slot of the Forecast. |
Forecast
object that contains the new origin vector.
## Not run: origin(Forecast) <- c("2015-01-01", "2015-01-02", "2015-01-03") ## End(Not run)
## Not run: origin(Forecast) <- c("2015-01-01", "2015-01-02", "2015-01-03") ## End(Not run)
performance_weighted_forc
takes two or more forecasts, an evaluation window,
and an error function. For each forecast period, the error function is used
to calculate forecast accuracy over the past eval_window
number of
periods. The forecast accuracy of each forecast is used to weight forecasts
based on performance. Returns a weighted forecast. Optionally returns the set
of weights used to weight forecasts in each period.
performance_weighted_forc( ..., eval_window, errors = "mse", return_weights = FALSE )
performance_weighted_forc( ..., eval_window, errors = "mse", return_weights = FALSE )
... |
Two or more forecasts of class Forecast. |
eval_window |
Integer representing the window over which forecast
accuracy is evaluated. Forecasts are weighted based on their accuracy over
the past |
errors |
Character, either "mse", "rmse", "mae", or "mape". Selects what forecast accuracy function is used to evaluate forecast errors. |
return_weights |
Boolean, selects whether the weights used to weight forecasts in each period are returned. If TRUE, a data frame of weights is returned to the Global Environment. |
Forecasts are weighted in each period with the following function. The error function used is MSE or RMSE depending on user selection. This example shows MSE errors.
Forecast
object that contains the weighted forecast.
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
y1_forecast <- Forecast( origin = as.Date(c("2009-03-31", "2009-06-30", "2009-09-30", "2009-12-31", "2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), future = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")), forecast = c(1.33, 1.36, 1.38, 1.68, 1.60, 1.55, 1.32, 1.22, 1.08, 0.88), realized = c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99), h_ahead = 4L ) y2_forecast <- Forecast( origin = as.Date(c("2009-03-31", "2009-06-30", "2009-09-30", "2009-12-31", "2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), future = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")), forecast = c(0.70, 0.88, 1.03, 1.05, 1.01, 0.82, 0.95, 1.09, 1.07, 1.06), realized = c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99), h_ahead = 4L ) performance_weighted_forc( y1_forecast, y2_forecast, eval_window = 2L, errors = "mse", return_weights = FALSE )
y1_forecast <- Forecast( origin = as.Date(c("2009-03-31", "2009-06-30", "2009-09-30", "2009-12-31", "2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), future = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")), forecast = c(1.33, 1.36, 1.38, 1.68, 1.60, 1.55, 1.32, 1.22, 1.08, 0.88), realized = c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99), h_ahead = 4L ) y2_forecast <- Forecast( origin = as.Date(c("2009-03-31", "2009-06-30", "2009-09-30", "2009-12-31", "2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), future = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")), forecast = c(0.70, 0.88, 1.03, 1.05, 1.01, 0.82, 0.95, 1.09, 1.07, 1.06), realized = c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99), h_ahead = 4L ) performance_weighted_forc( y1_forecast, y2_forecast, eval_window = 2L, errors = "mse", return_weights = FALSE )
R2
takes a Forecast
object and returns the R2 of the
forecast. R2 is calculated as:
cor(forecast, realized)^2
R2(Forecast)
R2(Forecast)
Forecast |
Forecast object. |
R2 value.
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) R2(my_forecast)
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) R2(my_forecast)
R2
takes a Forecast
object and returns the R2 of the
forecast. R2 is calculated as:
cor(forecast, realized)^2
## S4 method for signature 'Forecast' R2(Forecast)
## S4 method for signature 'Forecast' R2(Forecast)
Forecast |
Forecast object. |
R2 value.
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) R2(my_forecast)
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) R2(my_forecast)
random_walk_forc
takes a vector of realized values, an integer
number of periods ahead to forecast, and an optional vector of time data
associated with the realized values. In each period, the current period value
of the realized_vec
series is set as the h_ahead
period ahead forecast.
Returns a random walk forecast where the h_ahead
period ahead forecast
is simply the present value of the series being forecasted.
random_walk_forc(realized_vec, h_ahead, time_vec = NULL)
random_walk_forc(realized_vec, h_ahead, time_vec = NULL)
realized_vec |
Vector of realized values. This is the series that is being forecasted. |
h_ahead |
Integer representing the number of periods ahead that is being forecasted. |
time_vec |
Vector of any class that is equal in length to the
|
Forecast
object that contains the random walk forecast.
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99) data <- data.frame(date, y) random_walk_forc( realized_vec = data$y, h_ahead = 4L, time_vec = data$date )
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99) data <- data.frame(date, y) random_walk_forc( realized_vec = data$y, h_ahead = 4L, time_vec = data$date )
realized
takes a Forecast
object and gets the realized
vector of the forecast.
realized(Forecast)
realized(Forecast)
Forecast |
Forecast object. |
Vector of realized values stored in the Forecast object.
## Not run: realized(Forecast) ## End(Not run)
## Not run: realized(Forecast) ## End(Not run)
realized
takes a Forecast
object and gets the realized
vector of the forecast.
## S4 method for signature 'Forecast' realized(Forecast)
## S4 method for signature 'Forecast' realized(Forecast)
Forecast |
Forecast object. |
Vector of realized values stored in the Forecast object.
## Not run: realized(Forecast) ## End(Not run)
## Not run: realized(Forecast) ## End(Not run)
realized
takes a Forecast
object and sets the realized
vector of the forecast.
realized(Forecast) <- value
realized(Forecast) <- value
Forecast |
Forecast object. |
value |
Vector of values assigned to the realized slot of the Forecast. |
Forecast
object that contains the new realized vector.
## Not run: realized(Forecast) <- c("2015-03-01", "2015-03-02", "2015-03-03") ## End(Not run)
## Not run: realized(Forecast) <- c("2015-03-01", "2015-03-02", "2015-03-03") ## End(Not run)
realized
takes a Forecast
object and sets the realized
vector of the forecast.
## S4 replacement method for signature 'Forecast' realized(Forecast) <- value
## S4 replacement method for signature 'Forecast' realized(Forecast) <- value
Forecast |
Forecast object. |
value |
Vector of values assigned to the realized slot of the Forecast. |
Forecast
object that contains the new realized vector.
## Not run: realized(Forecast) <- c("2015-03-01", "2015-03-02", "2015-03-03") ## End(Not run)
## Not run: realized(Forecast) <- c("2015-03-01", "2015-03-02", "2015-03-03") ## End(Not run)
rmse
takes a Forecast
object and returns the RMSE of the
forecast. RMSE is calculated as: sqrt(mse)
rmse(Forecast)
rmse(Forecast)
Forecast |
Forecast object. |
RMSE value.
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) rmse(my_forecast)
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) rmse(my_forecast)
rmse
takes a Forecast
object and returns the RMSE of the
forecast. RMSE is calculated as: sqrt(mse)
## S4 method for signature 'Forecast' rmse(Forecast)
## S4 method for signature 'Forecast' rmse(Forecast)
Forecast |
Forecast object. |
RMSE value.
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) rmse(my_forecast)
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) rmse(my_forecast)
show
takes a Forecast
object and prints it to console.
## S4 method for signature 'Forecast' show(object)
## S4 method for signature 'Forecast' show(object)
object |
Forecast object. |
Printed Forecast
object.
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) print(my_forecast)
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) print(my_forecast)
states_weighted_forc
takes two or more forecasts, a data frame, matrix,
or array of matching variables, an optional vector of time data associated with
the matching variables, a matching window size, a matching function, and an
error function. For each forecast period, matching_vars
are standardized
and the current state of the world is set as the the past matching_window
periods of the matching variables. The current state is compared to all past
periods of the matching variables using the matching function. The current state
is matched to the past state that minimizes the matching function. The forecast
error function is then used to compute the accuracy of each forecast over the matched
past state. Forecast weights are computed based on this forecast accuracy,
and the current period forecast is subsequently computed based on the forecast
weights. Produces a weighted average of multiple forecasts based on how each
forecast performed during the past state that is most similar to the current
state of the world.
states_weighted_forc( ..., matching_vars, time_vec = NULL, matching_window, matching = "euclidean", errors = "mse", return_weights = FALSE )
states_weighted_forc( ..., matching_vars, time_vec = NULL, matching_window, matching = "euclidean", errors = "mse", return_weights = FALSE )
... |
Two or more forecasts of class Forecast. |
matching_vars |
data frame, array, or matrix of variables used to match the current state of the world to a past state. |
time_vec |
Vector of any class that is equal in length to the data
in |
matching_window |
Integer representing the window size over which the
current state of the world is matched to a past state. Forecasts are also
weighted based on their accuracy over |
matching |
Character, "euclidean", "mse", or "rmse". Selects the function used to match the current state of the world to a past state. |
errors |
Character, either "mse", "rmse", "mae", or "mape". Selects what forecast accuracy function is used to evaluate forecast errors. |
return_weights |
Boolean, selects whether the weights used to weight forecasts in each period are returned. If TRUE, a data frame of weights and matched periods is returned to the Global Environment. |
Forecasts are weighted in each period with the function below. The error function used is MSE or RMSE depending on user selection. This example shows MSE errors.
Forecast
object that contains the state weighted forecast.
For a detailed example see the help vignette:
vignette("lmForc", package = "lmForc")
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) future <- as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30", "2012-09-30", "2012-12-31", "2013-03-31", "2013-06-30")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99) x1 <- c(4.22, 3.86, 4.27, 5.60, 5.11, 4.31, 4.92, 5.80, 6.30, 4.17) x2 <- c(10.03, 10.49, 10.85, 10.47, 9.09, 10.91, 8.68, 9.91, 7.87, 6.63) data <- data.frame(date, y, x1, x2) matching_vars <- data[, c("x1", "x2")] y1_forecast <- Forecast( origin = date, future = future, forecast = c(1.33, 1.36, 1.38, 1.68, 1.60, 1.55, 1.32, 1.22, 1.08, 0.88), realized = c(1.78, 1.35, 2.89, 2.11, 2.97, 0.99, 1.31, 1.41, 1.02, 1.05), h_ahead = 4L ) y2_forecast <- Forecast( origin = date, future = future, forecast = c(0.70, 0.88, 1.03, 1.05, 1.01, 0.82, 0.95, 1.09, 1.07, 1.06), realized = c(1.78, 1.35, 2.89, 2.11, 2.97, 0.99, 1.31, 1.41, 1.02, 1.05), h_ahead = 4L ) states_weighted_forc( y1_forecast, y2_forecast, matching_vars = matching_vars, time_vec = data$date, matching_window = 2L, matching = "euclidean", errors = "mse", return_weights = FALSE ) states_weighted_forc( y1_forecast, y2_forecast, matching_vars = matching_vars, time_vec = data$date, matching_window = 3L, matching = "rmse", errors = "rmse" )
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30")) future <- as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30", "2012-09-30", "2012-12-31", "2013-03-31", "2013-06-30")) y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99) x1 <- c(4.22, 3.86, 4.27, 5.60, 5.11, 4.31, 4.92, 5.80, 6.30, 4.17) x2 <- c(10.03, 10.49, 10.85, 10.47, 9.09, 10.91, 8.68, 9.91, 7.87, 6.63) data <- data.frame(date, y, x1, x2) matching_vars <- data[, c("x1", "x2")] y1_forecast <- Forecast( origin = date, future = future, forecast = c(1.33, 1.36, 1.38, 1.68, 1.60, 1.55, 1.32, 1.22, 1.08, 0.88), realized = c(1.78, 1.35, 2.89, 2.11, 2.97, 0.99, 1.31, 1.41, 1.02, 1.05), h_ahead = 4L ) y2_forecast <- Forecast( origin = date, future = future, forecast = c(0.70, 0.88, 1.03, 1.05, 1.01, 0.82, 0.95, 1.09, 1.07, 1.06), realized = c(1.78, 1.35, 2.89, 2.11, 2.97, 0.99, 1.31, 1.41, 1.02, 1.05), h_ahead = 4L ) states_weighted_forc( y1_forecast, y2_forecast, matching_vars = matching_vars, time_vec = data$date, matching_window = 2L, matching = "euclidean", errors = "mse", return_weights = FALSE ) states_weighted_forc( y1_forecast, y2_forecast, matching_vars = matching_vars, time_vec = data$date, matching_window = 3L, matching = "rmse", errors = "rmse" )
str
takes a Forecast
object and prints its internal
structure to the console.
## S4 method for signature 'Forecast' str(object)
## S4 method for signature 'Forecast' str(object)
object |
Forecast object. |
Structure of Forecast
object.
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) str(my_forecast)
my_forecast <- Forecast( origin = as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31")), forecast = c(4.21, 4.27, 5.32, 5.11), realized = c(4.40, 4.45, 4.87, 4.77), h_ahead = 4L ) str(my_forecast)
Function for subsetting all forecasts in a list of Forecast objects based on origin or future values.
subset_bytime(forcs, values, slot)
subset_bytime(forcs, values, slot)
forcs |
List of Forecast objects. |
values |
Single time object or a vector of time objects. The class of the values must match the class of the origin and future values in the list of Forecast objects. |
slot |
Character representing whether the list of Forecasts will be subset by origin or future values. Must be either "origin" or "future". |
List of subsetted Forecast objects.
forc1_1h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-05", "2011-03-10")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(4.27, 3.36, 4.78, 5.45, 5.12), realized = c(4.96, 4.17, 4.26, 4.99, 5.38), h_ahead = 1 ) forc2_1h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22", "2011-03-27")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(4.01, 3.89, 3.31, 4.33, 4.61), realized = c(4.96, 4.17, 4.26, 4.99, 5.38), h_ahead = 1 ) forcs <- list(forc1_1h, forc2_1h) subset_bytime(forcs, values = as.Date("2010-05-14"), slot = "origin") subset_bytime( forcs, values = as.Date(c("2010-09-30", "2010-12-31", "2011-03-31")), slot = "future" )
forc1_1h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-05", "2011-03-10")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(4.27, 3.36, 4.78, 5.45, 5.12), realized = c(4.96, 4.17, 4.26, 4.99, 5.38), h_ahead = 1 ) forc2_1h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22", "2011-03-27")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(4.01, 3.89, 3.31, 4.33, 4.61), realized = c(4.96, 4.17, 4.26, 4.99, 5.38), h_ahead = 1 ) forcs <- list(forc1_1h, forc2_1h) subset_bytime(forcs, values = as.Date("2010-05-14"), slot = "origin") subset_bytime( forcs, values = as.Date(c("2010-09-30", "2010-12-31", "2011-03-31")), slot = "future" )
General function for subsetting all forecasts in a list of Forecast objects.
subset_forcs(forcs, index)
subset_forcs(forcs, index)
forcs |
List of Forecast objects. |
index |
Numeric or logical value or vector. |
List of subsetted Forecast objects.
forc1_1h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-05", "2011-03-10")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(4.27, 3.36, 4.78, 5.45, 5.12), realized = c(4.96, 4.17, 4.26, 4.99, 5.38), h_ahead = 1 ) forc2_1h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22", "2011-03-27")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(4.01, 3.89, 3.31, 4.33, 4.61), realized = c(4.96, 4.17, 4.26, 4.99, 5.38), h_ahead = 1 ) forcs <- list(forc1_1h, forc2_1h) subset_forcs(forcs, 1:4) subset_forcs(forcs, origin(forc1_1h) >= as.Date("2010-12-31"))
forc1_1h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-05", "2011-03-10")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(4.27, 3.36, 4.78, 5.45, 5.12), realized = c(4.96, 4.17, 4.26, 4.99, 5.38), h_ahead = 1 ) forc2_1h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22", "2011-03-27")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(4.01, 3.89, 3.31, 4.33, 4.61), realized = c(4.96, 4.17, 4.26, 4.99, 5.38), h_ahead = 1 ) forcs <- list(forc1_1h, forc2_1h) subset_forcs(forcs, 1:4) subset_forcs(forcs, origin(forc1_1h) >= as.Date("2010-12-31"))
Function for subsetting all forecasts in a list of Forecast objects to overlapping origin or future values.
subset_identical(forcs, slot)
subset_identical(forcs, slot)
forcs |
List of Forecast objects. |
slot |
Character representing whether the list of Forecasts will be subset to identical origin or future values. Must be either "origin" or "future". |
List of subsetted Forecast objects with identical future or origin values.
forc1_1h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-05", "2011-03-10")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(4.27, 3.36, 4.78, 5.45, 5.12), realized = c(4.96, 4.17, 4.26, 4.99, 5.38), h_ahead = 1 ) forc2_1h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22", "2011-03-27")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(4.01, 3.89, 3.31, 4.33, 4.61), realized = c(4.96, 4.17, 4.26, 4.99, 5.38), h_ahead = 1 ) forcs <- list(forc1_1h, forc2_1h) subset_identical(forcs, slot = "origin")
forc1_1h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-05", "2011-03-10")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(4.27, 3.36, 4.78, 5.45, 5.12), realized = c(4.96, 4.17, 4.26, 4.99, 5.38), h_ahead = 1 ) forc2_1h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22", "2011-03-27")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(4.01, 3.89, 3.31, 4.33, 4.61), realized = c(4.96, 4.17, 4.26, 4.99, 5.38), h_ahead = 1 ) forcs <- list(forc1_1h, forc2_1h) subset_identical(forcs, slot = "origin")
Given a list of forecasts with homogenous origin or future values, converts all forecasts in the list to h_ahead format.
transform_byh(forcs, h_aheads)
transform_byh(forcs, h_aheads)
forcs |
List of Forecast objects. |
h_aheads |
Vector of h_ahead values that is equal in length to the number of Forecast objects in forcs. |
List of Forecast objects in h_ahead format.
# The following forecasts are in time format. Each forecast was made at a # different time and represents a forecast for a number of h_ahead periods # ahead. forc1_t1 <- Forecast( origin = as.Date(c("2010-02-17", "2010-02-17", "2010-02-17")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31")), forecast = c(4.27, 3.77, 3.52), realized = c(4.96, 4.17, 4.26), h_ahead = NA ) forc1_t2 <- Forecast( origin = as.Date(c("2010-05-14", "2010-05-14", "2010-05-14")), future = as.Date(c("2010-09-30", "2010-12-31", "2011-03-31")), forecast = c(3.36, 3.82, 4.22), realized = c(4.17, 4.26, 4.99), h_ahead = NA ) forc1_t3 <- Forecast( origin = as.Date(c("2010-07-22", "2010-07-22", "2010-07-22")), future = as.Date(c("2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(4.78, 4.53, 5.03), realized = c(4.26, 4.99, 5.33), h_ahead = NA ) forc1_t4 <- Forecast( origin = as.Date(c("2010-12-22", "2010-12-22", "2010-12-22")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30")), forecast = c(5.45, 4.89, 5.78), realized = c(4.99, 5.33, 5.21), h_ahead = NA ) forcs <- list(forc1_t1, forc1_t2, forc1_t3, forc1_t4) transform_byh(forcs, h_aheads = c(1, 2, 3))
# The following forecasts are in time format. Each forecast was made at a # different time and represents a forecast for a number of h_ahead periods # ahead. forc1_t1 <- Forecast( origin = as.Date(c("2010-02-17", "2010-02-17", "2010-02-17")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31")), forecast = c(4.27, 3.77, 3.52), realized = c(4.96, 4.17, 4.26), h_ahead = NA ) forc1_t2 <- Forecast( origin = as.Date(c("2010-05-14", "2010-05-14", "2010-05-14")), future = as.Date(c("2010-09-30", "2010-12-31", "2011-03-31")), forecast = c(3.36, 3.82, 4.22), realized = c(4.17, 4.26, 4.99), h_ahead = NA ) forc1_t3 <- Forecast( origin = as.Date(c("2010-07-22", "2010-07-22", "2010-07-22")), future = as.Date(c("2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(4.78, 4.53, 5.03), realized = c(4.26, 4.99, 5.33), h_ahead = NA ) forc1_t4 <- Forecast( origin = as.Date(c("2010-12-22", "2010-12-22", "2010-12-22")), future = as.Date(c("2011-03-31", "2011-06-30", "2011-09-30")), forecast = c(5.45, 4.89, 5.78), realized = c(4.99, 5.33, 5.21), h_ahead = NA ) forcs <- list(forc1_t1, forc1_t2, forc1_t3, forc1_t4) transform_byh(forcs, h_aheads = c(1, 2, 3))
Given a list of forecasts with different h_ahead values, converts all forecasts in the list to time format. Transforms a list of Forecast objects that have homogenous h_ahead values to a list of Forecast objects with homogenous origin or future values.
transform_bytime(forcs, slot = "future")
transform_bytime(forcs, slot = "future")
forcs |
List of Forecast objects. |
slot |
Character representing whether the list of Forecasts will be converted to a list of Forecasts with homogenous origin or future values. Must be either "origin" or "future". |
List of Forecast objects in time format.
# The following forecasts are in h_ahead format. All forecasts come from the # same source (forc1) and have the same origin values. However, the forecasts # are for different periods ahead. forc1_1h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31")), forecast = c(4.27, 3.36, 4.78, 5.45), realized = c(4.96, 4.17, 4.26, 4.99), h_ahead = 1 ) forc1_2h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22")), future = as.Date(c("2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(3.77, 3.82, 4.53, 4.89), realized = c(4.17, 4.26, 4.99, 5.33), h_ahead = 2 ) forc1_3h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22")), future = as.Date(c("2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30")), forecast = c(3.52, 4.22, 5.03, 5.78), realized = c(4.26, 4.99, 5.33, 5.21), h_ahead = 3 ) forcs <- list(forc1_1h, forc1_2h, forc1_3h) transform_bytime(forcs, slot = "origin")
# The following forecasts are in h_ahead format. All forecasts come from the # same source (forc1) and have the same origin values. However, the forecasts # are for different periods ahead. forc1_1h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22")), future = as.Date(c("2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31")), forecast = c(4.27, 3.36, 4.78, 5.45), realized = c(4.96, 4.17, 4.26, 4.99), h_ahead = 1 ) forc1_2h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22")), future = as.Date(c("2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30")), forecast = c(3.77, 3.82, 4.53, 4.89), realized = c(4.17, 4.26, 4.99, 5.33), h_ahead = 2 ) forc1_3h <- Forecast( origin = as.Date(c("2010-02-17", "2010-05-14", "2010-07-22", "2010-12-22")), future = as.Date(c("2010-12-31", "2011-03-31", "2011-06-30", "2011-09-30")), forecast = c(3.52, 4.22, 5.03, 5.78), realized = c(4.26, 4.99, 5.33, 5.21), h_ahead = 3 ) forcs <- list(forc1_1h, forc1_2h, forc1_3h) transform_bytime(forcs, slot = "origin")