Learn R Programming

dlmodeler (version 1.4-2)

dlmodeler.forecast: Forecast function

Description

Simulates forecasting for a DLM, with the specified horizon, step and number of iterations.

Usage

dlmodeler.forecast(yt, model, ahead = 1, iters = 1, step = 1, start = 1, prob = .90, backend = c('KFAS','FKF','dlm'), debug = FALSE)

Arguments

yt
matrix of observed values (one column per time-step).
model
an instance of dlmodeler.
ahead
in case of MSE fitting, the number of predictions to make for each iteration.
iters
in case of MSE fitting, the number of iterations.
step
in case of MSE fitting, the step between iterations.
start
in case of MSE fitting, the index of the first prediction.
prob
probability to use for the computation of prediction intervals.
backend
an optional argument which specifies the back-end to use for the computations.
debug
use slow but more robust code.

Value

A data.frame with the following variables:
index
the index of the forecasted value, index==i means that the $i$-th element of the serie is forecasted
distance
the forecasting distance, distance==k means that this value is a $k$-step ahead forecast
lower
the lower bound for yhat computed with probability prob
yhat
the forecasted value for the specified index and distance
upper
the upper bound for yhat computed with probability prob
y
the observed serie for the specified yt[index]

Details

This function simulates forecasts for the specified serie yt and model by generating iters forecasts every step points. The procedure starts at position start, and each iteration, ahead values are predicted.

See Also

dlmodeler, dlmodeler.filter, dlmodeler.smooth, dlmodeler.forecast

Examples

Run this code
require(dlmodeler)

# generate some quarterly data
n <- 80
level <- 12
sigma <- .75
season <- c(5,6,8,2)
y <- level + 3*sin((1:n)/10) + rep(season,n/4) + rnorm(n, 0, sigma)
y <- matrix(y,nrow=1)

# fit a stochastic level + quarterly seasonal model to the data by
# maximum likelihood estimation
build.fun <- function(p) {
	sigmaH <- exp(p[1])
	sigmaQ <- exp(p[2])*sigmaH
	mod <- dlmodeler.build.polynomial(0,sigmaH=sigmaH,sigmaQ=sigmaQ) +
         dlmodeler.build.dseasonal(4,sigmaH=0)
	return(mod)
}
fit <- dlmodeler.fit.MLE(y, build.fun, c(0,0))

# generate forecasts for observations 81 to 100
f <- dlmodeler.forecast(y, fit$model, start=80, ahead=20)
plot(y[1,],type='l',xlim=c(60,100),ylim=c(10,30))
lines(f$index,f$yhat,col='dark blue')
lines(f$index,f$lower,col='light blue')
lines(f$index,f$upper,col='light blue')

# simulate forecasts post-ex.
f <- dlmodeler.forecast(y, fit$model, ahead=20, start=20, iters=40)
plot(y[1,],type='p')

# show the one step ahead forecasts
with(f[f$distance==1,], lines(index,yhat,col='dark blue'))
# show the 10 step ahead forecasts
with(f[f$distance==10,], lines(index,yhat,col='blue'))
# show the 20 step ahead forecasts
with(f[f$distance==20,], lines(index,yhat,col='light blue'))

Run the code above in your browser using DataLab