|
| 1 | +\name{predict.stsmSS} |
| 2 | +\alias{predict.stsmSS} |
| 3 | + |
| 4 | +\title{Kalman Filter for State Space Models} |
| 5 | + |
| 6 | +\description{ |
| 7 | +These functions run the iterative equations of the Kalman filter |
| 8 | +for a state space model. |
| 9 | +} |
| 10 | + |
| 11 | +\usage{ |
| 12 | +\S3method{predict}{stsmSS}(object, y, n.ahead = 12L, ...) |
| 13 | +} |
| 14 | + |
| 15 | +\arguments{ |
| 16 | +\item{object}{a list containing the matrices of the state space model.} |
| 17 | +\item{y}{a numeric time series.} |
| 18 | +\item{n.ahead}{a numeric. The number of steps ahead to predict.} |
| 19 | +\item{...}{further arguments. Currently omitted.} |
| 20 | +} |
| 21 | + |
| 22 | +\value{ |
| 23 | +A list containing the following elements: |
| 24 | +item{pred}{a time series containing \code{n.ahead} predictions.} |
| 25 | +item{se}{a time series containing the standard errors of \code{pred}.} |
| 26 | +item{a}{a univariate or multivariate time series object containing \code{n.ahead} |
| 27 | +predictions for the state vector.} |
| 28 | +item{P}{a univariate or multivariate time series object containing the square of |
| 29 | +the standard errors of \code{a}.} |
| 30 | +} |
| 31 | + |
| 32 | +\details{ |
| 33 | +This function computes the same values as the |
| 34 | +function \link[stats]{predict.StructTS} from the \pkg{stats} package but |
| 35 | +the predictions of the components are also returned. |
| 36 | +} |
| 37 | + |
| 38 | +\references{ |
| 39 | +Harvey, A. C. (1989). |
| 40 | +\emph{Forecasting, Structural Time Series Models and the Kalman Filter}. |
| 41 | +Cambridge University Press. |
| 42 | +} |
| 43 | + |
| 44 | +\examples{ |
| 45 | +## local level model |
| 46 | +## Nile time series |
| 47 | +y <- Nile |
| 48 | +m <- stsm.class::stsm.model(model = "local-level", y = y, transPars = "StructTS") |
| 49 | +fit <- StructTS(y, "level") |
| 50 | +m <- stsm.class::set.pars(m, as.vector(fit$coef[c(2,1)]) * 100 / var(y)) |
| 51 | +ss <- stsm.class::char2numeric(m, P0cov = TRUE) |
| 52 | +res <- predict(ss, y, 5) |
| 53 | + |
| 54 | +# display forecasts and confidence intervals |
| 55 | +plot(cbind(y, res$pred), type = "n", plot.type = "single") |
| 56 | +lines(y) |
| 57 | +lines(res$pred, col = "blue") |
| 58 | +lines(res$pred + 2 * res$se, col = "red", lty = 2) |
| 59 | +lines(res$pred - 2 * res$se, col = "red", lty = 2) |
| 60 | + |
| 61 | +# for the whole series, the above is the same as "predict.StructTS" |
| 62 | +all.equal(res$pred, predict(fit, 5)$pred) |
| 63 | +all.equal(res$se, predict(fit, 5)$se) |
| 64 | + |
| 65 | +## basic Structural model |
| 66 | +## AirPassengers time series (in logarithms) |
| 67 | +y <- log(AirPassengers) |
| 68 | +m <- stsm.class::stsm.model(model = "BSM", y = y, transPars = "StructTS") |
| 69 | +fit <- StructTS(y, "BSM") |
| 70 | +m <- stsm.class::set.pars(m, as.vector(fit$coef[c(4,1:3)]) * 100 / var(y)) |
| 71 | +ss <- stsm.class::char2numeric(m, P0cov = TRUE) |
| 72 | +res <- predict(ss, y, 12) |
| 73 | + |
| 74 | +all.equal(res$pred, predict(fit, 12)$pred) |
| 75 | +all.equal(res$se, predict(fit, 12)$se) |
| 76 | + |
| 77 | +# forecasts and confidence intervals for the series |
| 78 | +# scaled back to original scale |
| 79 | +expy <- exp(y) |
| 80 | +plot(cbind(expy, exp(res$pred + 2 * res$se)), type = "n", plot.type = "single") |
| 81 | +lines(expy) |
| 82 | +lines(exp(res$pred), col = "blue") |
| 83 | +lines(exp(res$pred + 2 * res$se), col = "red", lty = 2) |
| 84 | +lines(exp(res$pred - 2 * res$se), col = "red", lty = 2) |
| 85 | + |
| 86 | +# forecasts for the trend component |
| 87 | +# the aproach in StructTS() seems to seasonal fluctuations in the trend |
| 88 | +# see the "stsm" package for a more flexible interface for maximum likelihood |
| 89 | +# procedures to fit a structural time series model |
| 90 | +trend <- exp(fitted(fit)[,1]) |
| 91 | +plot(cbind(trend, AirPassengers), |
| 92 | + type = "n", plot.type = "single") |
| 93 | +lines(AirPassengers, col = "gray") |
| 94 | +lines(trend) |
| 95 | +lines(exp(res$a[,1]), col = "blue") |
| 96 | +lines(exp(res$a[,1] + 2 * sqrt(res$P[,1])), col = "red", lty = 2) |
| 97 | +lines(exp(res$a[,1] - 2 * sqrt(res$P[,1])), col = "red", lty = 2) |
| 98 | + |
| 99 | +# forecasts for the seasonal component |
| 100 | +seas <- exp(fitted(fit)[,3]) |
| 101 | +plot(cbind(seas, exp(res$a[,3]) + 2 * sqrt(res$P[,3])), |
| 102 | + type = "n", plot.type = "single") |
| 103 | +lines(seas) |
| 104 | +lines(exp(res$a[,3]), col = "blue") |
| 105 | +lines(exp(res$a[,3] + 2 * sqrt(res$P[,3])), col = "red", lty = 2) |
| 106 | +lines(exp(res$a[,3] - 2 * sqrt(res$P[,3])), col = "red", lty = 2) |
| 107 | +} |
| 108 | + |
| 109 | +\keyword{ts, model} |
0 commit comments