Skip to contents
library(causalverse)

The question

A third variable ZZ is a moderator of a treatment XX if it is fixed before treatment, unaffected by it, and the effect of XX on the outcome YY 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:

  • whether XX moves ZZ (randomization identifies the effect of XX on anything measured after it), and
  • whether baseline ZZ modifies the effect of XX (effect modification by a pre-treatment variable).

The third arrow, Z→YZ \to Y, is not randomized, so a “mediator” verdict always means mediator candidate under sequential ignorability.

Did the treatment move Z?

shift_test() is an exact randomization test of the sharp null that the treatment left ZZ unchanged for every unit. It combines a mean, a variance, and a distributional (KS) component, so it catches treatments that push ZZ 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 ZZ:

d0 <- simulate_role_data("neither", n = 2000, seed = 3)
equivalence_shift(d0$Z_post, d0$X, margin = 0.2)
#> 
#> Equivalence test (TOST) of the standardized shift in Z, margin +/- 0.20 SD
#>   Estimate = 0.045 SD, 90% CI [-0.028, 0.119], TOST p = 0.000
#>   Equivalence established.

The gated role test

role_test() chains the steps and returns one of: moderator, mediator, both, neither, post-treatment with no Z→YZ \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.

How often do the rules get it right?

role_simulation() compares the naive rule (a significant X×ZpostX \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:

sims <- role_simulation(n = 400, reps = 1000, B = 499, cores = 4, seed = 2026)
summary(sims)

In a full run, the naive rule calls ZZ 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 ZZ a moderator in essentially none of those samples and recovers “both” in about 95 percent.

Bounds when the moderator was measured after treatment

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.48171

role_bounds(..., sensitivity = TRUE) returns the bounds as a function of the share of respondents whose moderator the treatment could have moved.

References

  • Blackwell, M., Brown, J. R., Hill, S., Imai, K., & Yamamoto, T. (2025). Priming bias versus post-treatment bias in experimental designs. Political Analysis, 33(4), 361-377.
  • Hartman, E., & Hidalgo, F. D. (2018). An equivalence approach to balance and placebo tests. American Journal of Political Science, 62(4), 1000-1013.
  • Kraemer, H. C., Wilson, G. T., Fairburn, C. G., & Agras, W. S. (2002). Mediators and moderators of treatment effects in randomized clinical trials. Archives of General Psychiatry, 59(10), 877-883.
  • Kraemer, H. C., Kiernan, M., Essex, M., & Kupfer, D. J. (2008). How and why criteria defining moderators and mediators differ between the Baron & Kenny and MacArthur approaches. Health Psychology, 27(2, Suppl.), S101-S108.
  • Montgomery, J. M., Nyhan, B., & Torres, M. (2018). How conditioning on posttreatment variables can ruin your experiment and what to do about it. American Journal of Political Science, 62(3), 760-775.

Use this package with your AI assistant

Ask questions about this package in Claude, Cursor, VS Code, or ChatGPT, with answers that cite the section.

https://research-methods-mcp.vercel.app/mcp?src=book-causalverse

Add to CursorInstall in VS Code

Setup for Claude, ChatGPT, and other clients