[R] Linear Models with positive coefficients?

ROS Mathieu mim78 at cict.fr
Thu Jul 8 09:47:25 CEST 1999

> is it possible in one of the libraries on linear methods to constrain
> the coefficients to
> be positive?

I'm not sure this is available in R, but I send the same question  one
month ago to Snews and the point is to use nnls.fit.
I put the answers I get in attachment.
regards,
Mathieu
-------------------------------------------------------
Mathieu Ros
mathieuros at bigfoot.com
Universite Paul Sabatier, Toulouse.
-------------------------------------------------------

-------------- next part --------------
Prof Brian D Ripley <ripley at stats.ox.ac.uk>
nnls() is for this purpose.

Christian Keller <ckeller at aicos.com>
You can use 'nnls.fit' (Linear least-squares fit with coefficients
constrained to be nonnegative). See help(nnls.fit) for details.

Note that 'nnls.fit' can't handle model formulas,
e.g. you have to write
nnls.fit(cbind(x1,x2), y)

where x1 and x2 are the explanatory variables and y is the
response.

Note also that (by default) 'nnls.fit' does not estimate an
intercept term. To include an intercept, use
nnls.fit(cbind(1,x1,x2), y)

Unfortunately the output is rather poor
(no special print and summary methods).

I hope this helps.

marcel.baumgartner at rdls.nestle.com
Try nlminb(). This has so-called box-constraints. You need just to write
differently. The help file and Venables and Ripley have examples. By the
way, using the Solver in
Excel, you can do also very easily ...

Marcel

Berwin A Turlach
nnls.fit()?

Caveat 1:
nnls.fit stems from the time before models were introduced.  It expects
you to give down the design matrix x and the y-vector.  No
specification via a formula.  But you can always do, say,
> X <- model.matrix(Hard~Dens+Dens^2+Dens^3,data=janka)
> Y <- janka[,"Hard"]
> nnls.fit(X,Y)
\$coefficients:
(Intercept)     Dens I(Dens^2) I(Dens^3)
0 4.243751 0.5619766         0

Caveat 2:
nnls.fit by default restricts *all* coefficients to be non-negative,

also the intercept.  If you don't want to impose the non-negativity
constraint on one of the parameters, then the standard trick is to
replicate the corresponding column and to multiplying that new column
by -1.  For example, if in the above example you wouldn't want to have
the intercept constrained to be non-negative, then you could do:
> X <- cbind(-X[,1],X)
> nnls.fit(X,Y)
\$coefficients:
(Intercept)     Dens I(Dens^2)   I(Dens^3)
431.8919           0 31.99779         0 0.003626301

So the estimated coefficient for the intercept is -431.8919.