*! version 1.0.0 18Jun2020

Title

rwrmed -- Causal mediation analysis using regression-with-residuals

Syntax

rwrmed
depvar
[lvars]
[if] [in]
[pweight]
,
avar(varname)
mvar(varname)
a(#)
astar(#)
m(#)
[ mreg(string)
cvar(varlist)
cat(varlist)
nointeraction
cxa
cxm
lxm
noisily
bootstrap[(bootstrap_options)]
model_options
]

lvars are post-treatment covariates

* avar(varname) specify the treatment (exposure) variable

* mvar(varname) specify the mediator variable

* a(#) set the reference level of treatment

* astar(#) set the alternative level of treatment

* m(#) set the level of the mediator at which the controlled direct effect
is evaluated. If there is no treatment-mediator interaction, then the controlled direct effect
is the same at all levels of the mediator and thus an arbitary value can be chosen

mreg(string) specify the form of regression model to be estimated for the mediator variable.
Options are regress, logit, or poisson; default is regress

cvars(varlist) specify the pre-treatment covariates to be included in the analysis

cat(varlist) specify which of the cvars and lvars should be handled as categorical variables

nointeraction specify that a treatment-mediator interaction should not be included in the outcome model

cxa specify that treatment-covariate interactions should be included in the mediator and outcome models

cxm specify that mediator-covariate interactions should be included in the outcome model

lxm specify that mediator-posttreatment interactions should be included in the outcome model

noisily display GSEM output tables; this option is not available with bootstrap

bootstrap[(bootstrap_options)] specify that bootstrap replications be used to estimate the variance-covariance matrix.
All bootstrap options are available

model_options specify any option available for gsem

* required option.

pweights are allowed; see weight.

vce(robust) and vce(cluster clustvar) are allowed.

Description

rwrmed performs causal mediation analysis using regression-with-residuals. Using gsem, two models are estimated: a
model for the mediator conditional on treatment and the pre-treatment covariates (if specified) after centering them
around their sample means, and a model for the outcome conditional on treatment, the mediator, the pre-treatment covariates
(if specified) after centering them around their sample means, and the post-treatment covariates (if specified) after
centering them around their estimated conditional means given all prior variables. Thus rwrmed allows for the presence of treatment-induced confounders,
which are post-treatment covariates that confound the mediator-outcome relationship.

rwrmed uses regress for the outcome, and requires the user to specify an appropriate form for the mediator model (regress, logit, or poisson).
Post-treatment covariates are residualized with respect to treatment and the pre-treatment covariates.

rwrmed provides estimates of the controlled direct effect and then the randomized intervention analogues to
the natural direct effect, the natural indirect effect, and the total effect when a set of measured post-treatment covariates
are included in lvars. When post-treatment covariates are not included in lvars, the command instead
provides estimates of the conventional natural direct and indirect effects and the conventional total effect.

By default, standard errors and confidence intervals for mediation effect estimates are derived by margins with
the vce(unconditional) option. However, bootstrapped standard errors are preferred when post-treatment covariates
are included in the model (i.e. by specifying lvars) because the boostrap procedure properly accounts for the uncertainty
that arises from estimating the models used to residualize the post-treatment covariates, whereas the analytic standard errors do not.

Assumptions

Let C be the measured pre-treatment covariates included in cvars(varlist), and let L be the measured post-treatment covariates
included in lvars(varlist). Obtaining consistent estimates of the controlled direct effect requires two main assumptions:

(1) There are no unmeasured treatment-outcome confounders given C

(2) There are no unmeasured mediator-outcome confounders given C and L

Obtaining consistent estimates of the randomized intervention analogues to natural direct and indirect effects requires assumptions (1)
and (2) and then an additional assumption:

(3) There are no unmeasured treatment-mediator confounders given C

Note that assumptions (1) and (3) are satisified by random assignment of the treatment variable. See references for further details.

Options

avar(varname) specifies the treatment (exposure) variable; avar() is required.

mvar(varname) specifies the mediator variable; mvar() is required.

a(#) sets the reference level of treatment; a() is required.

astar(#) sets the alternative level of treatment. Together, (astar - a) defines
the treatment contrast of interest; astar() is required.

m(#) sets the level of the mediator at which the controlled direct effect
is evaluated. If there is no treatment-mediator interaction, then the controlled direct effect
is the same at all levels of the mediator and thus an arbitrary value can be chosen; m() is required.

mreg(string) specifies the form of regression model to be estimated for the mediator variable.
Options include regress, logit, and poisson; default is regress.

cvars(varlist) specifies the pre-treatment covariates to be included in the analysis.

cat(varlist) specifies which of the cvars and lvars should be handled as categorical variables.
For multi-categorical variables, rwrmed generates dummy variables for each level and then residualizes them individually. A warning
message will be issued if the logit model produces perfect predictions, resulting in dropped observations. The program will terminate
if the logit model cannot converge. In both of these cases (dropped observations or model non-convergence), the user should consider either
collapsing the multi-categorical variable into fewer categories, or specify it as a continuous variable (by not adding it to cat()).

nointeraction specifies that a treatment-mediator interaction should not be included in the outcome model. When not specified, rwrmed
will generate a treatment-mediator interaction term.

cxa specifies that treatment-covariate interactions be included in the mediator and outcome models.

cxm specifies that mediator-covariate interactions be included in the outcome model.

lxm specifies that mediator-posttreatment interactions be included in the outcome model.

noisily displays the GSEM output tables; this option is not available with bootstrap.

bootstrap[(bootstrap_options)] specifies that bootstrap replications are to be used to estimate the variance-covariance matrix.
All bootstrap options are available. Specifying bootstrap without options uses the default bootstrap settings.

model_options allows the user to specify any option available for gsem

Examples

Setup

. use depression.dta

A continuous outcome (cesd40), a continuous mediator (ses), a binary treatment (college), nine pre-treatment covariates (of which five are categorical), three post-treatment covariates,
no interaction between treatment and mediator, and the default analytic method for estimation:

. rwrmed cesd40 cesd92 prmarr98 transitions98, avar(college) mvar(ses) cvar(male black test_score educ_exp father hispanic urban educ_mom num_sibs) ///
cat(male black father hispanic urban) a(0) astar(1) m(0) mreg(reg) nointer

Same as above, but include all interactions and specify that 1000 bootstrap replications be used for estimation:

. rwrmed cesd40 cesd92 prmarr98 transitions98, avar(college) mvar(ses) cvar(male black test_score educ_exp father hispanic urban educ_mom num_sibs) ///
cat(male black father hispanic urban) a(0) astar(1) m(0) mreg(reg) cxa cxm lxm boot(reps(1000) seed(1234))

Display all available post-estimation bootstrap confidence intervals:

. estat boot, all

Same as the first model, but now include the survey weights (note: bootstrap cannot be used in conjunction with weights):

. rwrmed cesd40 cesd92 prmarr98 transitions98 [pw=weights], avar(college) mvar(ses) cvar(male black test_score educ_exp father hispanic urban educ_mom num_sibs) ///
cat(male black father hispanic urban) a(0) astar(1) m(0) mreg(reg) nointer

This model includes a binary mediator. All interactions are included and the bootstrap is specified:

. rwrmed cesd40 cesd92 prmarr98 transitions98, avar(college) mvar(ses_bin) cvar(male black test_score educ_exp father hispanic urban educ_mom num_sibs) ///
cat(male black father hispanic urban) a(0) astar(1) m(0) mreg(logit) cxa cxm lxm boot(reps(1000) seed(1234))

This model includes a mediator which is a count variable. All interactions are included and the bootstrap is specified:

. rwrmed cesd40 cesd92 prmarr98 transitions98, avar(college) mvar(ses_count) cvar(male black test_score educ_exp father hispanic urban educ_mom num_sibs) ///
cat(male black father hispanic urban) a(0) astar(1) m(0) mreg(poisson) cxa cxm lxm boot(reps(1000) seed(1234))

Stored results

When margins is used for estimating VCE, rwrmed stores the following in r():

macros:

r(NDEtype) natural direct effect type

r(NDEtext) natural direct effect description

r(NIEtype) natural indirect effect type

r(NIEtext) natural indirect effect description

r(ATEtype) average total effect type

r(ATEtext) average total effect description

and also stores the following in e():

macros:

e(cmdline) command as typed

e(properties) b V

matrices:

e(b) vector of transformed coefficients

e(V) estimated variance-covariance matrix of the transformed
coefficients

When the bootstrap method is specified for estimating VCE, rwrmed stores the following in r():

macros:

r(NDEtype) natural direct effect type

r(NDEtext) natural direct effect description

r(NIEtype) natural indirect effect type

r(NIEtext) natural indirect effect description

r(ATEtype) average total effect type

r(ATEtext) average total effect description

and also stores the following in e():

macros:

e(cmdname) command name from command

e(cmd) same as e(cmdname) or bootstrap

e(command) command

e(cmdline) command as typed

e(prefix) bootstrap

e(title) title in estimation output

e(strata) strata variables

e(cluster) cluster variables

e(rngstate) random-number state used

e(size) from the size(#) option

e(exp#) expression for the #th statistic

e(ties) ties, if specified

e(mse) mse, if specified

e(vce) bootstrap

e(vcetype) title used to label Std. Err.

e(properties) b V

matrices:

e(b) observed statistics

e(b_bs) bootstrap estimates

e(reps) number of nonmissing results

e(bias) estimated biases

e(se) estimated standard errors

e(z0) median biases

e(accel) estimated accelerations

e(ci_normal) normal-approximation CIs

e(ci_percentile) percentile CIs

e(ci_bc) bias-corrected CIs

e(ci_bca) bias-corrected and accelerated CIs

e(V) bootstrap variance-covariance matrix

e(V_modelbased) model-based variance

functions:

e(sample) estimation sample

Citation of rwrmed

rwrmed is not an official Stata command. It is a free contribution
to the research community, like a paper. Please cite it as such:

Linden, A., C. Huber, G. T. Wodtke. 2020. RWRMED: Stata module for conducting causal mediation analysis using regression-with-residuals.

Authors

Ariel Linden

President, Linden Consulting Group, LLC

alinden@lindenconsulting.org

Chuck Huber

Associate Director of Statistical Outreach, StataCorp, LLC

chuber@stata.com

Geoffrey T. Wodtke

Department of Sociology

University of Chicago

wodtke@uchicago.edu

References

Wodtke, G. T. and D. Almirall. 2017. Estimating Moderated Causal Effects with Time-varying Treatments and Time-varying Moderators: Structural Nested Mean Models and Regression-with-residuals. Sociological Methodology 47: 212-245.

Wodtke, G. T., and X. Zhou. 2020. Effect Decomposition in the Presence of Treatment-induced Confounding: A Regression-with-Residuals Approach. Epidemiology 31: 369-375.

Wodtke, G. T., Z. Alaca, and X. Zhou. 2020. Regression-with-residuals Estimation of Marginal Effects. Journal of the Royal Statistical Society Series A 183: 311-332.

Zhou, X., and Wodtke, G. T. 2019. A Regression-with-residuals Method for Estimating Controlled Direct Effects. Political Analysis 27: 360-369.

Acknowledgments

We thank Enrique Pinzon of StataCorp for his helpful suggestion to use margins with the vce(unconditional) option for estimating analytic standard errors

Also see

Online: gsem, margins, bootstrap (if installed)