bartc-generics | R Documentation |
bartcFit
ObjectsVisual exploratory data analysis and model fitting diagnostics for causal inference models fit
using the bartc
function.
## S3 method for class 'bartcFit'
fitted(object,
type = c("pate", "sate", "cate", "mu.obs", "mu.cf", "mu.0",
"mu.1", "y.cf", "y.0", "y.1", "icate", "ite",
"p.score", "p.weights"),
sample = c("inferential", "all"),
...)
extract(object, ...)
## S3 method for class 'bartcFit'
extract(object,
type = c("pate", "sate", "cate", "mu.obs", "mu.cf", "mu.0",
"mu.1", "y.cf", "y.0", "y.1", "icate", "ite",
"p.score", "p.weights", "sigma"),
sample = c("inferential", "all"),
combineChains = TRUE,
...)
## S3 method for class 'bartcFit'
predict(object, newdata,
group.by,
type = c("mu", "y", "mu.0", "mu.1", "y.0", "y.1", "icate", "ite",
"p.score"),
combineChains = TRUE,
...)
refit(object, newresp, ...)
## S3 method for class 'bartcFit'
refit(object,
newresp = NULL,
commonSup.rule = c("none", "sd", "chisq"),
commonSup.cut = c(NA_real_, 1, 0.05),
...)
object |
Object of class |
type |
Which quantity to return. See details for a description of possible values. |
sample |
Return information for either the |
combineChains |
If the models were fit with more than one chain, results retain the chain structure unless
|
newresp |
Not presently used, but provided for compatibility with other definitions of the |
newdata |
Data corresponding to the confounders in a |
group.by |
Optional grouping variable. See definition of |
commonSup.rule , commonSup.cut |
As in |
... |
Additional parameters passed up the generic method chain. |
fitted
returns the values that would serve as predictions for an object returned by the
bartc
function, while extract
instead returns the full matrix or array of posterior
samples. The possible options are:
"pate", "sate", "cate"
- various target quantities; see summary
"mu"
- predict only: expected value; requires user-supplied treatment variable in
newdata
"y"
- predict only: sample of the response; requires user-supplied treatment variable in
newdata
"mu.obs"
- (samples from the posterior of) the expected value under the observed
treatment condition, i.e.
\hat{mu}_i(1) * z_i + \hat{mu}_i(0) * (1 - z_i)
"mu.cf"
- the expected value under the counterfactual treatment condition, i.e.
\hat{mu}_i(1) * (1 - z_i) + \hat{mu}_i(0) * z_i)
"mu.0"
- the expected value under the control condition
"mu.1"
- the expected value under the treated condition
"y.cf"
- samples of the response under the the counterfactual treatment condition, i.e.
\hat{y}_i(1 - z_i))
; values are obtained by adding noise to mu.cf
using the posterior predictive distribution
"y.0"
- observed responses under the control together with predicted under the treated, i.e.
\hat{y}_i(1) * z_i + y(0) * (1 - z_i)
"y.1"
- observed responses under the treatment together with predicted under the control, i.e.
y_i(1) * z_i + \hat{y}(0) * (1 - z_i)
"ite"
- (sample) individual treatment effect estimates, i.e.
(y_i(z_i) - y_i(1 - z_i)) * (2z_i - 1)
; uses observed responses and posterior
predicted counterfactuals
"icate"
- individual conditional average treatment effect estimates, i.e.
\hat{mu}_i(1) - \hat{mu}_i(0)
"p.score"
- probability that each observation is assigned to the treatment group
"p.weights"
- weights assigned to each individual difference if the response method
is "p.weight"
"sigma"
- residual standard deviation from continuous response models
refit
exists to allow the same regressions to be used to calculate estimates under different
common support rules. To refit those models on a subset, see the examples in bartc
.
predict
allows the fitted model to be used to make predictions on an out-of-sample set.
Requires model to be fit with keepTrees
equal to TRUE
. As ‘y
’ values are
all considered out of sample, the posterior predictive distribution is always used when relevant.
For fitted
, extract
, and predict
, a matrix, array, or vector depending on the
dimensions of the result and the number of chains. For the following, when n.chains
is one
the dimension is dropped.
"pate"
, "sate"
, or "cate"
- with fitted
, a scalar; with
extract
, n.chains x n.samples
"p.score"
- depending on the fitting method, samples may or not be present; when samples
are absent, a vector is returned for both functions; when present, the same as "y"
.
all other types - with fitted
, a vector of length equal to the number of
observations (n.obs
); with extract
or predict
, a matrix or array of
dimensions n.chains x n.samples x n.obs
.
For refit
, an object of class bartcFit
.
Vincent Dorie: vdorie@gmail.com.
bartc
## fit a simple linear model
n <- 100L
beta.z <- c(.75, -0.5, 0.25)
beta.y <- c(.5, 1.0, -1.5)
sigma <- 2
set.seed(725)
x <- matrix(rnorm(3 * n), n, 3)
tau <- rgamma(1L, 0.25 * 16 * rgamma(1L, 1 * 32, 32), 16)
p.score <- pnorm(x %*% beta.z)
z <- rbinom(n, 1, p.score)
mu.0 <- x %*% beta.y
mu.1 <- x %*% beta.y + tau
y <- mu.0 * (1 - z) + mu.1 * z + rnorm(n, 0, sigma)
# low parameters only for example
fit <- bartc(y, z, x, n.samples = 100L, n.burn = 15L, n.chains = 2L)
# compare fit to linear model
lm.fit <- lm(y ~ z + x)
plot(fitted(fit, type = "mu.obs"), fitted(lm.fit))
# rank order sample individual treatment effect estimates and plot
ites <- extract(fit, type = "ite")
ite.m <- apply(ites, 2, mean)
ite.sd <- apply(ites, 2, sd)
ite.lb <- ite.m - 2 * ite.sd
ite.ub <- ite.m + 2 * ite.sd
ite.o <- order(ite.m)
plot(NULL, type = "n",
xlim = c(1, length(ite.m)), ylim = range(ite.lb, ite.ub),
xlab = "effect order", ylab = "individual treatment effect")
lines(rbind(seq_along(ite.m), seq_along(ite.m), NA),
rbind(ite.lb[ite.o], ite.ub[ite.o], NA), lwd = 0.5)
points(seq_along(ite.m), ite.m[ite.o], pch = 20)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.