Exercise: An indirect effect with and without hidden confounding

Exercise: An indirect effect with and without hidden confounding#

A randomized exposure predicts a mediator and an outcome. Randomizing the exposure does not by itself remove mediator–outcome confounding.

See the shared submission guidance and rubric. Run from book/exercises; the setup below supplies data, not completed answers.

# Shared simulator and small utility functions; no live service is called.
source("../R/course.R")
set.seed(118)
n <- 500
base <- data.frame(x=rbinom(n,1,.5), u=rnorm(n), e_m=rnorm(n), e_y=rnorm(n))
make_case <- function(confounding=0) {
  m <- .8*base$x + confounding*base$u + base$e_m
  y <- .3*base$x + .7*m + confounding*base$u + base$e_y
  data.frame(x=base$x,m=m,y=y,u=base$u)
}
clean <- make_case(0); confounded <- make_case(1)

1. Estimate a known indirect path#

For clean, fit m~x and y~x+m. Calculate ab and compare with the generating .8.7. Draw the causal graph and state what randomization of x does and does not establish. Bootstrap complete rows for an interval, keeping the two fitted paths paired within each resample.

# Estimate the paths and bootstrap their product.

Your explanation and evidence:

2. Hide a common cause#

Repeat on confounded while omitting u, then include u in both equations as an oracle analysis. Compare indirect-path estimates. Explain why real analysts often cannot make the oracle adjustment and why a precise bootstrap interval does not solve confounding.

# Compare clean, omitted-u, and oracle analyses.

Your explanation and evidence:

3. Keep moderation distinct#

Create a separate outcome y_mod = .3*x + .5*u + 1.2*x*u + e_y. Fit x*u and plot the exposure contrast at u=-1,0,1. Explain how this conditional effect differs from the indirect path through m; do not dichotomize u.

# Fit and interpret the interaction on the new outcome.

Your explanation and evidence:

What to submit#

A causal graph, a three-analysis indirect-path table, and a continuous-moderation plot. Identify the assumptions behind causal language in each part.