The cpmr package implements connectome predictive modeling (CPM) in R. Its primary workflow is native and matrix-first: you pass a subject-by-edge matrix together with a behavioral vector, and cpmr handles CPM-specific training, fold planning, and leakage-safe preprocessing inside package code. This package relies on Rfast for row-oriented calculation.
Installation
You can install the released version of cpmr from CRAN with:
install.packages("cpmr")Or you can install the development version of cpmr from r-universe with:
install.packages("cpmr", repos = c("https://psychelzh.r-universe.dev", getOption("repos")))Native workflow
Shape your connectivity matrix as a subjects-by-edges matrix, where each row contains the edge vector for one subject, and pair it with a behavioral vector. The native workflow uses spec() together with cpm().
library(cpmr)
withr::local_seed(123)
conmat <- matrix(rnorm(100 * 1000), nrow = 100)
behav <- rnorm(100)
s <- spec()
result <- cpm(conmat = conmat, behav = behav, spec = s, resamples = 5)
summary(result)
#> CPM summary:
#> Number of folds: 5
#> Pooled correlations (Pearson):
#> Joint: -0.108
#> Positive: -0.074
#> Negative: -0.080
#> Fold-wise correlations (Pearson):
#> Joint: -0.062 (SE 0.064) (5/5 folds)
#> Positive: 0.007 (SE 0.086) (5/5 folds)
#> Negative: -0.039 (SE 0.078) (5/5 folds)
#> Pooled RMSE:
#> Joint: 1.242
#> Positive: 1.204
#> Negative: 1.199
#> Pooled MAE:
#> Joint: 0.951
#> Positive: 0.956
#> Negative: 0.910
#> Selected edges:
#> Positive: 0.56%
#> Negative: 0.32%
head(result$predictions)
#> row fold observed joint positive negative
#> 1 1 1 0.26499342 0.591709195 -0.08846823 0.76686948
#> 2 2 5 1.83074748 0.325440165 0.29380525 0.05747766
#> 3 3 5 -0.05937826 0.845311244 1.05634560 -0.34018567
#> 4 4 3 -0.05320937 0.664779940 0.56680746 0.25720238
#> 5 5 1 0.43790418 0.001660943 0.29376910 -0.36672391
#> 6 6 4 1.33744904 0.591903346 0.45146065 0.34438321spec() can also express richer CPM variants without leaving the native API surface:
rich_spec <- spec(
selection = cpm_selection_cor(
method = "spearman",
criterion = "absolute",
level = 0.1
),
construction = cpm_construction_strength(
sign_mode = "net",
weight_scale = 0.03
),
model = cpm_model_lm()
)
cpm(conmat = conmat, behav = behav, spec = rich_spec, resamples = 5)$predictions |>
head()
#> row fold observed net
#> 1 1 1 0.26499342 0.12167754
#> 2 2 1 1.83074748 0.47750573
#> 3 3 5 -0.05937826 0.43681670
#> 4 4 5 -0.05320937 -0.01008645
#> 5 5 2 0.43790418 -0.06417020
#> 6 6 3 1.33744904 0.09903232This keeps the main CPM stages visible while grouping naturally paired options into small helpers:
-
selection = cpm_selection_cor(...)defines how edges are screened; -
construction = cpm_construction_strength(...)defines how screened edges become CPM-derived predictors; -
model = cpm_model_lm()defines the outcome model fitted on those predictors.
By default, spec() keeps edge standardization turned off so the native workflow stays close to the classic CPM path of screening edges, constructing summary features, and fitting the outcome model. If you want fold-local edge z-scoring, opt in with cpm_construction_strength(standardize_edges = TRUE).
If you are new to CPM, the key idea behind the current summary-based construction is:
-
positiveedges are edges whose screening association with the outcome is positive and passes the threshold; -
negativeedges are edges whose screening association with the outcome is negative and passes the threshold; - CPM then collapses those screened edge sets into subject-level summary features before fitting the outcome model.
With sign_mode = "separate", cpmr keeps the positive and negative summaries as separate predictors and reports three prediction streams:
-
joint, which fits one model with both summaries together; -
positive, a positive-only diagnostic stream; -
negative, a negative-only diagnostic stream.
With sign_mode = "net", cpmr constructs a single net_summary = positive_summary - negative_summary feature and returns one net prediction stream.
cpm() is fold-based: it runs CPM under the requested assessment plan and returns out-of-fold predictions together with the fold structure.
summary(result)
#> CPM summary:
#> Number of folds: 5
#> Pooled correlations (Pearson):
#> Joint: -0.108
#> Positive: -0.074
#> Negative: -0.080
#> Fold-wise correlations (Pearson):
#> Joint: -0.062 (SE 0.064) (5/5 folds)
#> Positive: 0.007 (SE 0.086) (5/5 folds)
#> Negative: -0.039 (SE 0.078) (5/5 folds)
#> Pooled RMSE:
#> Joint: 1.242
#> Positive: 1.204
#> Negative: 1.199
#> Pooled MAE:
#> Joint: 0.951
#> Positive: 0.956
#> Negative: 0.910
#> Selected edges:
#> Positive: 0.56%
#> Negative: 0.32%
head(result$predictions)
#> row fold observed joint positive negative
#> 1 1 1 0.26499342 0.591709195 -0.08846823 0.76686948
#> 2 2 5 1.83074748 0.325440165 0.29380525 0.05747766
#> 3 3 5 -0.05937826 0.845311244 1.05634560 -0.34018567
#> 4 4 3 -0.05320937 0.664779940 0.56680746 0.25720238
#> 5 5 1 0.43790418 0.001660943 0.29376910 -0.36672391
#> 6 6 4 1.33744904 0.591903346 0.45146065 0.34438321
dim(result$edges)
#> [1] 1000 2
invisible(head(tidy(result, component = "metrics")))
pooled_metrics <- tidy(result, component = "metrics", level = "pooled")
invisible(head(pooled_metrics[pooled_metrics$metric == "correlation", ]))summary(result) reports pooled out-of-fold RMSE and MAE, pooled correlations, and mean fold-wise correlations. The pooled metrics are available as tables through tidy(result, component = "metrics", level = "pooled"); fold-wise correlations are returned with level = "foldwise".
cpm() defaults to 10-fold assessment. It requires at least 10 complete cases; around 30 cases are a rough minimum for having at least 3 observations in each assessment fold, not a guarantee of stable correlations. Folds smaller than 3 produce a warning because their fold-wise correlations are unavailable. In that setting, RMSE and MAE are prioritized and pooled out-of-fold correlations are marked descriptive_only in the pooled metric table. When covariates are used, RMSE and MAE are calculated on the fold-specific residualized outcome scale rather than the original behavior scale. Use resamples = NULL to explicitly request leave-one-out assessment, resamples = 5 for 5-fold assessment, or a list of indices for a manual assessment-fold partition.
To compare selection levels, provide selection_levels. cpm() then uses inner folds to choose a level independently within each outer training set; the outer assessment folds remain held out for the reported performance. The level minimizing the mean inner-fold RMSE is selected by default, with MAE available as an alternative. In sign_mode = "separate", selection uses the joint prediction stream; sign_mode = "net" uses net. The inner candidate scores and selected level for each outer fold are available with tidy(result, component = "tuning").
tuned_result <- cpm(
conmat = conmat,
behav = behav,
spec = s,
resamples = 5,
selection_levels = c(0.001, 0.01, 0.05),
selection_metric = "rmse",
inner_resamples = 4
)
summary(tuned_result)
#> CPM summary:
#> Number of folds: 5
#> Pooled correlations (Pearson):
#> Joint: -0.082
#> Positive: -0.010
#> Negative: -0.174
#> Fold-wise correlations (Pearson):
#> Joint: -0.092 (SE 0.015) (2/5 folds)
#> Positive: 0.006 (SE 0.082) (2/5 folds)
#> Negative: -0.295 (1/5 folds)
#> Pooled RMSE:
#> Joint: 1.132
#> Positive: 1.122
#> Negative: 1.126
#> Pooled MAE:
#> Joint: 0.857
#> Positive: 0.844
#> Negative: 0.843
#> Selected edges:
#> Positive: 0.64%
#> Negative: 0.34%
invisible(tidy(tuned_result, component = "tuning"))Choosing a path
cpmr treats this native workflow as the primary package path:
- use
cpm(..., spec = spec())for native CPM analyses; - keep the
spec()object around when you want an explicit, reusable parameter object.
Why this matters: CPM often needs leakage-safe fold-local preprocessing and can benefit from future fold-level caching or threshold-aware optimization. Those workloads fit native cpmr runners better than a generic orchestration layer.
Code of Conduct
Please note that the cpmr project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.