Christoph M. Friedrich
1999-Jul-07 14:48 UTC
[R] Linear Models with positive coefficients?
Hi, is it possible in one of the libraries on linear methods to constrain the coefficients to be positive? Thanks Chris -- Christoph M. Friedrich | mailto:friedrich at computer.org Gesellschaft f?r Modulfermenterbau mbH (GfM mbH) | http://www.tussy.uni-wh.de/~chris Alfred-Herrhausen Str. 44 ; D-58455 Witten, Germany -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 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 your model slightly 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.