# `opls` — Orthogonal PLS (OPLS)
_Group_: **Core PLS** · _Registry tolerance_: `1e-08`
## Description
Orthogonal PLS (Trygg & Wold 2002)
From the `pls4all.sklearn.OPLSRegression` docstring:
> Orthogonal PLS regression (Trygg & Wold 2002).
> **Registry note** — Bioconductor `ropls::opls` is the external OPLS reference; convergence and orthogonal-component conventions may differ.
### Parameters
| Name | Type | Default | Notes |
|------|------|---------|-------|
| `n_components` | `int` | `2` | Number of latent components extracted (k). |
| `solver` | `str` | `'nipals'` | Inner algorithm: 'nipals', 'simpls', 'svd', 'kernel', 'orthogonal-scores', 'power', 'randomized-svd', 'wide-kernel'. |
| `center_x` | `bool` | `True` | Subtract the column mean of X before fitting. |
| `scale_x` | `bool` | `True` | Standardize X columns to unit variance before fitting. |
| `center_y` | `bool` | `True` | Subtract the column mean of y before fitting. |
| `scale_y` | `bool` | `False` | Standardize y columns to unit variance before fitting. |
| `tol` | `float` | `1e-06` | Convergence tolerance for iterative solvers (NIPALS / power-iteration). |
| `max_iter` | `int` | `500` | Maximum iterations for iterative solvers. |
| `store_scores` | `bool` | `False` | If True, keep the latent score matrix (`x_scores_`) after fit. |
## Explanations
### Bibliographic source
Trygg, J. & Wold, S. (2002). *Orthogonal projections to latent structures (O-PLS)*. Journal of Chemometrics 16(3), 119–128.
### Mathematical principle
OPLS rotates the standard PLS latent space so that a single direction captures all $\mathbf{Y}$-correlated variation while the remaining components capture $\mathbf{Y}$-orthogonal structural variation in $\mathbf{X}$. The resulting decomposition $\mathbf{X} = \mathbf{t}_p\mathbf{p}_p^{\top} + \mathbf{T}_o\mathbf{P}_o^{\top} + \mathbf{E}$ separates the **predictive component** $\mathbf{t}_p$ from the orthogonal block $\mathbf{T}_o$, which absorbs spectroscopic baselines, scatter and other nuisance factors that confound interpretation of the predictive loading.
Numerically OPLS proceeds by NIPALS-deflating $\mathbf{X}$ against directions orthogonal to $\mathbf{X}^{\top}\mathbf{y}$ before each new predictive component is extracted. Predictions are identical to those of a one-component PLS on the orthogonal-corrected $\mathbf{X}$; the value is in **the interpretation of the loadings**, not in better predictions per se.
OPLS shines in metabolomics and process spectroscopy where the spectra carry strong systematic but non-predictive variation; in those settings the single-vector predictive loading is far easier to relate to biology / chemistry than a multi-component PLS loading matrix.
### Implementation
`Algorithm.OPLS` + `Solver.NIPALS` + `Deflation.ORTHOGONAL`. Reference: Bioconductor `ropls::opls`. Note: orthogonal-component ordering and the criterion that stops orthogonal extraction differ between implementations — exact bit parity is not expected, but RMSE-rel parity within ~1e-3 is.
R roxygen note (`sklearn.R::opls`):
> Formula-based OPLS regression wrapper around the n4m C ABI.
MATLAB header (`bindings/matlab/+pls4all/OplsRegression.m`):
```text
pls4all.OplsRegression — Orthogonal Partial Least Squares Regression model.
Example:
mdl = pls4all.OplsRegression(X, y, 5);
yhat = predict(mdl, Xnew);
```
### Usage
Every pls4all binding tab dispatches into the same C kernel; the external libraries listed at the bottom of the page are the parity references registered in `benchmarks.parity_timing.registry`. Switch tabs to read the same fit in your language. The R package now ships drop-in-compatible facades for the CRAN `pls` package (`plsr`, `pcr`, `mvr`) and for the `mdatools::pls(x, y, ...)` matrix idiom — those tabs appear only on the methods that have a meaningful equivalence.
**pls4all bindings**
::::{tab-set}
:class: pls4all-bindings
:::{tab-item} C ABI · libn4m
:sync: c
:class-label: lang-c
```c
/* C ABI — libn4m (Model.fit path) */
n4m_context_t* ctx = n4m_context_create();
n4m_config_t* cfg = n4m_config_create();
n4m_config_set_algorithm(cfg, N4M_ALGORITHM_PLS_REGRESSION);
n4m_config_set_solver (cfg, N4M_SOLVER_SIMPLS);
n4m_config_set_n_components(cfg, 4);
n4m_model_t* mdl = NULL;
n4m_model_fit(ctx, cfg, &x_view, &y_view, &mdl);
n4m_model_predict(ctx, mdl, &x_test_view, &y_hat_view);
n4m_model_destroy(mdl);
n4m_config_destroy(cfg);
n4m_context_destroy(ctx);
```
:::
:::{tab-item} Python · pls4all (raw)
:sync: python-raw
:class-label: lang-python
```python
import pls4all
from pls4all import Algorithm, Solver
with pls4all.Context() as ctx, pls4all.Config() as cfg:
cfg.algorithm = Algorithm.PLS_REGRESSION
cfg.solver = Solver.SIMPLS
cfg.n_components = 4
with pls4all.Model.fit(ctx, cfg, X, y) as mdl:
y_hat = mdl.predict(X_test)
```
:::
:::{tab-item} Python · pls4all.sklearn
:sync: python-sklearn
:class-label: lang-python
```python
from pls4all.sklearn import OPLSRegression
mdl = OPLSRegression(n_components=2, solver='nipals', center_x=True, scale_x=True, center_y=True, scale_y=False, tol=1e-06, max_iter=500, store_scores=False)
mdl.fit(X, y)
y_hat = mdl.predict(X_test)
```
:::
:::{tab-item} R · pls4all_method()
:sync: r-dispatcher
:class-label: lang-r
```r
library(pls4all)
# Unified low-level dispatcher (May 2026 R cleanup):
res <- pls4all_method("opls", X, y,
n_components = 4L)
# res is a named list with MethodResult arrays/scalars.
# selected_indices / top_k_intervals are 1-based.
```
:::
:::{tab-item} R · pls4all (formula+S3)
:sync: r-formula
:class-label: lang-r
```r
library(pls4all)
fit <- opls(y ~ ., data = train, ncomp = 4L)
yhat <- predict(fit, newdata = test)
summary(fit)
```
:::
:::{tab-item} MATLAB · pls4all (MEX)
:sync: matlab-mex
:class-label: lang-matlab
```matlab
res = pls4all.opls(X, y, 4);
% see header of bindings/matlab/+pls4all/opls.m for full
% parameter surface:
% [coefs, x_mean, y_mean, predictions] = opls(X, Y, n_components)
yhat = predict(res, Xtest);
```
:::
:::{tab-item} MATLAB · pls4all (classdef)
:sync: matlab-classdef
:class-label: lang-matlab
```matlab
mdl = pls4all.fit("opls", X, y, "NumComponents", 4);
yhat = predict(mdl, Xtest);
```
:::
::::
**Registry parity references** 📐
:::{card}
:class-card: external-refs
- 📐 **`ref.r_ropls`** (R · r) — `ropls` Bioc · strict (rmse_rel ≤ 1e-08) — Bioconductor `ropls::opls` — OPLS reference. Permutations and plotting are disabled in benchmark timing; ropls still requires crossvalI >= 1 for a finite Q2 path.
:::
### Benchmarks
Adaptive wall-clock per cell measured against [`full_matrix.csv`](../benchmarks/overview.md). Only backends that implement this method are listed; libraries without the method are omitted.
**Verdict** · ✓ ref / ≈ ref / ~ shape mark a reference-gate pass at strict / relaxed / qualitative tolerance · ✓ bind = pls4all binding agrees with the C++ baseline · ⇄ cross-check = documented by-design selector/RNG/model, noncanonical API/facade convention, or secondary oracle · ✗ divergent · ⚠ error · — not run. The fastest backend per column is marked 🏆.
**Reference gate**: strict — numeric equivalence (`rmse_rel_tol ≤ 1e-08`).
Rows tagged with **📐** are the canonical parity references for this method (declared in [`parity_timing.registry`](../benchmarks/methodology.md)). C++ and external rows show reference parity; pls4all language bindings show binding parity against the C++ backend. Hover the icon for role and tolerance band.
::::{tab-set}
:class: parity-tabs
:::{tab-item} 1 thread
:sync: threads-1
| Backend | Parity | 200×50 (ms) |
| C++ native · libn4m |
pls4all.cpp.blas+omp | ✓ ref 5e-15 | 1.87 ms🏆 |
| Python · pls4all |
pls4all.python | ✓ bind | 1.88 ms |
pls4all.sklearn | ✓ bind | 2.12 ms |
| R · pls4all |
pls4all.R | ✓ 2e-14 | 4.35 ms |
pls4all.R.formula | ✓ 2e-14 | 5.13 ms |
pls4all.R.mdatools | ✓ 2e-14 | 5.23 ms |
pls4all.R.pls | ✓ 2e-14 | 5.40 ms |
| R · external |
📐ref.r_ropls | source | 15.4 ms |
:::
:::{tab-item} 3 threads
:sync: threads-3
| Backend | Parity | 200×50 (ms) |
| C++ native · libn4m |
pls4all.cpp.blas+omp | ✓ ref 5e-15 | 1.96 ms |
| Python · pls4all |
pls4all.python | ✓ bind | 1.79 ms🏆 |
pls4all.sklearn | ✓ bind | 2.14 ms |
| R · pls4all |
pls4all.R | ✓ 2e-14 | 6.22 ms |
pls4all.R.formula | ✓ 2e-14 | 5.57 ms |
pls4all.R.mdatools | ✓ 2e-14 | 7.12 ms |
pls4all.R.pls | ✓ 2e-14 | 7.88 ms |
| R · external |
📐ref.r_ropls | source | 16.0 ms |
:::
:::{tab-item} 10 threads
:sync: threads-10
| Backend | Parity | 200×50 (ms) |
| C++ native · libn4m |
pls4all.cpp.blas+omp | ✓ ref 5e-15 | 1.73 ms🏆 |
| Python · pls4all |
pls4all.python | ✓ bind | 1.81 ms |
pls4all.sklearn | ✓ bind | 1.94 ms |
| R · pls4all |
pls4all.R | ✓ 2e-14 | 4.17 ms |
pls4all.R.formula | ✓ 2e-14 | 5.09 ms |
pls4all.R.mdatools | ✓ 2e-14 | 5.39 ms |
pls4all.R.pls | ✓ 2e-14 | 5.17 ms |
| R · external |
📐ref.r_ropls | source | 16.0 ms |
:::
::::
---
_See also_: [benchmark overview](../benchmarks/overview.md) · [methods index](index.md) · [interactive dashboard](../landing/dashboard.md)