Run forward, backward, or two-way stepwise selection on an existing
hazard fit using Wald p-values or AIC deltas as the entry /
retention criterion. Phase-specific entry is supported for
multiphase models: a covariate can enter one phase and not another.
Usage
hzr_stepwise(
fit,
scope = NULL,
data,
direction = c("both", "forward", "backward"),
criterion = c("wald", "aic"),
slentry = 0.3,
slstay = 0.2,
max_steps = 50L,
max_move = 4L,
force_in = character(),
force_out = character(),
trace = TRUE,
...
)
# S3 method for class 'hzr_stepwise'
print(x, ...)
# S3 method for class 'hzr_stepwise'
summary(object, ...)
# S3 method for class 'summary.hzr_stepwise'
print(x, ...)
# S3 method for class 'hzr_stepwise'
as.data.frame(x, ...)Arguments
- fit
A fitted
hazardobject built via theformula = Surv(...) ~ predictors, data = dfinterface.- scope
Candidate set.
NULL(default) uses every data-frame column not already in the model for every phase. For single-distribution fits, pass a one-sided formula (~ age + nyha) or a character vector of names. For multiphase fits, pass a named list of one-sided formulas keyed by phase.- data
Data frame the base fit was built on. Required for refits.
- direction
One of
"both"(default),"forward","backward".- criterion
One of
"wald"(default) or"aic". SAS-style p-value thresholds apply to Wald; AIC usesDeltaAIC < 0uniformly.- slentry
Entry p-value threshold for the Wald criterion. Default
0.30matches SASSLENTRY.- slstay
Retention p-value threshold for the Wald criterion. Default
0.20matches SASSLSTAY.- max_steps
Hard cap on total accepted actions. Emits a
warning()if hit. Default50.- max_move
Per-variable oscillation cap. When a variable has entered + exited more than
max_movetimes it is frozen for the remainder of the run. Default4.- force_in
Character vector of variables that must remain in the model. Such variables are still scored and reported in the selection trace, but are never dropped.
- force_out
Character vector of variables that may never be considered as candidates.
- trace
Logical; print step-by-step progress to the console. Default
TRUE.- ...
Unused.
- x
An
hzr_stepwiseobject.- object
An
hzr_stepwiseobject.
Value
An object of class c("hzr_stepwise", "hazard") – the
final fit augmented with:
stepsData frame with one row per accepted / frozen action; see Details.
scopeRecord of the candidate scope, plus
force_in,force_out, and the frozen set.criteriaNamed list of the threshold / direction settings actually applied.
trace_msgCharacter vector of the trace lines, captured regardless of the
traceflag.elapseddifftimefrom start to finish.final_callThe call that produced this result.
print.hzr_stepwise returns x invisibly.
summary.hzr_stepwise returns a summary.hzr_stepwise object
(extends summary.hazard) with $stepwise_steps and $stepwise_trace
appended.
print.summary.hzr_stepwise returns x invisibly.
as.data.frame.hzr_stepwise returns the $steps data frame.
Details
The steps data frame has columns:
step_numInteger sequence starting at 1.
action"enter","drop", or"frozen".variableVariable affected.
phasePhase name (multiphase) or
NA_character_.criterion"wald"or"aic".scoreWinning score used for the decision.
stat,dfWald statistic and degrees of freedom.
p_value,delta_aicAlways populated when computable, regardless of the active criterion.
logLik,aic,n_coefGoodness-of-fit diagnostics of the model after this step.
Examples
data(avc)
avc <- na.omit(avc)
base <- hazard(survival::Surv(int_dead, dead) ~ age,
data = avc, dist = "weibull", fit = TRUE)
# \donttest{
sw <- hzr_stepwise(base, scope = ~ age + nyha,
data = avc, direction = "forward",
control = list(n_starts = 1))
#> Stepwise selection (direction = forward, criterion = wald, slentry = 0.30, slstay = 0.20)
#>
#> Warning: Stepwise forward: candidate refit failed for nyha.
#> (no further action after 0 steps)
#>
#> Final model: 0 covariates, logLik = NA, AIC = NA
print(sw)
#> Stepwise selection (direction = forward, criterion = wald, slentry = 0.30, slstay = 0.20)
#>
#> (no further action after 0 steps)
#>
#> Final model: 0 covariates, logLik = NA, AIC = NA
# }