corrPrune() performs model-free variable subset selection by iteratively
removing predictors until all pairwise associations fall below a specified
threshold. It returns a single pruned data frame with predictors that satisfy
the association constraint.
Usage
corrPrune(
data,
threshold = 0.7,
measure = "auto",
mode = "auto",
force_in = NULL,
by = NULL,
group_q = 1,
max_exact_p = 100,
...
)Arguments
- data
A data.frame containing candidate predictors.
- threshold
Numeric scalar. Maximum allowed pairwise association (default: 0.7). Must be in
[0, 1]– every supported association measure is a correlation magnitude in[0, 1](seemeasure), so this range is enforced the same way regardless ofmode(threshold = 0is valid only inmode = "greedy"; see Mode Selection below).- measure
Character string specifying the numeric-numeric association measure to use. One of
"auto"(default, Pearson),"pearson","spearman","kendall","bicor","distance", or"maximal". This only customizes numeric-numeric pairs; every other pair-type combination is fixed and not affected bymeasure: eta (the correlation ratio, \(\sqrt{\eta^{2}}\)) for numeric-categorical pairs, Cramer's V for categorical-categorical pairs, and Spearman for numeric-ordered and ordered-ordered pairs. The measure actually used for each pair-type combination is reported in theassoc_methods_usedattribute of the result.All of these are correlation magnitudes on a common scale, so
thresholdmeans the same thing for every pair type: eta is the multiple correlation between the numeric variable and the factor, equal to the absolute point-biserial correlation for a two-level factor, and Cramer's V equals the absolute phi coefficient for a 2x2 table. Encoding a binary variable as 0/1 numeric or as a two-level factor therefore gives the same association, and the same pruning result.- mode
Character string specifying the search algorithm. Options:
"auto"(default): uses exact search if number of predictors <=max_exact_pand there are at least 2 predictors withthreshold > 0, otherwise uses greedy search (exact search requires both, since it routes throughMatSelect())"exact": exhaustive search for maximal subsets (may be slow for large p); requires at least 2 predictors andthreshold > 0"greedy": fast approximate search using iterative removal; supports a single predictor andthreshold = 0
- force_in
Character vector of variable names that must be retained in the final subset. Default: NULL.
- by
Character vector naming one or more grouping variables. If provided, associations are computed separately within each group, then aggregated using the quantile specified by
group_q. Default: NULL (no grouping).- group_q
Numeric scalar in (0, 1]. Quantile used to aggregate associations across groups when
byis provided. Default: 1 (maximum, ensuring threshold holds in all groups). Use 0.9 for 90th percentile, etc.- max_exact_p
Integer. Maximum number of predictors for which exact mode is used when
mode = "auto". Default: 100.- ...
Additional arguments (reserved for future use).
Value
A data.frame containing the pruned subset of predictors, with the
selected columns unchanged from data (same types and values –
character/logical/integer columns are converted internally only for
association computation, never in the returned data). The result
has the following attributes:
- selected_vars
Character vector of retained variable names
- removed_vars
Character vector of removed variable names
- mode
Character string indicating which mode was used ("exact" or "greedy")
- measure
Character string indicating which measure was used for numeric-numeric pairs
- assoc_methods_used
Named list mapping each pair-type combination (e.g. "numeric_numeric", "numeric_factor") to the association method actually used
- threshold
The threshold value used
- n_rows_used
Number of complete-case rows used to compute associations (see Details); the returned data itself is not row-filtered
Details
corrPrune() identifies a subset of predictors whose pairwise associations
are all below threshold. The function works in several stages:
Variable type detection: Identifies numeric vs. categorical predictors
Constant-column removal: Predictors that are constant across every complete-case row are excluded with a warning, since their association with anything is undefined and they would otherwise ride into the result without contributing any information
Association measurement: Computes appropriate pairwise associations
Grouping (optional): If
byis specified, computes associations within each group and aggregates using the specified quantileFeasibility check: Verifies that
force_invariables satisfy the threshold constraint (aforce_invariable excluded for being constant also errors here)Subset selection: Uses either exact or greedy search to find a valid subset
Grouped Pruning: When by is provided, the function ensures the selected
predictors satisfy the threshold constraint across groups. For example, with
group_q = 1 (default), the returned predictors will have pairwise associations
below threshold in all groups. With group_q = 0.9, they will satisfy
the constraint in at least 90% of groups.
Mode Selection: Exact mode guarantees finding all maximal subsets and returns the largest one. Greedy mode is faster but approximate, using an iterative removal strategy based on association scores.
Tie-Breaking: When multiple subsets or variables are equally good, deterministic tie-breaking is applied:
Exact mode: Selects by (1) largest subset size, (2) lowest average correlation, (3) lexicographically first variable names, compared in C order (
method = "radix") so the result does not depend on the session's collation locale. Column order does not affect the result.Greedy mode: Removes the variable with (1) most constraint violations, (2) highest max association, (3) highest average association, (4) lowest column index. Column order can influence the result when earlier criteria are tied.
To see all maximal subsets instead of a single selection, use
corrSelect().
See also
corrSelect for exhaustive subset enumeration,
assocSelect for mixed-type data subset enumeration,
modelPrune for model-based predictor pruning.
Examples
# Basic numeric data pruning
data(mtcars)
pruned <- corrPrune(mtcars, threshold = 0.7)
names(pruned)
# Force certain variables to be included
pruned <- corrPrune(mtcars, threshold = 0.7, force_in = "mpg")
# Use greedy mode for faster computation
pruned <- corrPrune(mtcars, threshold = 0.7, mode = "greedy")