scib.metrics.pcr.linreg_multiple_np

scib.metrics.pcr.linreg_multiple_np(X_pca, covariate, n_jobs=None)

Compute per-PC \(R^2\) with a dense numpy regression backend.

Execution has two paths:
  1. If exactly one non-numeric covariate is provided, use a fast one-way ANOVA formulation based on between-group variance. This is automatically used for one non-numeric covariate.

  2. Otherwise, run dense least-squares regression using an intercept and pseudo-inverse.

One-way ANOVA path:

For a single categorical covariate, compute per-PC R2 = SS_between / SS_total where SS_between = sum_g n_g * (mu_g - mu)^2 and SS_total = sum_i (y_i - mu)^2. This is algebraically equivalent to linear regression with categorical indicators and an intercept, but avoids explicit design-matrix solves.

Thread control:

If n_jobs is provided and threadpoolctl is installed, BLAS/LAPACK thread pools are limited for the full function scope (including shortcut checks and dense regression). This keeps runtime behavior reproducible across environments and avoids BLAS oversubscription.

Used by linreg_method='numpy' in pc_regression().

Parameters:

n_jobs – Preferred number of BLAS/LAPACK threads for numpy linalg calls. If None, keep the runtime default. If threadpoolctl is unavailable, this hint is ignored.