Bookmark and Share

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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: generate regression coefficients as varaibles


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: generate regression coefficients as varaibles
Date   Wed, 9 Jan 2013 21:37:36 +0000

The syntax diagram for -forval- doesn't include your syntax; what is
not allowed is forbidden.

You will have to nest loops doing it this way.

forval y=2001/2012 {
      forval  i= 1/37 {
            reg Wscaled Wone Wscaled_ch WPPE if  sic== `i' & datayearfiscal==`y'
            replace coefficientCon = _b[_cons]  if datayearfiscal==`y'
& sic==`i'
      }
}

See also

FAQ     . . . . . . . . . . Making foreach go through all values of a variable
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        8/05    Is there a way to tell Stata to try all values of a
                particular variable in a foreach statement without
                specifying them?
http://www.stata.com/support/faqs/data-management/try-all-values-with-foreach/

esp. Method 1.

Nick

On Wed, Jan 9, 2013 at 7:38 PM, Nahla Betelmal <nahlaib@gmail.com> wrote:

>> Thank you for your kind reply, and the simplification.
>>
>> I meant by not working is that i only managed to get the coefficients
>> for every year (y) but in each industry (SIC).
>> As I have 37 industries, I had to repeat the loop 37 times and hence
>> got 37 new variables for the constant coefficient instead of have one
>> constant variable that gathers the constant coefficients  for all
>> industries in every year.
>>
>> I was thinking about
>>
>> gen coefficientCon=.
>> forvalue y=2001/2012 & i= 1/37 {
>> reg Wscaled Wone Wscaled_ch WPPE if  sic== `i' & datayearfiscal==`y'
>> replace coefficientCon = _b[_cons]  if datayearfiscal==`y' & sic==`i'
>> }
>>
>> but I got : invalid syntax r(198);
>>
>> Is there a way to make forvalues accept years `y' and industries `i'
>> simultaneously! if not, is there another way to do it?

On 9 January 2013 18:37, Nick Cox <njcoxstata@gmail.com> wrote:

>>> You don't explain what "is not working" means. This is crucial.
>>>
>>> Your code appears more complicated than needed, but I can't see a bug.
>>>
>>> gen coefficientCon=.
>>> forvalue y= 2001/2012{
>>>                reg Wscaled Wone Wscaled_ch  WPPE if  dind1 & datayearfiscal==`y'
>>>                replace coefficientCon = _b[_cons]  if datayearfiscal==`y'
>>> }
>>>
>>> See also -statsby-.

On Wed, Jan 9, 2013 at 6:30 PM, Nahla Betelmal <nahlaib@gmail.com> wrote:

>>> > I am applying a cross sectional regression for 37 industries (i.e.
>>> > dind) over 11 fiscal years. I need to
>>> > extract the coefficients of each industry/year to substitute in a sub-sample.
>>> >
>>> > I tried this loop but it is not working:
>>> >
>>> > gen coefficientCon=.
>>> > forvalue y= 2001/2012{
>>> > reg Wscaled Wone Wscaled_ch  WPPE if  dind1 & datayearfiscal==`y'
>>> > scalar bvar=_b[_cons]
>>> > gen bvar=_b[_cons] if datayearfiscal==`y'
>>> > replace coefficientCon= bvar if datayearfiscal==`y'
>>> > drop bvar
>>> > }
>>> >
>>> > the out put I hope to get is silmilar to this in the edit file
>>> >
>>> >  year       industry        coefficientCon    coefficientb1       coefficientb2
>>> > 2001         dind1            0.2324                1,4356             8.98
>>> > 2002         dind1            0.654                   1.445              3.98
>>> > .
>>> > .
>>> > 2011
>>> > 2001        dind2              0.343                 .4344               2.3434
>>> > .
>>> > .
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index