dlmodeler.forecast: Forecast function

Description Usage Arguments Details Value Note Author(s) See Also Examples

View source: R/dlmodeler-core.R

Description

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

Usage

1
2
3
4
5
6
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.

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.

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]

Note

Currently, the function only works for univariate time-series.

Currently the implementation is very slow, but its speed will be increased in future versions of this package.

Author(s)

Cyrille Szymanski <cnszym@gmail.com>

See Also

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

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
33
34
35
36
37
38
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'))

dlmodeler documentation built on May 29, 2017, 11:33 a.m.