# NOT RUN {
## Generate a data set including two confounders (C1 and C2), one binary
## mediator (M), one binary exposure (X), one binary outcome (Y1), and
## one continuous outcome (Y2)
C1=rnorm(2000)>0
C2=rnorm(2000)
X = rnorm(2000)
M= as.numeric(runif(2000)< 1/(1+exp(0-0.9*X+0.1*C1)))
Y1= as.numeric(runif(2000)< 1/(1+exp(-(-2+0.5*X+0.5*M+0.2*C1+0.2*C2))))
Y2= -2+0.9*X-0.6*M+0.2*C1+0.2*C2+rnorm(2000,mean=0,sd=1)
mydata=as.data.frame(cbind(Y1,Y2,M,X,C1,C2))
## Example 1: Mediation analysis for X in change from 0 to 1 on Outcome Y1.
## In the outcome model, we adjusted for C1 and C2. In the mediator model,
## we only adjusted for C1. We calculated the NIE, TE and MP for exposure in
## change from 0 to 1, conditional on C1=0 and C2=1.
result=mediate(data=mydata, outcome="Y1", mediator="M", exposure="X",
binary.outcome=1, binary.mediator=1,
covariate.outcome=c("C1","C2"),
covariate.mediator=c("C1"),
x0=0, x1=1, c.outcome=c(0,1), c.mediator=c(0),
boot=0)
print(result)
## Example 2: Mediation analysis for X in change from 0 to 1 on Outcome Y2.
## In the outcome model, we adjusted for C1 and C2. In the mediator model,
## we did not adjust for any confounders. We calculated the NIE, TE and MP
## for exposure in change from 0 to 1, conditional on C1=0 and C2=1.
result=mediate(data=mydata, outcome="Y2", mediator="M", exposure="X",
binary.outcome=0, binary.mediator=1,
covariate.outcome=c("C1","C2"),
covariate.mediator=NULL,
x0=0, x1=1, c.outcome=c(0,1), c.mediator=NULL,
boot=0)
print(result)
## Example 3: Mediation analysis without any confounding adjustment
## We conduct mediation analysis for X in change from 0 to 1 on Outcome
## Y1, but here no confounders in the outcome and mediator models were
## considered.
result=mediate(data=mydata, outcome="Y1", mediator="M", exposure="X",
binary.outcome=1, binary.mediator=1,
covariate.outcome=NULL,
covariate.mediator=NULL,
x0=0, x1=1, c.outcome=NULL, c.mediator=NULL,
boot=0)
print(result)
# }
Run the code above in your browser using DataLab