Dynamic treatment rule

Description

Creates a dynamic intervention where treatment is determined by a user-supplied function of the covariate history. When contrast() evaluates the intervention, rule receives two arguments:

  • data: the full counterfactual data.table (a copy of fit$data with all columns intact, including lag columns for longitudinal fits). For point treatments this is one row per individual; for longitudinal ICE it is the entire person-period panel at once. The rule is called once per intervention, not once per time step.

  • treatment: the observed (or currently-held) treatment vector, length nrow(data).

rule must return a numeric vector of length nrow(data). To branch on time, reference the time column directly (e.g. data$time == 0). To reference the treatment or covariate history in longitudinal data, use the materialised lag columns (data$lag1_A, data$lag1_L, …) that prepare_data() built from history.

Usage

dynamic(rule)

Arguments

rule A function with signature function(data, treatment) that returns a vector of treatment values of length nrow(data).

Value

A causatr_intervention object.

References

Hernan MA, Robins JM (2025). Causal Inference: What If. Chapman & Hall/CRC. Chapter 19 (dynamic treatment strategies).

See Also

static(), shift(), scale_by(), threshold(), ipsi()

Examples

library("causatr")

# Point treatment: treat if CD4 count is below 200
cd4_rule <- dynamic(\(data, trt) ifelse(data$cd4 < 200, 1, 0))

# Longitudinal: treat at time k if a time-varying confounder triggered
# at time k (the whole panel is passed in one call; the time column
# lets the rule dispatch per-row).
adaptive <- dynamic(\(data, trt) as.integer(!is.na(data$L) & data$L > 0))