*-Constrainted regression
*-
https://bbs.pinggu.org/thread-1155288-1-1.html
/* Question:
我想做一个回归:Y = b0 + b1 * X1 + b2*X2 + b3*X3+e
但有两个限制条件:
1、b1+b2+b3 = 1
2、b1,b2,b3非负
请问是用stata如何实现?谢谢。
Treatment:
For contraint 2, we can estimate ln(b) instead of b,
once we get the esimator of ln(b), b can be recovered as b=exp(ln(b)),
which ensure b is positive.
With constraint 1, only two parameters remian, say, b3=1-b1-b2
NLS can be used to get the estimations of b1, b2, and b3
*/
clear
set obs 1000
gen x1 = 2*invnorm(uniform())
gen x2 = 3*invnorm(uniform())
gen x3 = 4*invnorm(uniform())
gen e = 1*invnorm(uniform())
gen y = 10 + 0.2*x1 + 0.3*x2 + 0.5*x3 + e
*-note: b0=10, b1=0.2, b2=0.3, b3=0.5
*----
*-OLS
reg y x*
*----
*-NLS
*-Only Constraints 2: b1+b2+b3=1
nl (y = {b0} + {b1}*x1 + {b2}*x2 + (1-{b1}-{b2})*x3)
*-Both Constraints
nl (y = {b0} + exp({b1})*x1 + exp({b2})*x2 + (1-exp({b1})-exp({b2}))*x3)
local ln_b1 = _b[/b1]
local ln_b2 = _b[/b2]
dis in g "b1= " in y exp(`ln_b1')
dis in g "b2= " in y exp(`ln_b2')
dis in g "b3= " in y 1-exp(`ln_b1')-exp(`ln_b2')