Skip to contents

Why use nested assessment?

The edge-selection level controls which connections enter a CPM. Choosing it using the same assessment predictions that are reported as final performance can make the performance estimate optimistic. With nested assessment, each outer training set has its own inner folds for choosing a level. The selected level is then fitted on that outer training set and evaluated on the held-out outer fold. Outer predictions estimate the performance of the entire level-selection procedure, not that of a single fixed level.

Set up an example

The following synthetic data have one row per subject and one column per connection. The nuisance variable is passed to cpm() rather than regressed out beforehand.

library(cpmr)

set.seed(2026)
n <- 80
conmat <- matrix(rnorm(n * 120), nrow = n)
covariates <- matrix(rnorm(n), ncol = 1)
behav <- rowMeans(conmat[, 1:10, drop = FALSE]) +
  0.5 * covariates[, 1] + rnorm(n, sd = 0.5)

model_spec <- spec(
  selection = cpm_selection_cor(criterion = "p_value"),
  construction = cpm_construction_strength(standardize_edges = TRUE)
)

Compare levels inside each outer fold

Supply at least two unique, finite candidate levels between 0 and 1 via selection_levels. They use the criterion in spec: here they are p-value cutoffs. For "absolute", they are absolute-correlation cutoffs; for "proportion", they are per-sign proportions. The fixed level in the selection specification is replaced by each candidate during tuning.

set.seed(123)
tuned <- cpm(
  conmat = conmat,
  behav = behav,
  spec = model_spec,
  covariates = covariates,
  resamples = 5,
  selection_levels = c(0.001, 0.01, 0.05),
  selection_metric = "rmse",
  inner_resamples = 4,
  return_edges = "none"
)

tuned
#> CPM:
#>   Call: cpm(conmat = conmat, behav = behav, spec = model_spec, covariates = covariates, 
#>     resamples = 5, return_edges = "none", selection_levels = c(0.001, 
#>         0.01, 0.05), selection_metric = "rmse", inner_resamples = 4)
#>   Number of folds: 5
#>   Number of observations: 80
#>   Threshold tuning: RMSE (4 inner folds)
#>     Candidate levels: 0.001, 0.01, 0.05
#>     Selected levels by outer fold: 0.001, 0.01, 0.05, 0.001, 0.001
#>     Complete cases: 80
#>   Parameters:
#>     Covariates:            included
#>     Missing data:          fail
#>     Edge storage:          not stored
#>     Selection:             pearson / p_value / nested level selection
#>     Construction:          separate / no weighting / z-score edges
#>     Model:                 linear regression
#>   Use summary() for aggregate metrics and tidy() for metric tables.

For each outer fold, cpm() compares the candidates using mean inner-fold RMSE and chooses the smallest mean. The mean is over valid fold scores, with each contributing fold weighted equally. Set selection_metric = "mae" to use mean MAE instead; RMSE is the default. Ties are resolved by the order of selection_levels. The default separate-sign construction uses the joint prediction stream for selection; sign_mode = "net" uses the net stream.

resamples controls the outer folds and can also accept a list of assessment row indices. inner_resamples is the number of randomly generated inner folds within each outer training set; its default is 5. Set the random seed before cpm() to reproduce both generated outer folds and inner folds. With manual outer folds, the seed still controls the inner folds.

Inspect choices and outer performance

The tuning table has one row per candidate and outer fold. For example, inspect the candidate scores in the first fold and then the choice in every fold:

tuning <- tidy(tuned, component = "tuning")
tuning[tuning$outer_fold == 1, ]
#> # A tibble: 3 × 7
#>   outer_fold level metric prediction estimate n_inner_folds selected
#>        <int> <dbl> <chr>  <chr>         <dbl>         <dbl> <lgl>   
#> 1          1 0.001 rmse   joint         0.569             4 TRUE    
#> 2          1 0.01  rmse   joint         0.641             4 FALSE   
#> 3          1 0.05  rmse   joint         0.692             4 FALSE
tuning[tuning$selected, ]
#> # A tibble: 5 × 7
#>   outer_fold level metric prediction estimate n_inner_folds selected
#>        <int> <dbl> <chr>  <chr>         <dbl>         <dbl> <lgl>   
#> 1          1 0.001 rmse   joint         0.569             4 TRUE    
#> 2          2 0.01  rmse   joint         0.585             4 TRUE    
#> 3          3 0.05  rmse   joint         0.688             4 TRUE    
#> 4          4 0.001 rmse   joint         0.586             4 TRUE    
#> 5          5 0.001 rmse   joint         0.611             4 TRUE

estimate is the mean inner-fold error, n_inner_folds counts the valid scores contributing to that mean, and selected marks the chosen candidate. Selections can differ across outer folds. These inner scores are used for selection, not as the final performance estimate.

summary(tuned)
#> CPM summary:
#>   Number of folds: 5
#>   Pooled correlations (Pearson):
#>     Joint: 0.149
#>     Positive: 0.143
#>     Negative: 0.043
#>   Fold-wise correlations (Pearson):
#>     Joint: 0.322 (SE 0.116) (2/5 folds)
#>     Positive: 0.303 (SE 0.096) (2/5 folds)
#>     Negative: 0.169 (1/5 folds)
#>   Pooled RMSE:
#>     Joint: 0.604
#>     Positive: 0.604
#>     Negative: 0.611
#>   Pooled MAE:
#>     Joint: 0.492
#>     Positive: 0.484
#>     Negative: 0.508
tidy(tuned, component = "metrics", level = "pooled")
#> # A tibble: 9 × 6
#>   metric      prediction estimate     n method  descriptive_only
#>   <chr>       <chr>         <dbl> <int> <chr>   <lgl>           
#> 1 rmse        joint        0.604     80 NA      FALSE           
#> 2 mae         joint        0.492     80 NA      FALSE           
#> 3 rmse        positive     0.604     80 NA      FALSE           
#> 4 mae         positive     0.484     80 NA      FALSE           
#> 5 rmse        negative     0.611     80 NA      FALSE           
#> 6 mae         negative     0.508     80 NA      FALSE           
#> 7 correlation joint        0.149     80 pearson FALSE           
#> 8 correlation positive     0.143     80 pearson FALSE           
#> 9 correlation negative     0.0428    80 pearson FALSE
head(tuned$predictions)
#>   row fold    observed        joint      positive     negative
#> 1   1    4  1.33516104 1.257675e-17  1.257675e-17 1.257675e-17
#> 2   2    5 -0.24595331 3.469447e-18  3.469447e-18 3.469447e-18
#> 3   3    5  0.28527638 3.469447e-18  3.469447e-18 3.469447e-18
#> 4   4    5  0.42572420 3.469447e-18  3.469447e-18 3.469447e-18
#> 5   5    3 -0.02175975 4.584328e-01 -5.158561e-02 7.268002e-01
#> 6   6    4 -0.63004461 1.257675e-17  1.257675e-17 1.257675e-17

The summary and pooled metrics use the outer-fold predictions. Because this example includes covariates, the observed outcomes and predictions are on the fold-specific residualized outcome scale: nuisance regressions are fitted on the relevant training split and applied to its assessment split.

Practical considerations

  • Covariate adjustment, edge standardization, edge screening, and model fitting are learned within each training split; outer assessment data do not select the level. Avoid preprocessing the full dataset before calling cpm().
  • Candidate values of 0 or 1 produce a warning because selection may become degenerate. Choose candidates in advance and report the criterion, metric, outer and inner fold counts, seed, and covariate handling.
  • Nested tuning evaluates the procedure; it does not return one final full-data model or identify a universally best level. Inspecting outer results to choose settings and reporting those same results as final performance would reuse assessment data for selection.
  • Every outer training set must support the requested inner folds and their training requirements, including residual degrees of freedom when using covariates and p-value screening.
  • Nested tuning is more expensive than a fixed-level run. Association statistics are reused across candidates within an inner split, but not across folds or calls. return_edges = "none" avoids storing edge masks.

See Leakage-Safe Covariate Handling in CPM for more on covariate handling and Native Getting Started with cpmr for the basic workflow.