[ Daniel Almirall's Home Page ]

R Implementation of a Weighted-and-Centered Least Squares Estimator of Proximal or Lagged Moderated Effects in mHealth

Accompanying Article

This R code accompanies the following journal article:


This R code implements the weighted and centered least squares method described in Boruvka, Almirall, Witkiewitz and Murphy (2016). The method is useful for examining proximal or lagged effects of time-varying treatment on a time-varying outcome conditional on baseline or time-varying moderators. The methodology is motivated by mHealth applications (where treatments aimed at behavior change and maintenance are provided in near real time, e.g., via a smartphone) using data arising from a micro-randomized trial (MRT). However, the methodology is applicable to any setting with time-varying treatment, outcome and candidate moderators.

The method is extremely easy to implement using over the counter statistical software. In the first two files below, we rely only on R's glm() and lm() functions. The method can be implemented just as easily with other software such as SAS.


Three files are included. The first two files include a useful README at the top.

1. weighting-and-centering-estimator-known-randomization-probs.R

This code implements the method for settings where the numerator treatment probabilities are estimated, but the denominator treatment probabilities are known. This implementation is most likely to be used with data arising from a micro-randomized trial, where the randomizations probabilities are known.

2. weighting-and-centering-estimator-estimated-randomization-probs.R

This code implements the method for settings where both the numerator and denominator treatment probabilities are estimated. This implementation is most likely to be used with observational study data or any data where the denominator probabilities are not known.

3. fakeMRTdata.txt

This file contains data from a simulated micro-randomized trial. This file can be used to test run the code above.

Additional Code

More sophisticated R code, including code to reproduce the results of the simulation experiments in the above-cited paper can be found in the Github repository mHealthModeration.

First Published: 12/14/2016; Last Revised: 12/14/2016

View Daniel Almirall, PhD's profile on LinkedIn