
Simulate Experiments With a Known Third-Variable Role
Source:R/third_variable_roles.R
simulate_role_data.RdGenerates a randomized experiment with a binary treatment X, a
third variable measured before (Z_pre) and after (Z_post)
treatment with measurement error, and an outcome Y, in one of six
worlds whose true role is known.
Usage
simulate_role_data(
world = c("neither", "moderator", "mediator", "curved_mediator", "cancelling", "both"),
n = 400,
me = 0.3,
seed = NULL
)Details
The six worlds:
"neither"Treatment does not move \(Z\); \(Z\) affects \(Y\) but does not modify the effect of \(X\).
"moderator"Baseline \(Z\) modifies the effect of \(X\); treatment does not move \(Z\).
"mediator"Treatment moves \(Z\), which affects \(Y\) linearly.
"curved_mediator"As
"mediator"with a convex \(Z \to Y\) effect, which makes baseline \(Z\) a genuine effect modifier: the true role is "both"."cancelling"Treatment moves \(Z\) up for some units and down for others, the units it moves up benefit more, and baseline \(Z\) modifies nothing: not a moderator.
"both"Treatment moves \(Z\) and baseline \(Z\) modifies the effect.
Examples
head(simulate_role_data("cancelling", n = 200, seed = 1))
#> X Z_pre Z_post Y
#> 1 1 0.3699207 0.58725067 2.0750806
#> 2 0 -0.4376004 -0.03467079 -0.1434375
#> 3 0 -0.9648468 -0.64224481 -0.9218453
#> 4 1 -0.3519595 0.82080141 0.6816605
#> 5 0 -1.3126439 -2.98953246 -0.8257504
#> 6 1 0.5554119 1.68980644 0.5397130