knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
Residual balancing is a method of constructing weights for marginal structural models, which can be used to estimate marginal effects of time-varying treatments and controlled direct/mediator effects in causal mediation analysis. Compared with inverse probability-of-treatment weights (IPW), residual balancing weights tend to be more robust and more efficient, and are easier to use with continuous exposures. This package provides three main functions, rbwPoint()
, rbwPanel()
and rbwMed()
, that produce residual balancing weights for analyzing point treatments, time-varying treatments, and causal mediation, respectively.
Reference
You can install the released version of rbw from CRAN with:
install.packages("rbw")
And the development version from GitHub with:
# install.packages("devtools") devtools::install_github("xiangzhou09/rbw")
The rbwPoint()
function constructs residual balancing weights for estimating the average effect of a point treatment. The following example illustrates its use by estimating the average effect of televised political advertisements (treat
) on campaign contributions (Cont
) among 16,265 zipcodes in the 2004 and 2008 US presidential elections.
library(rbw) # install.packages("survey") library(survey) # residual balancing weights rbwPoint_fit <- rbwPoint(treat, baseline_x = c(log_TotalPop, PercentOver65, log_Inc, PercentHispanic, PercentBlack, density, per_collegegrads, CanCommute), data = advertisement) # attach residual balancing weights to data advertisement$rbw_point <- rbwPoint_fit$weights # fit marginal structural model rbw_design <- svydesign(ids = ~ 1, weights = ~ rbw_point, data = advertisement) # the outcome model includes the treatment, the square of the treatment, # and state-level fixed effects (Fong, Hazlett, and Imai 2018) msm_rbwPoint <- svyglm(Cont ~ treat + I(treat^2) + factor(StFIPS), design = rbw_design) summary(msm_rbwPoint)
The rbwPanel()
function constructs residual balancing weights for estimating marginal effects of time-varying treatments. The following example illustrates its use by estimating the effect of negative campaign advertising (d.gone.neg
) on election outcomes (demprcnt
) for 113 Democratic candidates in US Senate and Gubernatorial elections.
# models for time-varying confounders m1 <- lm(dem.polls ~ (d.gone.neg.l1 + dem.polls.l1 + undother.l1) * factor(week), data = campaign_long) m2 <- lm(undother ~ (d.gone.neg.l1 + dem.polls.l1 + undother.l1) * factor(week), data = campaign_long) xmodels <- list(m1, m2) # residual balancing weights rbwPanel_fit <- rbwPanel(treatment = d.gone.neg, xmodels = xmodels, id = id, time = week, data = campaign_long) # merge weights into wide-format data campaign_wide2 <- merge(campaign_wide, rbwPanel_fit$weights, by = "id") # fit a marginal structural model (adjusting for baseline confounders) rbw_design <- svydesign(ids = ~ 1, weights = ~ rbw, data = campaign_wide2) msm_rbw <- svyglm(demprcnt ~ cum_neg * deminc + camp.length + factor(year) + office, design = rbw_design) summary(msm_rbw)
In causal mediation analysis, the rbwMed()
function can be used to construct residual balancing weights for estimating the controlled direct effect or the controlled mediator effect with a marginal structural model. The following example illustrates its use by estimating the controlled direct effect of shared democracy (democ
) on public support for war (strike
) at different levels of perceived morality of war (immoral
) for a sample of respondents in a survey experiment.
# models for post-treatment confounders m1 <- lm(threatc ~ ally + trade + h1 + i1 + p1 + e1 + r1 + male + white + age + ed4 + democ, data = peace) m2 <- lm(cost ~ ally + trade + h1 + i1 + p1 + e1 + r1 + male + white + age + ed4 + democ, data = peace) m3 <- lm(successc ~ ally + trade + h1 + i1 + p1 + e1 + r1 + male + white + age + ed4 + democ, data = peace) # residual balancing weights rbwMed_fit <- rbwMed(treatment = democ, mediator = immoral, zmodels = list(m1, m2, m3), interact = TRUE, baseline_x = c(ally, trade, h1, i1, p1, e1, r1, male, white, age, ed4), data = peace) # attach residual balancing weights to data peace$rbw_cde <- rbwMed_fit$weights # fit marginal structural model rbw_design <- svydesign(ids = ~ 1, weights = ~ rbw_cde, data = peace) msm_rbwMed <- svyglm(strike ~ democ * immoral, design = rbw_design) summary(msm_rbwMed)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.