Skip to content
Prev 247832 / 398503 Next

fix sign of a coefficient in formula

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:

            
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'.
It should work and do what it is documented to do, just not what you 
want.