Description Usage Arguments Details Value Note Author(s) See Also Examples
View source: R/dlmodeler-core.R
Simulates forecasting for a DLM, with the specified horizon, step and number of iterations.
1 2 3 4 5 6 |
yt |
matrix of observed values (one column per time-step). |
model |
an instance of |
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. |
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.
A data.frame with the following variables:
index |
the index of the forecasted value, |
distance |
the forecasting distance, |
lower |
the lower bound for |
yhat |
the forecasted value for the specified |
upper |
the upper bound for |
y |
the observed serie for the specified |
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.
Cyrille Szymanski <cnszym@gmail.com>
dlmodeler,
dlmodeler.filter,
dlmodeler.smooth,
dlmodeler.forecast
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'))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.