View source: R/predict_counterfactual.R
predict_counterfactual | R Documentation |
Given the new covariate data and output from walker
,
obtain samples from posterior predictive distribution for counterfactual case,
i.e. for past time points with different covariate values.
predict_counterfactual(
object,
newdata,
u,
summary = TRUE,
type = ifelse(object$distribution == "gaussian", "response", "mean")
)
object |
An output from |
newdata |
A |
u |
For Poisson model, a vector of exposures i.e. E(y) = uexp(xbeta). For binomial, a vector containing the number of trials. Defaults 1. |
summary |
If |
type |
If |
If summary=TRUE
, time series containing summary statistics of predicted values.
Otherwise a matrix of samples from predictive distribution.
## Not run:
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))
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.