R: How to add constraints to the model for evaluation through lm or nls?

I am evaluating a model in R, and I would like to add a constraint that would make one of the coefficients be smaller than the other. How can i do this?

+4
source share
1 answer

If you really need to use lm or nls and cannot use anything that allows you to specify restrictions, one way is to re-parameterize the model, so the difference in the coefficients is itself a parameter.

For example, if you have a model with b1 * x1 + b2 * x2, but where b2> b1, you can encode this as b1 * x3 + d * x2, where x3 = x1 + x2 and d represents b2-b1, now you you need to force d> 0. You just reanalyze again, d = exp (k), say, and use nls to match the new model b1 * x3 + exp (k) * x2, where your parameters are b1 and k. After the estimate, you can calculate the 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 just set 0 as the lower bound for d (although this may not guarantee strict inequality).

Hope this helps.

+8
source

Source: https://habr.com/ru/post/1434515/


All Articles