## Example 1: Time series (ts) data
data(AirPassengers)
y <- log(AirPassengers)
ss <- AddLocalLinearTrend(list(), y)
ss <- AddSeasonal(ss, y, nseasons = 12)
model <- bsts(y, state.specification = ss, niter = 500)
pred <- predict(model, horizon = 12, burn = 100)
par(mfrow = c(1,2))
plot(model)
plot(pred)
MakePlots <- function(model, ask = TRUE) {
## Make all the plots callable by plot.bsts.
opar <- par(ask = ask)
on.exit(par(opar))
plot.types <- c("state", "components", "residuals",
"prediction.errors", "forecast.distribution")
for (plot.type in plot.types) {
plot(model, plot.type)
}
if (model$has.regression) {
regression.plot.types <- c("coefficients", "predictors", "size")
for (plot.type in regression.plot.types) {
plot(model, plot.type)
}
}
}
## Example 2: GOOG is the Google stock price, an xts series of daily
## data.
data(goog)
ss <- AddGeneralizedLocalLinearTrend(list(), goog)
model <- bsts(goog, state.specification = ss, niter = 500)
MakePlots(model)
## Example 3: Change GOOG to be zoo, and not xts.
goog <- zoo(goog, index(goog))
ss <- AddGeneralizedLocalLinearTrend(list(), goog)
model <- bsts(goog, state.specification = ss, niter = 500)
MakePlots(model)
## Example 4: Naked numeric data works too
y <- rnorm(100)
ss <- AddLocalLinearTrend(list(), y)
model <- bsts(y, state.specification = ss, niter = 500)
MakePlots(model)
## Example 5: zoo data with intra-day measurements
y <- zoo(rnorm(100),
seq(from = as.POSIXct("2012-01-01 7:00 EST"), len = 100, by = 100))
ss <- AddLocalLinearTrend(list(), y)
model <- bsts(y, state.specification = ss, niter = 500)
MakePlots(model)
Run the code above in your browser using DataLab