# NOT RUN {
model <- bsm_ng(Seatbelts[, "VanKilled"], distribution = "poisson",
sd_level = halfnormal(0.01, 1),
sd_seasonal = halfnormal(0.01, 1),
beta = normal(0, 0, 10),
xreg = Seatbelts[, "law"],
# default values, just for illustration
period = 12,
a1 = rep(0, 1 + 11), # level + period - 1 seasonal states
P1 = diag(1, 12),
C = matrix(0, 12, 1),
u = rep(1, nrow(Seatbelts)))
# }
# NOT RUN {
set.seed(123)
mcmc_out <- run_mcmc(model, iter = 5000, particles = 10, mcmc_type = "da")
mcmc_out$acceptance_rate
theta <- expand_sample(mcmc_out, "theta")
plot(theta)
summary(theta)
library("ggplot2")
ggplot(as.data.frame(theta[,1:2]), aes(x = sd_level, y = sd_seasonal)) +
geom_point() + stat_density2d(aes(fill = ..level.., alpha = ..level..),
geom = "polygon") + scale_fill_continuous(low = "green", high = "blue") +
guides(alpha = "none")
# Traceplot using as.data.frame method for MCMC output
library("dplyr")
as.data.frame(mcmc_out) %>%
filter(variable == "sd_level") %>%
ggplot(aes(y = value, x = iter)) + geom_line()
# }
# NOT RUN {
# Model with slope term and additional noise to linear predictor to capture
# excess variation
model2 <- bsm_ng(Seatbelts[, "VanKilled"], distribution = "poisson",
sd_level = halfnormal(0.01, 1),
sd_seasonal = halfnormal(0.01, 1),
beta = normal(0, 0, 10),
xreg = Seatbelts[, "law"],
sd_slope = halfnormal(0.01, 0.1),
sd_noise = halfnormal(0.01, 1))
# instead of extra noise term, model using negative binomial distribution:
model3 <- bsm_ng(Seatbelts[, "VanKilled"],
distribution = "negative binomial",
sd_level = halfnormal(0.01, 1),
sd_seasonal = halfnormal(0.01, 1),
beta = normal(0, 0, 10),
xreg = Seatbelts[, "law"],
sd_slope = halfnormal(0.01, 0.1),
phi = gamma_prior(1, 5, 5))
# }
Run the code above in your browser using DataLab