forecast_dfosr: Forecast with a dynamic function-on-scalars regression model

Description Usage Arguments Value Examples

View source: R/helper_functions.R

Description

Compute the one-step forecasting estimate under a dynamic function-on-scalars regression model.

Usage

1
forecast_dfosr(X_Tp1 = NULL, post_sims, factor_model = "AR")

Arguments

X_Tp1

the p x 1 matrix of predictors at the forecasting time point T + 1; if NULL, set to an intercept

post_sims

a named list of posterior draws for the following parameters:

  • alpha (regression coefficients)

  • fk (loading curves)

  • mu_k (intercept term for factor k)

  • ar_phi (OPTIONAL; AR coefficients for each k under AR(1) model)

factor_model

model for the (dynamic) factors; must be one of

  • "IND" (independent errors)

  • "AR" (stationary autoregression of order 1)

  • "RW" (random walk model)

Value

Yfore, the m x 1 curve forecasting estimate

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
## Not run: 
# Simulate some data:
sim_data = simulate_dfosr(T = 200, m = 50, p_0 = 2, p_1 = 2)
Y = sim_data$Y; X = sim_data$X; tau = sim_data$tau
T = nrow(Y); m = ncol(Y); p = ncol(X) # Dimensions

# Delete and store the last time point (for forecasting):
Y_Tp1 = Y[T,]; X_Tp1 = X[T,]
Y = Y[-T,]; X = X[-T,]; T = nrow(Y);

# Run the MCMC w/ K = 6:
out = dfosr(Y = Y, tau = tau, X = X, K = 6,
           factor_model = 'AR',
           use_dynamic_reg = TRUE,
           mcmc_params = list("beta", "fk", "alpha", "mu_k", "ar_phi"))

# Compute one-step forecasts:
Yfore = forecast_dfosr(X_Tp1 = X_Tp1,
                       post_sims = out,
                       factor_model = "AR")
# Plot the results:
plot(tau, Yfore, ylim = range(Yfore, Y_Tp1, Y[T,], na.rm=TRUE),
     main = 'One-Step Forecast',
     lwd = 8, col = "cyan", type = 'l')
# Add the most recent observed curve:
lines(tau, Y[T,], type='p', pch = 2)
# Add the realized curve:
lines(tau, Y_Tp1, type='p')
# And the true curve:
lines(tau, sim_data$Y_true[T+1,], lwd=8, col='black', lty=6)

## End(Not run)

drkowal/dfosr documentation built on May 7, 2020, 3:09 p.m.