fnnls_regs: Fast Non-Negative Least Squares for Multiple Outputs

View source: R/fnnls_regs.R

fnnls_regsR Documentation

Fast Non-Negative Least Squares for Multiple Outputs

Description

Solves the NNLS problem min ||Y - XB||_F^2 subject to B >= 0 using the Fast Non-Negative Least Squares algorithm of Bro & de Jong (1997).

Usage

fnnls_regs(
  Y,
  X,
  tol = 1e-06,
  max_iter = 1000,
  sum_to_constant = FALSE,
  constant = 1,
  lower_bound = FALSE,
  lb = 0,
  parallel = FALSE,
  ncores = -1
)

Arguments

Y

A numeric matrix of dimensions n x m.

X

A numeric matrix of dimensions n x p.

tol

The convergence tolerance, default is 1e-6.

max_iter

The maximum number of iterations, default is 1000.

sum_to_constant

If TRUE all entries in each column of B sum to 'constant'. Default is FALSE.

constant

If sum_to_constant is TRUE, all entries in each column sum to this number. The default value is 1.

lower_bound

If TRUE all entries bounded below by 'lb', otherwise they are nonnegative. The default value is FALSE.

lb

If lower_bound is TRUE all entries are bounded below by 'lb'. The default value is 0.

parallel

If TRUE, the columns of B are computed in parallel. The default value is FALSE.

ncores

If parallel is TRUE, this many cores are used in the parallel computations. Must be positive integer. The default value is -1 (use all available cores).

Value

A list with two elements:

  • B: A non-negative numeric matrix of dimensions p x m with the estimated coefficients.

  • mse: A numeric vector of length m with the mean squared error for each output column.

References

Bro, Rasmus & Jong, Sijmen. (1997). A Fast Non-negativity-constrained Least Squares Algorithm. Journal of Chemometrics. 11. 393-401. 10.1002/(SICI)1099-128X(199709/10)11:53.0.CO;2-L.

Examples

n <- 50
p <- 10
m <- 3
X <- matrix(rnorm(n * p), nrow = n, ncol = p)
Y <- matrix(runif(n * m, min = 0, max = 10), nrow = n, ncol = m)
result <- fnnls_regs(Y, X, tol = 1e-8, max_iter = 1000)
result$B
result$mse


nnsolve documentation built on April 12, 2026, 5:06 p.m.