Select models. Compare them properly.
Run the complete workflow in your browser

Wait for Ready, then run each code block in order. The first run can take a moment while R and the package 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.
The data
data_SynthDiabetes is included with gtregression. It contains 768 records and the following candidate predictors of diabetes:
| Variable | Meaning |
|---|---|
glucose |
Plasma glucose concentration |
mass |
Body mass index |
age |
Age in years |
pregnant |
Number of pregnancies |
pedigree |
Diabetes pedigree function |
pressure |
Diastolic blood pressure |
One short line retains complete observations so every compared model uses the same denominator.
1. Select candidate models
select_models() needs six straightforward instructions:
data— the dataset to analyse;outcome— the variable being modelled;exposures— the candidate predictors;approach— the regression method;direction— forward, backward or both-direction selection; andformat— aflextableorgtresult.
Forward selection starts with the intercept-only model. At each step it considers adding a candidate predictor, accepts the best improvement, and continues until no further candidate improves the selection criterion.
The table shows every accepted step and its formula, number of predictors, AIC, BIC, log-likelihood and deviance. The lowest-AIC model is retained as selected$best_model; every accepted model remains available in selected$all_models.
Changing one word changes the direction:
direction = "backward"
direction = "both"Model selection is a screening tool. The lowest AIC does not automatically make a model scientifically correct.
2. Compare as many models as required
compare_models() is not limited to two models. Supply several fitted models in the order you want to compare them.
Here we begin with glucose and progressively add body mass, age, pregnancies and pedigree. primary_exposure = "glucose" tracks the glucose estimate across all four models—including the percentage change in its log-effect estimate.
The single table shows:
- the formula and denominator for every model;
- AIC, BIC and log-likelihood;
- sequential likelihood-ratio comparisons for nested models;
- the glucose estimate in every model; and
- how much that primary estimate changes as variables are added.
compare_models() compares the models already fitted—it does not silently refit them. It also checks whether they appear nested and use the same analysis sample. The same function accepts models from multi_reg(), cox_reg() and surv_reg().
3. Compare models with and without an interaction
Does the association between glucose and diabetes differ with age?
interaction_models() requires the main exposure, the proposed effect_modifier, and the covariates shared by both models. It then:
- fits the model without the interaction;
- fits the model containing
glucose × ageon the same analysis data; and - compares them using a likelihood-ratio or Wald test.
One focused call answers the model-comparison question. Change to test = "Wald" when a Wald comparison is preferred.
4. Display every interaction estimate
After comparing the models, multi_reg() presents the main effects and product term in one aligned table.
No formula has to be assembled manually, and no separate table-building workflow is required.
Keep the scientific decision visible
Model-fit statistics help compare models. They do not decide whether a variable is a confounder, whether an interaction was prespecified, or whether a model answers the research question. gtregression makes the comparison transparent; the investigator makes the decision.
Cite gtregression
citation("gtregression")