# [R] fix sign of a coefficient in formula

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jan 20 11:15:20 CET 2011

```Fitting a linear model with constraints is a completely different task
from fitting one without -- and it is the fit you want to constrain,
not the formula.

See CRAN package nnls to fit a linear model with sign constraints. If
you have range constraints you can use nls(algorithm="port") since
'non-linear' here subsumes 'linear'.

In your simple case you can also use some convex optimization theory
to know that if the unconstrained solution has either A < 0 or B < 0
then a solution constrained by A >= 0 and B >= 0 has either A=0 or
B=0 or both, so try those and see which fits best.

On Thu, 20 Jan 2011, Peter Schmidtke wrote:

> Dear R users & experts,
>
> I'd like to create a model using lm (or glm) under some constraints of
> how coefficients for each component could look like (sort of a range of
> coefficients that should be allowed).
> So let's go for an example :
>
> model=lm(age ~ eyecolor + height, data=inputdata)
>
> So let's suppose that R pops out a model with positive estimates for the
> coefficients eyecolor and height, I have to use them as is. But however,
> if I'd like to produce a model that follows that behaviour :
>
> age = A*eyecolor - B*height
> having A>0 and B>0

Note that in general that will not have an optimal solution.  You need
A >= 0 and B >= 0.

Also, that differs from your R formula, which has an intercept and the
opposite sign for 'height'.

> how can I achieve that? I read about offsets, but I just know the range
> of the coefficient, but not the exact value and doing something like
> that offset(seq(-1,0,0.01)*height) doesn't seem to work in lm.

It should work and do what it is documented to do, just not what you
want.

>
> Thanks in advance for any hint!
>
> Best regards.
>
> --
>
> Peter Schmidtke
> PhD Student
> Dept. Physical Chemistry
> Faculty of Pharmacy
> University of Barcelona

--
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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

```