Learn R Programming

dlmodeler (version 1.4-2)

dlmodeler.fit: Fitting function for a model (MLE, MSE, MAD, sigma)

Description

Fits a DLM by maximum likelihood (MLE), minimum squared errror (MSE), minimum average deviation (MAD) or minimum standard deviation (sigma) methods.

Usage

dlmodeler.fit(yt, model=NULL, method=c("MLE","MSE","MAD","MAPE","sigma"), ...)
dlmodeler.fit.MLE(yt, build.fun, par, backend = c('KFAS','FKF','dlm'), method = "L-BFGS-B", verbose = FALSE, silent = FALSE, filter = TRUE, smooth = FALSE, raw.result = FALSE, ...)
dlmodeler.fit.MSE(yt, build.fun, par, ahead, iters = NCOL(yt)-ahead-start-1, step = 1, start = 1, backend = c('KFAS','FKF','dlm'), method = "L-BFGS-B", verbose = FALSE, silent = FALSE, filter = TRUE, smooth = FALSE, raw.result=FALSE, ...)
dlmodeler.fit.MAD(yt, build.fun, par, ahead, iters = NCOL(yt)-ahead-start-1, step = 1, start = 1, backend = c('KFAS','FKF','dlm'), method = "L-BFGS-B", verbose = FALSE, silent = FALSE, filter = TRUE, smooth = FALSE, raw.result=FALSE, ...)
dlmodeler.fit.MAPE(yt, build.fun, par, ahead, iters = NCOL(yt)-ahead-start-1, step = 1, start = 1, backend = c('KFAS','FKF','dlm'), method = "L-BFGS-B", verbose = FALSE, silent = FALSE, filter = TRUE, smooth = FALSE, raw.result=FALSE, ...)
dlmodeler.fit.sigma(yt, build.fun, par, backend = c('KFAS','FKF','dlm'), method = "L-BFGS-B", verbose = FALSE, silent = FALSE, filter = TRUE, smooth = FALSE, raw.result=FALSE, ...)

Arguments

yt
matrix of observed values (one column per time step).
model
object of class dlmodeler with NA values to be fitted.
build.fun
function taking parameter vector p as first argument and returning a DLM.
par
initial value of the parameter vector p.
backend
an optional argument which specifies the back-end to use for the computations.
method
optimization method passed to function optim.
verbose
if TRUE, then write one line per iteration giving the parameter vector p and the value of the objective function.
silent
if TRUE, then do not write anything.
filter
if TRUE, then return the filtered optimal model.
smooth
if TRUE, the return the smoothed optimal model.
raw.result
if TRUE, the raw results from the back-end will be stored in raw.result.
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.
...
additional arguments passed to build.fun.

Value

An object of class dlmodeler.fit with the following values:
par
optimal parameter returned by the optimization function optim()
message
message returned by the optimization function optim()
convergence
convergence code returned by the optimization function optim()
model
optimal model found: build.fun(par)
logLik
value of the log-likelihood or NA
par0
initial value of par
filtered
optionally, the filtered model: dlmodeler.filter(yt,build.fun(par))

Details

dlmodeler.fit.MLE is designed to find parameter values which maximize the log-likelihood for the given data. This is called Maximum Likelihood Estimation.

dlmodeler.fit.MSE is designed to find parameter values which minimize the average $n$-step ahead prediction squared error $(predicted-actual)^2$ for the given data. This is called Minimum Squared Error fitting. The squared error is averaged over ahead prediction steps. Note that having ahead==1 is roughly equivalent to MLE fitting as long as only the mean is concerned.

dlmodeler.fit.MAD is designed to find parameter values which minimize the average $n$-step ahead prediction absolute error $|predicted-actual|$ for the given data. This is called Minimum Average Deviation fitting. The absolute error is averaged over ahead prediction steps.

dlmodeler.fit.MAPE is designed to find parameter values which minimize the average $n$-step ahead prediction absolute percentage error $|predicted-actual|$ for the given data. This is called Minimum Average Percentage Error fitting. The absolute percentage error is averaged over ahead prediction steps.

dlmodeler.fit.sigma is designed to find parameter values which minimize the one-step ahead prediction variance for the given data.

See Also

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

Examples

Run this code
## Not run: 
# require(dlmodeler)
# 
# # analysis from Durbin & Koopman book page 32
# 
# # load and show the data
# y <- matrix(Nile,nrow=1)
# plot(y[1,],type='l')
# 
# # y(t)   = a(t) + eta(t)
# # a(t+1) = a(t) + eps(t)
# mod <- dlmodeler.build.polynomial(0,sigmaH=NA,sigmaQ=NA,name='p32')
# 
# # fit the model by maximum likelihood estimation
# fit <- dlmodeler.fit(y, mod, method="MLE")
# 
# # compare the fitted parameters with those reported by the authors
# fit$par[2]        # psi = -2.33
# fit$model$Ht[1,1] # H   = 15099
# fit$model$Qt[1,1] # Q   = 1469.1
# 
# # compute the filtered and smoothed values
# f <- dlmodeler.filter(y, fit$mod, smooth=TRUE)
# 
# # f.ce represents the filtered one steap ahead observation
# # prediction expectations E[y(t) | y(1), y(2), ..., y(t-1)]
# f.ce <- dlmodeler.extract(f, fit$model,
#                           type="observation", value="mean")
# 
# # s.ce represents the smoothed observation expectations
# # E[y(t) | y(1), y(2), ..., y(n)]
# s.ce <- dlmodeler.extract(f$smooth, fit$model,
#                           type="observation", value="mean")
# 
# # plot the components
# plot(y[1,],type='l')
# lines(f.ce$p32[1,],col='light blue',lty=2)
# lines(s.ce$p32[1,],col='dark blue')
# ## End(Not run)

Run the code above in your browser using DataLab