predict.dfm {dfms} | R Documentation |
DFM Forecasts
Description
This function produces h-step ahead forecasts of both the factors and the data, with an option to also forecast autocorrelated residuals with a univariate method and produce a combined forecast.
Usage
## S3 method for class 'dfm'
predict(
object,
h = 10L,
method = switch(object$em.method, none = "2s", "qml"),
standardized = TRUE,
resFUN = NULL,
resAC = 0.1,
...
)
## S3 method for class 'dfm_forecast'
print(x, digits = 4L, ...)
## S3 method for class 'dfm_forecast'
plot(
x,
main = paste(x$h, "Period Ahead DFM Forecast"),
xlab = "Time",
ylab = "Standardized Data",
factors = seq_len(ncol(x$F)),
scale.factors = TRUE,
factor.col = rainbow(length(factors)),
factor.lwd = 1.5,
fcst.lty = "dashed",
data.col = c("grey85", "grey65"),
legend = TRUE,
legend.items = paste0("f", factors),
grid = FALSE,
vline = TRUE,
vline.lty = "dotted",
vline.col = "black",
...
)
## S3 method for class 'dfm_forecast'
as.data.frame(
x,
...,
use = c("factors", "data", "both"),
pivot = c("long", "wide"),
time = seq_len(nrow(x$F) + x$h),
stringsAsFactors = TRUE
)
Arguments
object |
an object of class 'dfm'. |
h |
integer. The forecast horizon. |
method |
character. The factor estimates to use: one of |
standardized |
logical. |
resFUN |
an (optional) function to compute a univariate forecast of the residuals.
The function needs to have a second argument providing the forecast horizon ( |
resAC |
numeric. Threshold for residual autocorrelation to apply |
... |
not used. |
x |
an object class 'dfm_forecast'. |
digits |
integer. The number of digits to print out. |
main , xlab , ylab |
character. Graphical parameters passed to |
factors |
integers indicating which factors to display. Setting this to |
scale.factors |
logical. Standardize factor estimates, this usually improves the plot since the factor estimates corresponding to the greatest PCA eigenvalues tend to have a greater variance than the data. |
factor.col , factor.lwd |
graphical parameters affecting the colour and line width of factor estimates plots. See |
fcst.lty |
integer or character giving the line type of the forecasts of factors and data. See |
data.col |
character vector of length 2 indicating the colours of historical data and forecasts of that data. Setting this to |
legend |
logical. |
legend.items |
character names of factors for the legend. |
grid |
logical. |
vline |
logical. |
vline.lty , vline.col |
graphical parameters affecting the appearance of the vertical line. See |
use |
character. Which forecasts to use |
pivot |
character. The orientation of the frame: |
time |
a vector identifying the time dimension, must be of length T + h, or |
stringsAsFactors |
logical. If |
Value
A list-like object of class 'dfm_forecast' with the following elements:
X_fcst |
| |||||||||||||
F_fcst |
| |||||||||||||
X |
| |||||||||||||
F |
| |||||||||||||
method |
the factor estimation method used. | |||||||||||||
anyNA |
logical indicating whether | |||||||||||||
h |
the forecast horizon. | |||||||||||||
resid.fc |
logical indicating whether a univariate forecasting function was applied to the residuals. | |||||||||||||
resid.fc.ind |
indices indicating for which variables (columns of | |||||||||||||
call |
call object obtained from |
Examples
library(xts)
library(collapse)
# Fit DFM with 3 factors and 3 lags in the transition equation
mod = DFM(diff(BM14_M), r = 3, p = 3)
# 15 period ahead forecast
fc = predict(mod, h = 15)
print(fc)
plot(fc, xlim = c(300, 370))
# Also forecasting autocorrelated residuals with an AR(1)
fcfun <- function(x, h) predict(ar(na_rm(x)), n.ahead = h)$pred
fcar = predict(mod, resFUN = fcfun, h = 15)
plot(fcar, xlim = c(300, 370))
# Retrieving a data frame of the forecasts
head(as.data.frame(fcar, pivot = "wide")) # Factors
head(as.data.frame(fcar, use = "data")) # Data
head(as.data.frame(fcar, use = "both")) # Both