
Build a DirectEffects::sequential_g() formula from a DAG (and optionally a base model formula)
Source:R/seqg_formula.R
seqg_formula.RdReturns a formula of the form Y ~ A + X | Z | M required by
DirectEffects::sequential_g(), where:
Ais the treatment (exposure),Xare baseline covariates,Zare intermediate (post-treatment) covariates affected by A that affect both M and Y,Mare mediators (blip-down model terms).
If formula is provided, X is (by default) constrained to the RHS terms that
appear in that base formula (mirroring DAGassist's current ACDE construction).
Usage
seqg_formula(
dag,
formula = NULL,
data = NULL,
exposure = NULL,
outcome = NULL,
m = NULL,
x = NULL,
z = NULL,
include_descendants = FALSE,
fe = NULL,
fe_as_factor = TRUE,
drop_fe_collinear = TRUE,
strict_rhs = TRUE,
return_parts = TRUE
)Arguments
- dag
A
dagittyobject (or a character DAGitty string).- formula
Optional base formula (or an engine call like
fixest::feols(...)). If supplied, the function uses its RHS to define candidate covariates and to pick up fixed-effects/grouping variables (which can be added to X).- data
Optional data.frame. Used to (i) drop terms that do not exist in the data, and (ii) optionally drop covariates collinear with fixed effects (important for seqg stability).
- exposure, outcome
Optional. If missing, inferred from
dagitty::exposures()/dagitty::outcomes().- m, x, z
Optional character vectors overriding inferred mediator (M), baseline (X), and intermediate (Z) terms.
- include_descendants
Logical; if TRUE, allow descendants of mediators to be included in M (conservatively restricted to descendants that remain ancestors of Y and descendants of A).
- fe
Optional character vector of FE/grouping variables to include in X (merged with any extracted from
formula).- fe_as_factor
Logical; if TRUE, wraps bare FE names as
factor(FE)(only when FE exists indata).- drop_fe_collinear
Logical; if TRUE and
datais provided, drop any single-variable term that is constant within an FE group (DirectEffects is less forgiving than fixest here).- strict_rhs
Logical; if TRUE and
formulais provided, X is drawn only from RHS terms informula. If FALSE, X is drawn from the DAG node set (pre-treatment ancestors) even if not informula.- return_parts
Logical; if TRUE, returns a list with components and metadata; otherwise returns just the formula.