A third variable \(Z\) is a moderator of a treatment \(X\) if it is fixed before treatment, unaffected by it, and the effect of \(X\) on the outcome \(Y\) varies with it. It is a mediator if the treatment moves it and it in turn moves the outcome. These are the MacArthur criteria of Kraemer et al. (2002, 2008). They are claims about causal arrows, so no regression on observational data can settle them. In a randomized experiment, however, two of the three arrows are identified:
The third arrow, \(Z \to Y\), is not randomized, so a “mediator” verdict always means mediator candidate under sequential ignorability.
shift_test() is an exact randomization test of the sharp
null that the treatment left \(Z\)
unchanged for every unit. It combines a mean, a variance, and a
distributional (KS) component, so it catches treatments that push \(Z\) up for some units and down for others,
which a test on the mean misses.
d <- simulate_role_data("cancelling", n = 600, seed = 1)
shift_test(d$Z_post, d$X, B = 999, seed = 2)
#>
#> Randomization test of no effect on Z for any unit (mean, var, ks; min-p combination)
#>
#> Treated: n = 300, mean = -0.009, sd = 1.319
#> Control: n = 300, mean = -0.061, sd = 1.172
#> Component p-values: mean = 0.598, var = 0.029, ks = 0.496
#> Combined p-value = 0.069 (B = 999 permutations)
shift_test(d$Z_post, d$X, stats = "mean", B = 999, seed = 2)
#>
#> Randomization test of no effect on Z for any unit (mean; min-p combination)
#>
#> Treated: n = 300, mean = -0.009, sd = 1.319
#> Control: n = 300, mean = -0.061, sd = 1.172
#> Component p-values: mean = 0.598
#> Combined p-value = 0.598 (B = 999 permutations)With a baseline measure, the test is applied to the post-treatment measure residualized on baseline, which stays exact and gains power:
shift_test(d$Z_post, d$X, z_pre = d$Z_pre, B = 999, seed = 2)
#>
#> Randomization test of no effect on Z for any unit (mean, var, ks; min-p combination, residualized on baseline Z)
#>
#> Treated: n = 300, mean = -0.009, sd = 1.319
#> Control: n = 300, mean = -0.061, sd = 1.172
#> Component p-values: mean = 0.761, var = 0.001, ks = 0.018
#> Combined p-value = 0.002 (B = 999 permutations)Failing to reject is not evidence of no effect.
equivalence_shift() runs the two one-sided tests (Hartman
and Hidalgo 2018) for a pre-specified margin in standard deviations of
\(Z\):
role_test() chains the steps and returns one of:
moderator, mediator, both, neither, post-treatment with no \(Z \to Y\) path, or undetermined. Supplying
the baseline measure is what makes “both” identifiable and removes most
“undetermined” verdicts.
d <- simulate_role_data("both", n = 1000, seed = 4)
role_test(d, treat = "X", outcome = "Y", z_post = "Z_post", z_pre = "Z_pre",
B = 999, seed = 5)
#>
#> Third-variable role test (pre-post design, n = 1000)
#>
#> step estimate p.value result
#> Treatment moved Z (randomization test) 0.002 yes
#> Z unaffected within +/- 0.20 SD (TOST) 0.445 1.000 not established
#> Moderation: X x Z_pre interaction 0.331 0.000 significant
#> Z -> Y path: Z_post | X, Z_pre 0.463 0.000 significant
#>
#> Verdict: both
#> Note: the Z -> Y path is not randomized; 'mediator' means mediator candidate
#> under sequential ignorability. Add a sensitivity analysis.
role_test(d, treat = "X", outcome = "Y", z_post = "Z_post", B = 999, seed = 5)
#>
#> Third-variable role test (post-only design, n = 1000)
#>
#> step estimate p.value result
#> Treatment moved Z (randomization test) 0.002 yes
#> Z unaffected within +/- 0.20 SD (TOST) 0.466 1.000 not established
#> Moderation: X x Z_post interaction (gated) 0.324 0.000 significant
#> Z -> Y path: Z_post | X 0.664 0.000 significant
#>
#> Verdict: mediator
#> Note: the Z -> Y path is not randomized; 'mediator' means mediator candidate
#> under sequential ignorability. Add a sensitivity analysis.
#> Note: post-only design cannot detect moderation by a variable the treatment moved.The post-only call cannot see the moderation, because the variable it would interact with has been moved by the treatment.
role_simulation() compares the naive rule (a significant
\(X \times Z_{post}\) interaction means
moderation) with the gated test in post-only and pre/post designs across
six worlds with a known truth. A small run:
In a full run, the naive rule calls \(Z\) a moderator in roughly 40 percent of samples from the “cancelling” world, where there is no moderator, and turns every “both” world into “moderator”. The pre/post gated test calls \(Z\) a moderator in essentially none of those samples and recovers “both” in about 95 percent.
When only a post-treatment measure of a binary moderator exists,
role_bounds() wraps the nonparametric bounds of Blackwell,
Brown, Hill, Imai, and Yamamoto (2025) from the prepost
package:
data(land_experiment, package = "prepost")
post <- subset(land_experiment, prepost == 1)
role_bounds(post, "treat_comb", "support", "land_insecure", design = "post", sims = 200)
#> $lower
#> [1] -1.23954
#>
#> $upper
#> [1] 1.23954
#>
#> $ci_lower
#> [1] -1.319199
#>
#> $ci_upper
#> [1] 1.319199
#>
#> $post_est
#> [1] 0.48171role_bounds(..., sensitivity = TRUE) returns the bounds
as a function of the share of respondents whose moderator the treatment
could have moved.