我在 R 中估计了一个模型,我想添加约束,强制其中一个系数小于另一个系数。我怎样才能做到这一点?
1 回答
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.