bal.plot {cobalt} | R Documentation |
Visualize Distributional Balance
Description
Generates density plots, bar graphs, or scatterplots displaying distributional balance between treatment and covariates using ggplot2.
Usage
bal.plot(
x,
var.name,
...,
which,
which.sub = NULL,
cluster = NULL,
which.cluster = NULL,
imp = NULL,
which.imp = NULL,
which.treat = NULL,
which.time = NULL,
mirror = FALSE,
type = "density",
colors = NULL,
grid = FALSE,
sample.names,
position = "right",
facet.formula = NULL,
disp.means = getOption("cobalt_disp.means", FALSE),
alpha.weight = TRUE
)
Arguments
x |
the object for which balance is to be assessed; can be any object for which there is support in |
var.name |
|
... |
other arguments to define the variable, treatment, and weights. Some inputs are required depending on the method. See Additional Arguments. Can also be used to supply the |
which |
whether to display distributional balance for the adjusted ( |
which.sub |
|
cluster |
optional; a vector of cluster membership, or the name of a variable in an available data set passed to |
which.cluster |
if clusters are used, which cluster(s) to display. Can be cluster names or numerical indices for which to display balance. Indices correspond to the alphabetical order of cluster names. If |
imp |
optional; a vector of imputation indices, or the name of a variable in an available data set passed to |
which.imp |
if imputations are used, which imputations(s) to display. Must be numerical indices for which to display balance. If |
which.treat |
which treatment groups to display. If |
which.time |
for longitudinal treatments, which time points to display. Can be treatment names or time period indices. If |
mirror |
|
type |
|
colors |
a vector of colors for the plotted densities/histograms. See 'Color Specification' at |
grid |
|
sample.names |
|
position |
the position of the legend. This can be any value that would be appropriate as an argument to |
facet.formula |
a |
disp.means |
|
alpha.weight |
|
Details
bal.plot()
uses ggplot2::ggplot()
from the ggplot2 package, and (invisibly) returns a "ggplot"
object. For categorical treatments with continuous covariates or continuous treatments with categorical covariates, density plots are created using ggplot2::geom_density()
, histograms are created using ggplot2::geom_histogram()
, and empirical CDF plots are created using ggplot2::geom_step()
; for categorical treatments with categorical covariates, bar graphs are created using ggplot2::geom_bar()
; for continuous treatments with continuous covariates, scatterplots are created using ggplot2::geom_point()
.
For continuous treatments with continuous covariates, four additional lines are presented for aid in balance assessment. The red line is the linear fit line. The blue line is a smoothing curve generated with ggplot2's ggplot2::geom_smooth()
with method = "auto"
. The horizontal black line is a horizontal reference line intercepting the (unweighted) treatment mean. The vertical black line is a reference line intercepting the (unweighted) treatment mean. Balance is indicated by the flatness of both fit lines and whether they pass through the intersection of the two black reference lines.
When multiple plots are to be displayed (i.e., when requesting subclass balance, cluster balance, or imputation balance, or when multiple sets of weights are provided or which = "both"
, or when treatment is longitudinal), the plots will be displayed in a grid using ggplot2's ggplot2::facet_grid()
. Subclassification cannot be used with clusters or multiply imputed data.
To change the plot and axis titles, use ggplot2::labs()
. Because the output is a ggplot
object, other elements can be changed using ggplot2 functions; see here for an example.
Value
A "ggplot"
object, returned invisibly.
Additional Arguments
bal.plot()
works like bal.tab()
in that it can take a variety of types of inputs and yield the same output for each. Depending on what kind of input is given, different additional parameters are required in ...
. For details on what is required and allowed for each additional input and their defaults, see the help file for the bal.tab()
method associated with the input. The following are the required additional arguments based on each input type:
For
matchit
objects: NoneFor
weightit
objects: NoneFor
ps
,ps.cont
,mnps
, andiptw
objects: (stop.method
; see defaults).For
Match
objects:formula
anddata
orcovs
andtreat
.For
optmatch
objects:formula
anddata
orcovs
(treat
is not required).For
CBPS
objects: NoneFor
ebalance
objects:formula
anddata
orcovs
andtreat
.For
formula
s:data
For
data.frame
s:treat
For
designmatch
objects:formula
anddata
orcovs
andtreat
.For
sbw
objects: NoneFor
mimids
andwimids
objects: None, but an argument towhich.imp
should be specified.For other objects processed through
bal.tab()
's default method, whichever arguments are required to identify treatment, variables, and a conditioning method (if any).
See Also
Examples
data("lalonde", package = "cobalt")
#Nearest Neighbor Matching
m.out <- MatchIt::matchit(treat ~ age + educ + race + married +
nodegree + re74 + re75,
data = lalonde)
bal.plot(m.out, "age", which = "both")
bal.plot(m.out, "re74", which = "both", type = "ecdf")
bal.plot(m.out, "race", which = "both")
bal.plot(m.out, "distance", which = "both", mirror = TRUE,
type = "histogram", colors = c("white", "black"))
#Entropy balancing with a continuous treatment
w.out <- WeightIt::weightit(re75 ~ age + I(age^2) + educ +
race + married + nodegree,
data = lalonde, method = "ebal")
bal.plot(w.out, "age", which = "both")
bal.plot(w.out, "married", which = "both")