Learn R Programming

multiway (version 1.0-2)

fnnls: Fast Non-Negative Least Squares

Description

Finds the vector b minimizing c{ sum( (y - X%*%b)^2 ) } subject to b[j] >= 0 for all j.

Usage

fnnls(XtX,Xty,ntol=NULL)

Arguments

XtX
Crossproduct matrix crossprod(X) of dimension p-by-p.
Xty
Crossproduct vector crossprod(X,y) of length p-by-1.
ntol
Tolerance for non-negativity.

Value

  • The vector b such that b[j] >= 0 for all j.

References

Bro, R., & De Jong, S. (1997). A fast non-negativity-constrained least squares algorithm. Journal of Chemometrics, 11, 393-401.

Examples

Run this code
##########   EXAMPLE 1   ##########
X <- matrix(1:100,50,2)
y <- matrix(101:150,50,1)
beta <- solve(crossprod(X))%*%crossprod(X,y)
beta
beta <- fnnls(crossprod(X),crossprod(X,y))
beta


##########   EXAMPLE 2   ##########
X <- cbind(-(1:50),51:100)
y <- matrix(101:150,50,1)
beta <- solve(crossprod(X))%*%crossprod(X,y)
beta
beta <- fnnls(crossprod(X),crossprod(X,y))
beta


##########   EXAMPLE 3   ##########
X <- matrix(rnorm(400),100,4)
btrue <- c(1,2,0,7)
y <- X%*%btrue + rnorm(100)
fnnls(crossprod(X),crossprod(X,y))


##########   EXAMPLE 4   ##########
X <- matrix(rnorm(2000),100,20)
btrue <- runif(20)
y <- X%*%btrue + rnorm(100)
beta <- fnnls(crossprod(X),crossprod(X,y))
crossprod(btrue-beta)/20

Run the code above in your browser using DataLab