if (FALSE) {
set.seed(1)
n <- 50
x1 <- rnorm(n, 0, 1)
x2 <- rnorm(n, 1, 0.5)
x3 <- rnorm(n)
beta1 <- cumsum(c(1, rnorm(n - 1, sd = 0.1)))
beta2 <- cumsum(c(0, rnorm(n - 1, sd = 0.1)))
beta3 <- -1
u <- sample(1:10, size = n, replace = TRUE)
y <- rbinom(n, u, plogis(beta3 * x3 + beta1 * x1 + beta2 * x2))
d <- data.frame(y, x1, x2, x3)
out <- walker_glm(y ~ x3 + rw1(~ -1 + x1 + x2, beta = c(0, 2),
sigma = c(2, 10)), distribution = "binomial", beta = c(0, 2),
u = u, data = d,
iter = 2000, chains = 1, refresh = 0)
# what if our covariates were constant?
newdata <- data.frame(x1 = rep(0.4, n), x2 = 1, x3 = -0.1)
fitted <- fitted(out)
pred <- predict_counterfactual(out, newdata, type = "mean")
ts.plot(cbind(fitted[, c(1, 3, 5)], pred[, c(1, 3, 5)]),
col = rep(1:2, each = 3), lty = c(1, 2, 2))
}
Run the code above in your browser using DataLab