# [R] regression constraints?

Prof Brian D Ripley ripley at stats.ox.ac.uk
Fri Jan 5 08:52:13 CET 2001

```On Fri, 5 Jan 2001, Strumila, John wrote:

> gday R gurus,
>
> I have a multivariate regression for which I want to constrain the
> coefficients to be > 0.  Is this possible?

Is that multiple linear regression?  (one y, lots of x's?)  Multivariate
regression might be thought to have many y's and only model part of the
relationship amongst them by many x's.

Fitting by least-squares, it is a quadratic programming problem.
So either use the quadprog package on CRAN, or use a general
minimizer with constraints: set forward optim().

Here's an example from the VR script ch08.R:

library(MASS)
data(whiteside)
attach(whiteside)
Gas <- Gas[Insul=="Before"]
Temp <- -Temp[Insul=="Before"]
#nnls.fit(cbind(1, -1, Temp), Gas) : this is an S-PLUS function
# can use box-constrained optimizer
fn <- function(par) sum((Gas - par[1] - par[2]*Temp)^2)
optim(rep(0,2), fn, lower=0, method="L-BFGS-B")\$par
rm(Gas, Temp)
detach()

You can do non-linear regression and non-LS fitting the same way.

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```