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 "qml", "2s" or "pca".

standardized

logical. FALSE will return data forecasts on the original scale.

resFUN

an (optional) function to compute a univariate forecast of the residuals. The function needs to have a second argument providing the forecast horizon (h) and return a vector or forecasts. See Examples.

resAC

numeric. Threshold for residual autocorrelation to apply resFUN: only residual series where AC1 > resAC will be forecasted.

...

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 ts.plot.

factors

integers indicating which factors to display. Setting this to NA, NULL or 0 will omit factor plots.

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 par.

fcst.lty

integer or character giving the line type of the forecasts of factors and data. See par.

data.col

character vector of length 2 indicating the colours of historical data and forecasts of that data. Setting this to NA, NULL or "" will not plot data and data forecasts.

legend

logical. TRUE draws a legend in the top-left of the chart.

legend.items

character names of factors for the legend.

grid

logical. TRUE draws a grid on the background of the plot.

vline

logical. TRUE draws a vertical line deliminating historical data and forecasts.

vline.lty, vline.col

graphical parameters affecting the appearance of the vertical line. See par.

use

character. Which forecasts to use "factors", "data" or "both".

pivot

character. The orientation of the frame: "long" or "wide".

time

a vector identifying the time dimension, must be of length T + h, or NULL to omit a time variable.

stringsAsFactors

logical. If TRUE and pivot = "long" the 'Variable' column is created as a factor. Same as option to as.data.frame.table.

Value

A list-like object of class 'dfm_forecast' with the following elements:

X_fcst

h \times n matrix with the forecasts of the variables.

F_fcst

h \times r matrix with the factor forecasts.

X

T \times n matrix with the standardized (scaled and centered) data - with attributes attached allowing reconstruction of the original data:

"stats" is a n \times 5 matrix of summary statistics of class "qsu" (see qsu). Only attached if standardized = TRUE.
"attributes" contains the attributes of the original data input.
"is.list" is a logical value indicating whether the original data input was a list / data frame.
F

T \times r matrix of factor estimates.

method

the factor estimation method used.

anyNA

logical indicating whether X contains any missing values.

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 X) the residuals were forecasted using the univariate function.

call

call object obtained from match.call().

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


[Package dfms version 0.2.2 Index]