Skip to contents

Summarize each variable's scores across folds and rank the variables, as draw_varimp() selects and orders them.

Usage

summarize_varimp(
  x,
  measure = NULL,
  top_n = NULL,
  rank_by = "magnitude",
  summary = "mean",
  folds = NULL,
  decreasing = TRUE
)

Arguments

x

Numeric vector, single-column matrix, or data frame: Importance scores. Tables require variable and named numeric measures, optionally fold. Variable names must be unique within each fold.

measure

Optional Character: Measure column. NULL selects the first column other than variable or fold; vectors use "importance".

top_n

Optional Integer [1, Inf): Maximum number of variables. NULL includes all variables with a summary; fractions are not accepted.

rank_by

Character {"magnitude", "signed"}: Rank by absolute or signed summary, respectively.

summary

Character {"mean", "median"}: Summary across folds.

folds

Optional Character: Full set of fold IDs, including folds with no rows. NULL uses the IDs observed in the fold column.

decreasing

Logical: Select and display ranks from largest to smallest. FALSE selects and displays the smallest ranks first.

Value

Data frame with one row per selected variable, in rank order: variable, importance (the summary), n_available (folds with a score), and n_folds (all folds).

Details

Each variable's summary is the mean or median of its available scores. Explicit NA scores, omitted rows, and wholly unavailable folds are excluded from the summary, and variables with no available score are omitted. Records without a fold column have one score per variable, which is its summary. Magnitude ranking uses the absolute summary, not the mean absolute fold score; signed ranking uses the summary itself. Ties retain first appearance in the input.

A producer whose importance omits variables with a zero score supplies those zeros explicitly; an omitted row is an unavailable score.

Examples

scores <- data.frame(
  variable = c("age", "weight", "age", "weight"),
  fold = c("A", "A", "B", "B"),
  coefficient = c(0.8, -1.2, 0.6, -0.9)
)
summarize_varimp(scores)
#>   variable importance n_available n_folds
#> 1   weight      -1.05           2       2
#> 2      age       0.70           2       2
summarize_varimp(scores, rank_by = "signed")
#>   variable importance n_available n_folds
#> 1      age       0.70           2       2
#> 2   weight      -1.05           2       2