I am skipping this week (because I'll be out of town).
Specify a hypothesis regarding a binary exposure, a continuous mediator, and a continuous outcome. Specify how each variable affects its children (i.e., how the exposure influences the mediator) and the distribution of the random or unmeasured determinants of the child variables.
Research question: Does septic shock affect children’s length of stay in the PICU directly and/or is this relationship mediated by volume of fluid resuscitation received (ml/kg)?
Septic shock (Y/N) --> fluid resuscitation volume (ml/kg) --> PICU LOS
Using the software of your choice, generate a population with 10000 people under a causal structure consistent with this hypothesized causal structure. Use a conventional Baron-Kenny decomposition to estimate the direct and indirect effects of the exposure on the outcome.
x=septic shock
c=source of infection
m=fluid resuscitation volume
y=PICU LOS
Direct effect of x on y, not mediated by m is 1
Indirect effect of x on y, via m is 2*.25 = 0.5
Total effect of x on y is 1+.5=1.5 and there is no interaction between m and x and c=0
Controlled direct effect = CDE= Y(x=1,m)-Y(x=0,m)
Total Natural Direct Effect = total NDE = Y(x=1, m=1)- Y(x=0, m=1)
Pure Natural Direct Effect = pure NDE = Y(x=1, m=0)- Y(x=0, m=0)
Total Natural Indirect Effect = total NIE = Y(x=1, m=1)- Y(x=1, m=0)
Pure Natural Indirect Effect= pure NIE = Y(x=0, m=1)- Y(x=0, m=0)
Now introduce a confounder of the mediator and outcome (C) into your causal model. Define the new causal models and simulate a new data set. Use a conventional decomposition without control for the confounder first and then with control for the confounder to derive estimates of the direct and indirect effects of exposure on outcome.
Baron-Kenny decomposition to estimate the direct and indirect effects of the exposure on the outcome.
Total effect of x on y, including mediation by m, not controlling for C is 1.6
The effect of m on y is 2.1
Biased direct effect of x on y, not mediated by m, not controlling for C is -4
Then, if control for c the direct effect of x on y, not mediated by m, controlling for C is 1
Indirect effect estimated w/o control for c is: 5.637
Indirect effect estimated w/ control for c is: .631, which is approximately what the above indirect effect of x on y (0.5) plus some random error
Total effect is: 1.64, which is approximately what the above total effect of x on y (1.5) plus some random error
Paramed gives the following, which is more similar to the above, true values:
CDE:Controlled direct effect= 1.0110056
NDE:natural direct effect = 1.0110515
NIE:natural indirect effect= .50238486
MTE:marginal total effect = 1.5134364
Create a new version of the mediator that represents a badly measured version of that variable, for example by taking the original variable and adding some random noise to it. Now use that mediator to evaluate the direct and indirect effects.
Baron-Kenny decomposition to estimate the direct and indirect effects of the exposure on the outcome with the noisy mediator.
Total effect of x on y, including mediation by m, not controlling for C is 1.6
The effect of m on y is 1.959073
Biased direct effect of x on y, not mediated by m, not controlling for C is -3.340545
Direct effect of x on y, not mediated by m, controlling for C is 1.115947
Now, because we have introduced error into the measurement of the mediator, the total effect doesn’t change but the effect of m on y increases, the biased direct of x on y decreases, and the direct effect of x on y increases slightly (therefore the indirect effect of x on y must decrease slightly).
Estimating the CDE
Direct Effect of x on y = 1.011006
The average value of y if x is set to 0 and m is set to 0 = -.120016
The average value of y if x is set to 0 and m is set to 0 = .8909896
Estimated direct effect of x, setting m to 0, is: .8909896-(-.120016) = 1.011
The counterfactual value of y setting x to 0 = -.1909555
The counterfactual value of y setting x to 1 = 1.449322
Estimated total effect of x on y is: 1.449322 – (-.1909555) = 1.64
Paramed check gives almost identical answers
cde:controlled direct effect= 1.0110056
nde:natural direct effect = 1.011025
nie:natural indirect effect= .16823403
mte:marginal total effect = 1.179259
Bonus
The counterfactual value of y setting x to 1 and m to the value it would take if x were set to 0= .8831828
The counterfactual value of y setting x to 1 and m to the value it would take if x were set to 1)= 1.385568
The natural indirect effect of x on y, mediated by m is: 1.385568- .8831828 = .502
Do file is attached
Specify a hypothesis regarding a binary exposure, a continuous mediator, and a continuous outcome. Specify how each variable affects its children (i.e., how the exposure influences the mediator) and the distribution of the random or unmeasured determinants of the child variables.
Research hypothesis: Does ApoE4 status influences cognitive function in late life mediated by presence of neurofibrillary tangles?
ApoE4 --> Neurofibrillary tangles --> Cognitive Function
Using the software of your choice, generate a population with 10000 people under a causal structure consistent with this hypothesized causal structure.
Data generating rules
ApoE4 prevalence 40%
Density neurofibrillary tangles [nft = 5 + (9*apoe4) + rnormal()]
Cognition = (-0.4*apoe4) + (-0.01*nft) + runiform()
. sum cognition
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
cognition | 10,000 .4321396 .3055797 -.5397511 .9700972
Use a conventional Baron-Kenny decomposition to estimate the direct and indirect effects of the exposure on the outcome.
With the formula:
Direct effect of ApoE4 is -0.4
Indirect effect of ApoE4 is -0.01*9 = -0.09
Total effect of ApoE4 is -0.4 + (-0.09) = -0.49
Estimated with a regression model:
Total effect of ApoE4 is -0.492
Direct effect of ApoE4 is -0.371
Indirect effect of ApoE4 is -0.492-(-0.371) = -0.121
Now introduce a confounder of the mediator and outcome © into your causal model. Define the new causal models and simulate a new data set. Use a conventional decomposition without control for the confounder first and then with control for the confounder to derive estimates of the direct and indirect effects of exposure on outcome.
Data generating rules for the confounder
Prevalence of the confounder (genx) 10%
Effect on neurofibrillary tangles
nft = 5 + (9*apoe4) + (-2*genx) + rnormal()
Effect on the outcome (cognitive function)
cognition = (-0.4*apoe4) + (-0.01*nft) + (0.1*genx) + runiform()
Direct effect without the genx -.3119716
Direct effect with the confounder -.4231034
*The unbiased (confounder adjusted) estimate was closer to the true indirect effect (-0.09)
Indirect effect estimated w/o control for c is: -.181
Indirect effect estimated w/ control for c is: -.07
Create a new version of the mediator that represents a badly measured version of that variable, for example by taking the original variable and adding some random noise to it. Now use that mediator to evaluate the direct and indirect effects.
The biased estimate was closer to the true indirect effect (-0.09)
Indirect effect estimated w/o control for c is: -.111
Indirect effect estimated w/ control for c is: -.039
Total effect is: -.493
Now try estimating the CDE:
. sum cf_y_x0_m0 if copy==2
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
cf_y_x0_m0 | 10,000 .5095942 .0274971 .5005004 .5927296
. * What the average potential outcome for y if x is set to 1 and m is set to 0?
. sum cf_y_x1_m0 if copy==3
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
cf_y_x1_m0 | 10,000 .0207165 .0274971 .0116227 .1038519
Estimated direct effect of ApoE4, setting m to 0, is: -.489. The direct effect of ApoE4 in my formula was -0.4
/* Bonus hw if you're having fun.
Go back to your original data (before you calculated the CDE)
The natural indirect effect of ApoE4 on Cognition mediated by nft is: -.031. The indirect effect in the data generating rules was -0.09, the natural indirect effect is smaller probably because of random noise or because we are estimating potential outcomes. I am not sure why.
With the Stata medeff command I found that the total natural indirect effect was -0.032 and the pure natural indirect effect was -0.0746.
1. Specify a hypothesis regarding a binary exposure, a continuous mediator, and a continuous outcome. Specify how each variable affects its children (i.e., how the exposure influences the mediator) and the distribution of the random or unmeasured determinants of the child variables.

Hypothesis: The effect of childhood poverty on income is mediated by years of education.
2. Using the software of your choice, generate a population with 10000 people under a causal structure consistent with this hypothesized causal structure. Use a conventional Baron-Kenny decomposition to estimate the direct and indirect effects of the exposure on the outcome.
DATA GENERATING RULES
gen poverty = runiform()<0.20
gen eduyears = 0.75*poverty + rnormal()
gen income = 0.75*poverty + 2*eduyears + rnormal()
BASED ON THE DATA GENERATING RULES ABOVE…
Direct effect of poverty on income, not mediated through years of
education = 0.75
Indirect effect of poverty on income, via years of education =
0.75*2 = 1.5
Total effect of poverty on income = 0.75+1.5 = 2.25
BARON-KENNY DECOMPOSITION RESULTS:
Direct effect: 0.723
Indirect effect: 1.536
Total effect: 2.259
3. Now introduce a confounder of the mediator and outcome (C) into your causal model. Define the new causal models and simulate a new data set. Use a conventional decomposition without control for the confounder first and then with control for the confounder to derive estimates of the direct and indirect effects of exposure on outcome.

NEW DATA GENERATING RULES
gen poverty = runiform()<0.20
gen eduqual = rnormal()
gen eduyears = 0.75*poverty + eduqual + rnormal()
gen income = 0.75*poverty + 2*eduyears + eduqual + rnormal()
BARON-KENNY DECOMPOSITION RESULTS:
Indirect effect without controlling for confounder: 1.838
Indirect effect with controlling for confounder: 1.467
Total effect: 2.196
CONFIRMING RESULTS WITH PARAMED
Controlled direct effect: 0.754
Natural direct effect: 0.755
Natural indirect effect: 1.46
Marginal total effect: 2.22
CONFIRMING RESULTS WITH MEDEFF
ACME: 1.487
Direct effect: 0.730
Total effect: 2.217
4. Create
a new version of the mediator that represents a badly measured version of that
variable, for example by taking the original variable and adding some random
noise to it. Now use that mediator to
evaluate the direct and indirect effects.
NEW DATA GENERATING RULES
gen bias = rnormal()*0.8
gen eduyears_biased = eduyears + bias
BARON-KENNY DECOMPOSITION RESULTS:
Direct effect: 1.305
Indirect effect: 0.892
Total effect: 2.196
5. Now
try estimating the CDE:
Controlled direct effect of x, setting M=0: 0.818
Estimated total effect of x on y: 2.253
CONFIRMING RESULTS WITH PARAMED
Controlled direct effect: 0.818
Natural direct effect: 0.818
Natural indirect effect: 0.25
Marginal total effect: 1.07
6. BONUS:
Go back to the original data (prior to estimating the CDE) and estimate the natural
indirect effect of X on Y, mediated by M.
Natural indirect effect of x on y, mediated by m: 1.442
Specify a hypothesis regarding a binary exposure, a continuous mediator, and a continuous outcome. Specify how each variable affects its children (i.e., how the exposure influences the mediator) and the distribution of the random or unmeasured determinants of the child variables.
Population: Ugandan Adults
Exposure: Kaposi sarcoma (KS) (y/n)
Outcome Quality of life
Mediator: Number of Kaposi sarcoma lesions
KS itself may have a decreasing effect of quality of life. Kaposi sarcoma commonly presents with cutaneous lesions, we wonder what effect (if any) the number of cutaneous lesions itself may have an effect on quality of life.
Using the software of your choice, generate a population with 10000 people under a causal structure consistent with this hypothesized causal structure. Use a conventional Baron-Kenny decomposition to estimate the direct and indirect effects of the exposure on the outcome.
Data generation:
Data arbitrarily follows data from a case-control study (Ziegler. Risk factors for Kaposi’s sarcoma in HIV-positive subjects in Uganda. AIDS. 11(13):1619-1626.) We use a KS+ proportion of 40%. Direct of KS on QOL is 5. Indirect effect of KS on QOL through number of lesions by .05 per lesion.
[QOL = 5*KS + .05*number of lesiosn + rnormal()]
gen ks = runiform()<.4
gen ksnum = .5*ks + runiform()
gen qol = 5*ks + .05*ksnum + runiform()
Baron-Kenny Decompensation results:
Direct Effect: 5.018
Indirect Effect: .501
Total Effect: 5.519
Now introduce a confounder of the mediator and outcome (C) into your causal model. Define the new causal models and simulate a new data set. Use a conventional decomposition without control for the confounder first and then with control for the confounder to derive estimates of the direct and indirect effects of exposure on outcome.
Confounder: nevirapine therapy (y/n)
gen ks = runiform()<.4
gen art = runiform
gen ksnum = .5*ks + art + runiform()
gen qol = 5*ks + .05*ksnum + art + runiform()
Baron-Kenny Decompensation results:
Indirect Effect without control: -.01
Indirect Effect with control .016
Total Effect: 5.026
paramed:
Controlled direct effect: 5.022
Natural direct effect: 5.013
Natural indirect effect: .014
Marginal total effect: 5.027
Create a new version of the mediator that represents a badly measured version of that variable, for example by taking the original variable and adding some random noise to it. Now use that mediator to evaluate the direct and indirect effects.
Baron-Kenny Decompensation results:
Direct Effect with confounder: 5.022
Indirect Effect with confounder: .001
Total Effect: 5.027
Now try estimating the CDE:
Controlled direct effect: 4.977
Estimated total effect of KS on QOL: 5.021
It took me forever, but here it is (attached)
This was my skip week.