set.seed(1)
n <- 25
x <- rnorm(n, 1, 1)
beta <- cumsum(c(1, rnorm(n - 1, sd = 0.1)))
level <- -1
u <- sample(1:10, size = n, replace = TRUE)
y <- rpois(n, u * exp(level + beta * x))
ts.plot(y)
# note very small number of iterations for the CRAN checks!
out <- walker_glm(y ~ -1 + rw1(~ x, beta = c(0, 10),
sigma = c(2, 10)), distribution = "poisson",
iter = 200, chains = 1, refresh = 0)
print(out$stanfit, pars = "sigma_rw1") ## approximate results
if (require("diagis")) {
weighted_mean(extract(out$stanfit, pars = "sigma_rw1")$sigma_rw1,
extract(out$stanfit, pars = "weights")$weights)
}
plot_coefs(out)
pp_check(out)
if (FALSE) {
data("discoveries", package = "datasets")
out <- walker_glm(discoveries ~ -1 +
rw2(~ 1, beta = c(0, 10), sigma = c(2, 10), nu = c(0, 2)),
distribution = "poisson", iter = 2000, chains = 1, refresh = 0)
plot_fit(out)
# Dummy covariate example
fit <- walker_glm(VanKilled ~ -1 +
rw1(~ law, beta = c(0, 1), sigma = c(2, 10)), dist = "poisson",
data = as.data.frame(Seatbelts), chains = 1, refresh = 0)
# compute effect * law
d <- coef(fit, transform = function(x) {
x[, 2, 1:170] <- 0
x
})
require("ggplot2")
d %>% ggplot(aes(time, mean)) +
geom_ribbon(aes(ymin = `2.5%`, ymax = `97.5%`), fill = "grey90") +
geom_line() + facet_wrap(~ beta, scales = "free") + theme_bw()
}
Run the code above in your browser using DataLab