Learn R Programming

⚠️There's a newer version (0.4.1) of this package.Take me there.

MRTAnalysis

The MRTAnalysis package provides functions to conduct post-study analyses of Micro-Randomized Trials (MRTs), focusing on estimating causal excursion effects.

  • Proximal outcomes (measured shortly after each treatment decision point):

    • wcls(): Primary analysis for continuous proximal outcomes. Implements weighted and centered least squares (the $k=1$ special case of Boruvka et al., 2018).
    • emee(): Primary analysis for binary proximal outcomes. Implements the estimator for marginal excursion effect (the $\Delta=1$ special case of Qian et al., 2021).
    • emee2(): Variant of emee(), centering treatment in the residual term. Basis for the sample size calculator in MRTSampleSizeBinary.
  • Distal outcomes (measured once at end of study):

    • dcee(): Exploratory analysis for distal causal excursion effects in MRTs (Qian et al. 2025). Supports linear models and machine-learning learners (lm, gam, random forest, ranger, SuperLearner) with optional cross-fitting.
  • Mediated Effects Through Time-Varying Mediators to Distal Outcomes

      • mcee(): Exploratory analysis for mediated causal excursion effects in MRTs, estimating natural direct excursion effects (NDEE) and natural indirect excursion effects (NIEE) through time-varying mediators. Supports GLM, GAM, random forest, ranger, and SuperLearner learners for fitting nuisance parameters.

Installation

You can install the package from CRAN:

install.packages("MRTAnalysis")

Usage

See vignettes for detailed examples:

library(MRTAnalysis)

# Proximal outcome analysis (continuous)
fit1 <- wcls(
  data = data_mimicHeartSteps,
  id = "userid", outcome = "logstep_30min",
  treatment = "intervention", rand_prob = 0.6,
  moderator_formula = ~1,
  control_formula = ~logstep_pre30min,
  availability = "avail"
)
summary(fit1)

# Distal outcome analysis
fit2 <- dcee(
  data = data_distal_continuous,
  id = "userid", outcome = "Y",
  treatment = "A", rand_prob = "prob_A",
  moderator_formula = ~1,
  control_formula = ~X,
  availability = "avail",
  control_reg_method = "lm"
)
summary(fit2)

# Mediation with distal outcome
fit3 <- mcee(
  data = data_time_varying_mediator_distal_outcome,
  id = "id", dp = "dp",
  outcome = "Y", treatment = "A", mediator = "M",
  availability = "I", rand_prob = "p_A",
  time_varying_effect_form = ~1,                # constant effects over time
  control_formula_with_mediator = ~ dp + M + X, # adjustment set
  control_reg_method = "glm"
)
summary(fit3)

References

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

  • Qian, T., Yoo, H., Klasnja, P., Almirall, D., & Murphy, S. A. (2021). Estimating time-varying causal excursion effects in mobile health with binary outcomes. Biometrika, 108(3), 507–527. doi:10.1093/biomet/asaa070

  • Qian, T. (2025). Distal Causal Excursion Effects: Modeling Long-Term Effects of Time-Varying Treatments in Micro-Randomized Trials. arXiv:2502.13500.

  • Qian, T. (2025). Dynamic Causal Mediation Analysis for Intensive Longitudinal Data. arXiv:2506.20027.

Copy Link

Version

Install

install.packages('MRTAnalysis')

Monthly Downloads

301

Version

0.3.1

License

GPL-3

Maintainer

Tianchen Qian

Last Published

November 5th, 2025

Functions in MRTAnalysis (0.3.1)

.mcee_check_no_missing_vars

Check data frame columns for missing/infinite values
.mcee_check_id_rows_grouped

Check that rows for each subject appear in contiguous blocks
.mcee_fit_nuisance

Fit a single nuisance component with flexible learner support
.mcee_drop_var_from_rhs

Remove a variable from RHS-only formula
.mcee_validate_clipping

Validate clipping bounds for probability predictions
.mcee_check_formula_mediator

Check config formula for inclusion/exclusion of mediator
.mcee_check_dp_strictly_increasing

Check that decision points are strictly increasing within each subject
.mcee_check_outcome_constant_within_id

Check that outcome is constant within each subject (required for distal outcomes)
.mcee_check_no_missing_vec

Check numeric vector for missing/infinite values
.mcee_resolve_rand_prob

Resolve randomization probability from column name or scalar
.mcee_require_cols

Check that required columns exist in data frame
.mcee_message_if_no_availability_provided

Print informative message if no availability column provided
.mcee_print_coef_table

Print formatted coefficient table for MCEE results
mcee

Mediated Causal Excursion Effects for MRTs (streamlined)
mcee_config_maker

Build a nuisance-configuration object for mcee_general()
.mcee_compact_model_info

Generate compact one-line description of nuisance model object
.mcee_vars_in_config

Extract variables from nuisance configuration formula
.mcee_check_time_varying_effect_form

Validate time-varying effect formula structure
emee

Estimates the causal excursion effect for binary outcome MRT
mcee_config_lm

Configure linear model for MCEE nuisance parameters
mcee_config_known

Configure known constant values for MCEE nuisance parameters
mcee_config_glm

Configure GLM for MCEE nuisance parameters
.mcee_vars_in_rhs

Extract variable names from RHS-only formula
emee2

Estimates the causal excursion effect for binary outcome MRT
mcee_config_gam

Configure GAM for MCEE nuisance parameters
print.summary.mcee_fit

Print method for summary of MCEE fits
mcee_userfit_nuisance

Mediated Causal Excursion Effects with user-supplied nuisance predictions
mcee_config_sl

Configure SuperLearner for MCEE nuisance parameters
mcee_config_rf

Configure Random Forest for MCEE nuisance parameters
mcee_general

Mediated Causal Excursion Effects (configurable nuisance models)
mcee_config_ranger

Configure Ranger Random Forest for MCEE nuisance parameters
.mcee_validate_method

Validate that learning method is supported
mcee_helper_stage2_estimate_mcee

Stage-2 MCEE parameter estimation given nuisance predictions
mcee_helper_stage1_fit_nuisance

Fit all nuisance models for MCEE Stage 1
wcls

Estimates the causal excursion effect for continuous outcome MRT
mcee_helper_2stage_estimation

Two-stage helper for mediated causal excursion effects (MCEE)
mcee_config_sl_user

Configure SuperLearner with user-specified library for MCEE nuisance parameters
summary.dcee_fit

Summary for DCEE fits
summary.emee_fit

Summarize Causal Excursion Effect Fits for MRT with Binary Outcomes
summary.wcls_fit

Summarize Causal Excursion Effect Fits for MRT with Continuous Outcomes
summary.mcee_fit

Summarize an mcee fit
data_time_varying_mediator_distal_outcome

Example longitudinal dataset with time-varying mediator and distal outcome
.mcee_check_control_formula

Validate control formula excludes treatment and outcome
.mcee_build_weights

Build per-row weights omega(i,t) for MCEE estimation
dcee

Distal Causal Excursion Effect (DCEE) Estimation
.mcee_assert_df

Assert that input is a data frame
.mcee_build_f_matrix

Build basis matrix f(t) from time-varying effect formula
.mcee_check_binary_col

Validate binary column coding
data_distal_continuous

A synthetic data set of an MRT with continuous distal outcome
data_binary

A synthetic data set of an MRT with binary proximal outcomes
data_mimicHeartSteps

A synthetic data set that mimics the HeartSteps V1 data structure to illustrate the use of [wcls()] function for continuous proximal outcomes
.mcee_default_family

Select default GLM family based on nuisance parameter type
.mcee_core_rows

Numerical core implementing MCEE estimation mathematics