Confounding, interaction and effect modification.

Wait for Ready, then run each code block in order. The first run can take a moment while R and the packages start.
The package uses flextable by default for document-ready output. This browser demo uses format = "gt" so the tables display here. Use RStudio for the full flextable and export experience.
Understand the teaching data
The dataset contains 189 births. We will examine whether maternal smoking is associated with low birth weight, whether race changes the crude estimate, and whether the association differs across racial groups.
| Variable | What it records | Coding or unit |
|---|---|---|
low |
Birth-weight outcome | Normal birth weight; low birth weight |
smoke |
Maternal smoking during pregnancy | No; yes |
race |
Maternal race in the source data | White; Black; other |
age |
Maternal age | Years |
lwt |
Maternal weight at last menstrual period | Pounds |
1. Start with the crude association
One function produces a publication-ready crude estimate with the exposure levels, reference category, effect estimate and 95% confidence interval neatly aligned.
2. Adjust for a defined variable
adjust_for = makes the adjustment set explicit. It is equally easy to supply several variables when that set is supported by the study design and subject knowledge.
3. Compare the estimates
Crude and adjusted regression tables merge in a single command. The output is fully customisable as either a flextable or gt table.
4. Organise the confounding assessment
The function brings the crude estimate, adjusted estimate, percentage change and effect-modification screen together. It is a screening and reporting aid—not an automatic substitute for causal reasoning, subject knowledge or a DAG.
Compare models with and without an interaction term
interaction_models() fits the two planned models on the same analysis data: one without and one with smoke × race. It then compares them using a likelihood-ratio or Wald test. Interaction always belongs to a specified model and scale; absence of a small p-value is not proof that every subgroup effect is identical.
Display the interaction model
The interaction terms appear in the same aligned, publication-ready table as the main effects—without manually constructing a regression formula.
Present estimates within each stratum
Effect modification is not a nuisance to remove. When an association differs meaningfully across groups, the stratum-specific estimates are often the result that readers need to see.
Show the stratum-specific estimates visually
The result is a ggplot object, so titles, labels, colours, scales and figure dimensions remain completely customisable.
A clearer effect-modification example
The birth-weight analysis demonstrates the workflow, but an effect modifier should not be declared simply because stratum-specific estimates look different. For a clearer example, use data_SynthDiabetes, another teaching dataset included with gtregression.
Here the exposure is glucose, expressed per 10-unit increase; the outcome is diabetes; and age group is the potential effect modifier. Body mass and number of pregnancies are retained as adjustment variables.
First test whether a model containing glucose_10 × age_group fits better than the same adjusted model without that product term.
Then report the adjusted glucose estimate separately for each age group rather than hiding the difference inside one overall estimate.
This is the practical point: test the planned interaction, then present the stratum-specific effects that make the effect modification understandable.
Keep the concepts separate
- Confounding can distort the exposure–outcome association and is addressed through design and justified adjustment.
- Interaction is assessed by adding a product term on a specified model scale.
- Effect modification is a difference in the association across groups and is usually something to describe and report—not adjust away.
gtregression organises the estimates. The investigator still decides which variables matter, why they belong in the model and how the result should be communicated.
Cite gtregression
citation("gtregression")