select_models: Stepwise Model Selection with Evaluation Metrics

View source: R/select_models.R

select_modelsR Documentation

Stepwise Model Selection with Evaluation Metrics

Description

Performs stepwise model selection using forward, backward, or both directions across different regression approaches. Returns a summary table with evaluation metrics (AIC, BIC, log-likelihood, deviance) and the best model.

Usage

select_models(
  data,
  outcome,
  exposures,
  approach = "logit",
  direction = "forward"
)

Arguments

data

A data frame containing the outcome and predictor variables.

outcome

A character string indicating the outcome variable.

exposures

vector of predictor variables to consider in the model.

approach

Regression method. One of: "logit", "log-binomial", "poisson", "robpoisson", "negbin", or "linear".

direction

Stepwise selection direction. One of: "forward" (default), "backward", or "both".

Value

A list with the following components:

  • results_table: A tibble summarising each tested model's metric (AIC, BIC, deviance, log-likelihood, adjusted R² if applicable).

  • best_model: The best-fitting model object based on low AIC.

  • all_models: A named list of all fitted models.

Examples

data <- data_PimaIndiansDiabetes
stepwise <- select_models(
  data = data,
  outcome = "glucose",
  exposures = c("age", "pregnant", "mass"),
  approach = "linear",
  direction = "forward"
)
summary(stepwise)
stepwise$results_table
stepwise$best_model


gtregression documentation built on Aug. 18, 2025, 5:23 p.m.