Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: How to add constraints for a model to be estimated via lm or nls?

Tags:

r

nls

I estimate a model in R and I would like to add constraint that would force one of the coefficients to be smaller than the other one. How can I do that?

like image 805
infoholic_anonymous Avatar asked Feb 20 '23 03:02

infoholic_anonymous


1 Answers

If you really need to use either lm or nls and can't use anything that lets you specify constraints, one way is to reparameterize the model so that the difference in coefficients is itself a parameter.

For example if you have a model with b1*x1+b2*x2 in it, but where b2 > b1 you could code that as b1*x3+d*x2 where x3=x1+x2 and d represents b2-b1. Now you need to force d > 0. You simply reparameterize again, d = exp(k), say, and use nls to fit the new model b1*x3+exp(k)*x2 where your parameters are b1 and k. Once estimated, you can then compute an estimate of b2 as b1+exp(k). Then b1 is guaranteed to be less than b2.

However, nls allows you to specify upper and lower bounds, so you should be able to just put 0 in as a lower bound for d (though that may not guarantee a strict inequality).

Hope that helps.

like image 70
Glen_b Avatar answered Feb 22 '23 01:02

Glen_b