PLS-DA
Partial Least Squares Discriminant Analysis with double cross-validation.
Scale and nuisance residualization
plsda_doubleCV takes four keyword-only arguments whose defaults reproduce its
earlier behaviour exactly:
scale(defaultTrue) is passed to everyPLSRegressionfit in the inner loop, the outer loop and the per-repeat refits.Falsefits in the metric the predictors are already in, for example pooled-standardized features that must keep their pooled scale.nuisance(defaultNone) takes aNuisanceDesign: row-aligned conditioning columnsD(no intercept) and covariate columnsC. Every training set (each outer rest set and each inner training set) regresses its predictors on[1, D, C], subtracts the fitted covariate partC γ, and residualizes its held-out rows asX_held_out − C_held_out γ. The held-out rows' conditioning (their labels) is never read. The per-repeat refits use coefficients estimated on all rows. Every training design must have full column rank, or the function raises aValueErrornaming the repeat and fold, for example when a factor level is absent from a training set. The result gains"nuisance_min_training_df", the smallest training-set residual degrees of freedom.strata(defaultNone) names row-aligned labels both CV levels stratify on instead ofy; the AUROC still scoresy. Folds depend only on the partition the labels define, so labels that partition the rows likeyreproduce the default folds. With a nuisance design, passnuisance.cells()(its conditioning cells) so every training set contains every cell: the inner folds are not shuffled, and on rows sorted by group within stage a stage-only stratification can put a whole group × stage cell into one validation fold.min_components(default1) is the smallest number of latent variables the inner CV considers: the candidates aremin_components … max_components − 1, so every outer-fold selection, every modalLVin the table and every refit model has at least that many components. It must satisfy1 ≤ min_components < max_components, or the function raises aValueErrornaming both values before any fold is fitted. The default is the unfloored rule; the trajectory latent space (fit_pls_latent_space, shared by the simulation harness andmotco analyze) applies a floor of 2 by default, see Real-data analysis.
The real-data analysis uses scale, nuisance and strata together for
covariate-aware latent construction; see
Real-data analysis.
Functions
motco.stats.pls.plsda_doubleCV(X, y, cv1_splits=7, cv2_splits=8, n_repeats=30, max_components=50, random_state=1203, n_jobs=1, progress=True, *, scale=True, nuisance=None, strata=None, min_components=1)
Run canonical double-nested cross-validation for PLS-DA.
For each outer fold, the inner CV averages AUROC across its V folds and selects the n_LV with the highest mean. The outer fold's selected n_LV is then evaluated on its held-out test set. Per repeat, the K outer-fold test AUROCs are aggregated by mean (and sample std), and the per-fold n_LV choices are aggregated by mode (parsimony tie-break). One final model is refit per repeat on the full input using the modal n_LV.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
The predictor variables. |
required |
y
|
Union[DataFrame, Series]
|
The outcome variable. |
required |
cv1_splits
|
int
|
Number of folds in the CV1 (inner) loop. Default: 7. |
7
|
cv2_splits
|
int
|
Number of folds in the CV2 (outer) loop. Default: 8. |
8
|
n_repeats
|
int
|
Number of repeats of the K-fold outer CV. Default: 30. |
30
|
max_components
|
int
|
Maximum number of LV to test (candidates are
|
50
|
random_state
|
int
|
For reproducibility. Default: 1203. |
1203
|
n_jobs
|
int
|
Number of parallel workers for the inner CV loop. Use -1 for all available CPUs. Default: 1 (serial). |
1
|
progress
|
bool
|
Whether to display a tqdm progress bar over outer folds. Default: True. |
True
|
scale
|
bool
|
Keyword-only. Whether every PLS fit (inner, outer and the per-repeat
refits) rescales the predictors to unit variance. |
True
|
nuisance
|
NuisanceDesign | None
|
Keyword-only. When given (row-aligned to |
None
|
strata
|
Union[ndarray, Series, Sequence[str], None]
|
Keyword-only. Row-aligned labels the outer and inner folds are
stratified on instead of |
None
|
min_components
|
int
|
Keyword-only. The smallest number of latent variables the inner CV
considers, so the candidates are |
1
|
Returns:
| Type | Description |
|---|---|
dict
|
With two keys:
With |
Source code in src/motco/stats/pls.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | |
motco.stats.pls.calculate_vips(model, components=None)
Estimates Variable Importance in Projection (VIP) in Partial Least Squares (PLS)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
model generated from the PLSRegression function |
required | |
components
|
Union[None, list[int]]
|
if not None, a list of integers indicating the components to compute the VIPs from. If None, all components are taken into account. Default None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
vips |
array
|
variable importance in projection for each variable |
Source code in src/motco/stats/pls.py
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 | |
Nuisance residualization
motco.stats.pls.NuisanceDesign
dataclass
Row-aligned nuisance design for covariate residualization.
conditioning (n × k) holds the design the covariate coefficients
are estimated conditional on (for example drop-first stage indicators),
without an intercept; covariates (n × q) holds the covariate
columns whose fitted part is removed. The residualization regresses each
predictor on [1, conditioning, covariates] and subtracts only the
covariate part, so signal collinear with the covariates but explained by
the conditioning design is kept (Frisch–Waugh–Lovell). kind optionally
names the conditioning design for callers that record it.
Source code in src/motco/stats/pls.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
column_names
property
Design column names beyond the intercept: conditioning, then covariates.
matrix()
The full residualization design [1, conditioning, covariates].
Source code in src/motco/stats/pls.py
88 89 90 91 | |
cells()
One label per row naming its conditioning cell (its distinct conditioning row).
Under stage indicators these are the stages; under group × stage indicators, the group × stage cells. Used to stratify cross-validation folds so that every training set contains every conditioning cell.
Source code in src/motco/stats/pls.py
93 94 95 96 97 98 99 100 101 | |
rows(index)
The design restricted to index (positional rows).
Source code in src/motco/stats/pls.py
103 104 105 106 107 108 109 | |
motco.stats.pls.fit_nuisance_coefficients(X, design)
Estimate the covariate coefficients of X conditional on the design.
Regresses every column of X on [1, conditioning, covariates] by
least squares and returns the covariate block of the coefficients
(q × p) and the residual degrees of freedom n − 1 − k − q.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/motco/stats/pls.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
motco.stats.pls.residualize(X, covariates, coefficients)
residualize(X: pd.DataFrame, covariates: np.ndarray, coefficients: np.ndarray) -> pd.DataFrame
residualize(X: np.ndarray, covariates: np.ndarray, coefficients: np.ndarray) -> np.ndarray
Subtract the fitted covariate part: X − covariates @ coefficients.
Only the covariate columns are read, so held-out rows can be residualized with training coefficients without their labels. A DataFrame keeps its index and columns.
Source code in src/motco/stats/pls.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
motco.stats.pls.nuisance_partial_r2(X, design)
Per-feature partial R² of the covariates given the intercept and conditioning.
1 − RSS([1, D, C]) / RSS([1, D]) for every column of X: the share
of the variance left after the conditioning design that the covariates
explain, i.e. what the residualization removes. A column with no variance
left after the conditioning design gets 0.
Source code in src/motco/stats/pls.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |