Learn R Programming

BiVariAn (version 1.0.3)

step_bw_firth: Stepwise backward for logistic Firth regression with automated dummy variables conversion (with forced terms)

Description

Extension code to perform stepwise backward on a logistf model with categorical variables. Automatically transforms predictors that are factors into dummy variables.

Usage

step_bw_firth(
  reg_model,
  s_lower = "~1",
  s_upper = "all",
  trace = TRUE,
  steps = NULL,
  p_threshold = 0.05,
  data = NULL,
  forced = NULL
)

Value

An object of class "step_bw" with components:

  • final_model: the fitted logistf model

  • steps: a data.frame of each elimination step

Arguments

reg_model

A logistf model object.

s_lower

Lower step. Not used; included for compatibility. Default = "~1".

s_upper

Upper step. Not used; included for compatibility. Default = "all".

trace

Logical. Print each step. Default TRUE.

steps

Maximum number of elimination steps. If NULL, set to number of predictors.

p_threshold

P-value threshold for elimination. Default = 0.05.

data

Data frame. If NULL, extracted from reg_model.

forced

Character vector of term names to always keep. Default NULL.

References

Heinze G, Ploner M, Jiricka L, Steiner G. logistf: Firth’s Bias-Reduced Logistic Regression. 2023.

Efroymson MA. Multiple regression analysis. In: Ralston A, Wilf HS, editors. Mathematical methods for digital computers. New York: Wiley; 1960.

Ullmann T, Heinze G, Hafermann L, Schilhart-Wallisch C, Dunkler D, et al. (2024) Evaluating variable selection methods for multivariable regression models: A simulation study protocol. PLOS ONE 19(8): e0308543

Examples

Run this code
if (requireNamespace("logistf", quietly = TRUE)) {
  library(logistf)
  data <- mtcars
  data$am <- as.factor(data$am)
  regression_model <- logistf(am ~ mpg + cyl + disp, data = data)
  # Perform backward stepwise, forcing 'cyl' to remain
  stepwise <- step_bw_firth(
    regression_model,
    forced = c("cyl"),
    p_threshold = 0.05,
    trace = FALSE
  )
  final_model <- stepwise$final_model
  # Show steps and summary
  stepwise$steps
  summary(final_model)
}

Run the code above in your browser using DataLab