 Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Constrained Regression in Stata

 From Steven Samuels To statalist@hsphsun2.harvard.edu Subject Re: st: Constrained Regression in Stata Date Fri, 3 Jun 2011 15:08:56 -0400

```In Austin's example, the third coefficient is constrained to be: (1-exp(_b[/b1])-exp(_b[/b2].
I don't see that this guarantees that the coefficient will be positive.  The following code does, though in this example, the results are identical:

***************************************
nl (gpm=   (exp({b1})/(1+exp({b1})+exp({b2})))*wt  ///
+ (exp({b2})/(1+exp({b1})+exp({b2})))*l   ///
+ (1/(1+exp({b1})+exp({b2})))*tn  ///
+{_cons})

nlcom (exp(_b[/b1])/(1+exp(_b[/b1])+exp(_b[/b2])))  ///
(exp(_b[/b2])/(1+exp(_b[/b1])+exp(_b[/b2])))  ///
(1/(1+exp(_b[/b1])+exp(_b[/b2]))), post
*****************************************************

Steve

On Jun 3, 2011, at 10:05 AM, Austin Nichols wrote:

If you want to impose all coefs are nonnegative and sum to one, try:

sysuse auto, clear
g gpm=1/mpg
g wt=weight/1000
g l=length/1000
g tn=turn/1000
reg gpm wt l tn, nohe
* constrained to be positive:
nl (gpm=exp({b1})*wt+exp({b2})*l+exp({b3})*tn+{_cons}), nolog
nlcom (exp(_b[/b1])) (exp(_b[/b2])) (exp(_b[/b3])), post
* constrained to be positive and sum to one:
nl (gpm=exp({b1})*wt+exp({b2})*l+(1-exp({b1})-exp({b2}))*tn+{_cons})
nlcom (exp(_b[/b1])) (exp(_b[/b2])) (1-exp(_b[/b1])-exp(_b[/b2])), post

But imposing such assumptions tends to result in very implausible
models, unless the unconstrained coefs are very close to satisfying

On Fri, Jun 3, 2011 at 2:03 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> This is an FAQ. See
>
> How do I fit a regression with interval constraints in Stata?
> Isabel Cañette, StataCorp
> October 2006; updated July 2008; minor revisions April 2011
> http://www.stata.com/support/faqs/stat/intconst.html
>
> Nick
>
> On Fri, Jun 3, 2011 at 12:24 AM, Meg22 <meadhbh2@gmail.com> wrote:
>
>> I need to do a constrained regression in stata-I need all the beta
>> coefficents to be positive and sum to 1.
>>
>> I have 11 independent variables so my regression is:
>> reg Rrf bills it lt cor lcvalue lcgrowth medcap smallcap noncanbonds us jap
>> if fundno ==`i'
>>
>> I need all the betas to be positive, sum to 1 and there to be a constant.
>>
>> I got the summing to 1 bit I think:
>> constraint define 1 bills + it + lt + cor + lcvalue + lcgrowth + medcap +
>> smallcap + noncanbonds + us + jap = 1
>> cnsreg  Rrf bills it lt cor lcvalue lcgrowth medcap smallcap noncanbonds us
>> jap, constraint(1)
>>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```