R code for assessing time-varying causal effect moderation in mobile health

Our software resources are permanently archived at https://scholarsphere.psu.edu/collections/v41687m23q. Our software is no longer supported by the Help Desk. For the latest software and research updates, visit Susan Murphy’s website.



Download the MHealth Moderation R code. (Note this download comes from GitHub and not from our servers.)


R code posted at this GitHub repository illustrates how data from a micro-randomized trial can be analyzed to assess time-varying causal effect moderation following Boruvka et al. (2018). As illustrated in the repository’s script “example.R,” point estimates for moderated proximal and delayed treatment effects can be obtained using base R functions. If the treatment probabilities are not fixed, subjects are not always available, or the sample size is small (n < 50), variance estimates require some additional effort to obtain; the needed routines are implemented in the script “xgeepack.R” and an illustration of variance estimation is provided in “example.R.” The same illustration with use of the contributed R package geepack (Højsgaard, Halekoh, and Yan, 2006) is provided in “example_geepack.R.” To calculate lagged and rolling variables, both “example.R” and “example_geepack.R” use zoo (Zeileis and Grothendieck, 2005) along with some helper functions in the repository script “xzoo.R”. This contributed R package is not necessary for point or variance estimation, provided that the user calculates the needed variables directly.

Recommended Citation

If you use this code in your own research, please cite the article listed below.

Boruvka, A., Almirall, D., Witkiewitz, K., & Murphy, S. A. (2018). Assessing time-varying causal effect moderation in mobile health, Journal of the American Statistical Association113(523):1112-1121, DOI: 10.1080/01621459.2017.1305274

Additional References

Højsgaard, S., Halekoh, U. & Yan, J. (2006) The R package geepack for generalized estimating equations, Journal of Statistical Software, 15(2):1-11.

Zeileis, A. & Grothendieck, G. (2005). zoo: S3 infrastructure for regular and irregular time series. Journal of Statistical Software, 14(6):1-27. DOI: 10.18637/jss.v014.i06