## Not run:
# require(dlmodeler)
#
# # This section illustrates most of the possibilities offered by the
# # package by reproducing famous examples of state-space time-series
# # analysis found in the litterature.
#
# ###############################################
# # analysis from Durbin & Koopman book page 32 #
# # random walk #
# ###############################################
#
# # 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)
# # with the parametrization (phi) proposed in the book
# build.fun <- function(p) {
# varH <- exp(p[1])
# varQ <- exp(p[2])*varH
# dlmodeler.build.polynomial(0,sqrt(varH),sqrt(varQ),name='p32')
# }
#
# # fit the model by maximum likelihood estimation
# fit <- dlmodeler.fit.MLE(y, build.fun, c(0,0), verbose=FALSE)
#
# # 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')
#
#
# ################################################
# # analysis from Durbin & Koopman book page 163 #
# # random walk + stochastic seasonal #
# ################################################
#
# # load and show the data
# y <- matrix(log(Seatbelts[,'drivers']),nrow=1)
# plot(y[1,],type='l')
#
# # y(t) = a(t) + s1(t) + eta(t)
# # a(t+1) = a(t) + eps_L(t)
# # s1(t+1) = -s2(t) - s3(t) - ... - s12(t) + eps_S(t)
# # s2(t+1) = s1(t)
# # s3(t+1) = s2(t), etc.
# mod <- dlmodeler.build.structural(
# pol.order=0,
# pol.sigmaQ=NA,
# dseas.order=12,
# dseas.sigmaQ=NA,
# sigmaH=NA,
# name='p163')
#
# # 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$model$Ht[1,1] # H = 0.0034160
# fit$model$Qt[1,1] # Q1 = 0.00093585
# fit$model$Qt[2,2] # Q2 = 5.0109e-007
#
# # compute the filtered and smoothed values
# f <- dlmodeler.filter(y, fit$model, 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,])
# lines(f.ce$level[1,],col='light blue',lty=2)
# lines(s.ce$level[1,],col='dark blue')
#
# # note that the smoothed seasonal component appears to be constant
# # throughout the serie, this is due to Qt[2,2]=sigmaQS being close to
# # zero. Durbin & Koopman treat the seasonal component as deterministic
# # in the remainder of their models.
# plot(y[1,]-s.ce$level[1,],ylim=c(-.5,.5))
# lines(f.ce$seasonal[1,],type='l',col='light green',lty=2)
# lines(s.ce$seasonal[1,],type='l',col='dark green')
#
#
# #########################################################
# # analysis from Durbin & Koopman book page 166 #
# # random walk + seasonal + seat belt law + petrol price #
# #########################################################
#
# # load and show the data
# y <- matrix(log(Seatbelts[,'drivers']),nrow=1)
# law <- matrix(Seatbelts[,'law'],nrow=1)
# petrolprice <- matrix(log(Seatbelts[,'PetrolPrice']),nrow=1)
# par(mfrow=c(3,1))
# plot(y[1,],type='l')
# plot(petrolprice[1,],type='l')
# plot(law[1,],type='l')
#
# # y(t) = a(t) + s1(t) + lambda*law + mu*petrolprice + eta(t)
# # a(t+1) = a(t) + eps(t)
# # s1(t+1) = -s2(t) - s3(t) - ... - s12(t)
# # s2(t+1) = s1(t)
# # s3(t+1) = s2(t), etc.
# m1 <- dlmodeler.build.structural(
# pol.order=0,
# dseas.order=12,
# sigmaH=NA, pol.sigmaQ=NA)
# m2 <- dlmodeler.build.regression(
# law,
# sigmaQ=0,
# name='law')
# m3 <- dlmodeler.build.regression(
# petrolprice,
# sigmaQ=0,
# name='petrolprice')
# mod <- m1+m2+m3
# mod$name <- 'p166'
#
# # fit the model by maximum likelihood estimation
# fit <- dlmodeler.fit(y, mod, method="MLE")
#
# # compute the filtered and smoothed values
# f <- dlmodeler.filter(y, fit$model, smooth=TRUE)
#
# # E[y(t) | y(1), y(2), ..., y(t-1)]
# f.ce <- dlmodeler.extract(f, fit$model,
# type="observation", value="mean")
# # E[y(t) | y(1), y(2), ..., y(n)]
# s.ce <- dlmodeler.extract(f$smooth, fit$model,
# type="observation", value="mean")
# # E[a(t) | y(1), y(2), ..., y(t-1)]
# fa.ce <- dlmodeler.extract(f, fit$model,
# type="state", value="mean")
# # E[a(t) | y(1), y(2), ..., y(n)]
# sa.ce <- dlmodeler.extract(f$smooth, fit$model,
# type="state", value="mean")
#
# # see to which values the model has converged and
# # compare them with those reported by the authors
# fa.ce$law[1,193] # law = -0.23773
# fa.ce$petrolprice[1,193] # petrolprice = -0.29140
#
# # plot the smoothed de-seasonalized serie
# par(mfrow=c(1,1))
# plot(y[1,])
# lines(s.ce$level[1,]+s.ce$law[1,]+s.ce$petrolprice[1,],
# col='dark blue')
#
# # show the AIC of the model
# AIC(fit)
#
#
# ###################################
# # testing other fitting functions #
# ###################################
#
# # load and show the data
# y <- matrix(Nile,nrow=1)
# plot(y[1,],type='l')
#
# # random walk
# # y(t) = a(t) + eta(t)
# # a(t+1) = a(t) + eps(t)
# mod <- dlmodeler.build.polynomial(0,sigmaQ=NA,name='p32')
#
# # fit the model by maximum likelihood estimation and compute the
# # 1-step ahead MSE
# fit.mle <- dlmodeler.fit(y, mod, method="MLE")
# mean((fit.mle$filtered$f[1,10:100]-y[1,10:100])^2)
#
# # fit the model by minimizing the 1-step ahead MSE
# fit.mse1 <- dlmodeler.fit(y, mod, method="MSE", ahead=1, start=10)
# mean((fit.mse1$filtered$f[1,10:100]-y[1,10:100])^2)
#
# # fit the model by minimizing the 4-step ahead MSE
# fit.mse4 <- dlmodeler.fit(y, mod, method="MSE", ahead=4, start=10)
# mean((fit.mse4$filtered$f[1,10:100]-y[1,10:100])^2)
#
# # compare the 1-step ahead forecasts for these models
# # as can be expected, the MLE and MSE1 models roughly
# # have the same means
# plot(y[1,],type='l')
# lines(fit.mle$filtered$f[1,],col='dark blue')
# lines(fit.mse1$filtered$f[1,],col='dark green')
# lines(fit.mse4$filtered$f[1,],col='dark red')
#
#
# #################################################
# # looking at variances and prediction intervals #
# #################################################
#
# # load and show the data
# y <- matrix(log(Seatbelts[,'drivers']),nrow=1)
# plot(y[1,],type='l')
#
# # model with level + seasonal
# mod <- dlmodeler.build.structural(
# pol.order=0,
# pol.sigmaQ=NA,
# dseas.order=12,
# dseas.sigmaQ=NA,
# sigmaH=NA,
# name='p163')
#
# # fit the model by maximum likelihood estimation, filter & smooth
# fit <- dlmodeler.fit(y, mod, method="MLE")
# fs <- dlmodeler.filter(y, fit$model, smooth=TRUE)
#
# # value we will be using to compute 90% prediction intervals
# prob <- 0.90
#
# # true output mean + prediction interval
# output.intervals <- dlmodeler.extract(fs,fit$model,
# type="observation",value="interval",prob=prob)
# plot(y[1,],xlim=c(100,150))
# lines(output.intervals$p163$mean[1,],col='dark green')
# lines(output.intervals$p163$lower[1,],col='dark grey')
# lines(output.intervals$p163$upper[1,],col='dark grey')
#
# # true state level mean + prediction interval
# state.intervals <- dlmodeler.extract(fs, fit$model,type="state",
# value="interval",prob=prob)
# plot(y[1,])
# lines(state.intervals$level$mean[1,],col='dark green')
# lines(state.intervals$level$lower[1,],col='dark grey')
# lines(state.intervals$level$upper[1,],col='dark grey')
#
# # true state seasonal mean + prediction interval
# plot(state.intervals$seasonal$mean[1,],
# ylim=c(-.4,.4),xlim=c(100,150),
# type='l',col='dark green')
# lines(state.intervals$seasonal$lower[1,],col='light grey')
# lines(state.intervals$seasonal$upper[1,],col='light grey')
# ## End(Not run)
Run the code above in your browser using DataLab