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: Regression by year and industry to save coefficients and standard errors


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Regression by year and industry to save coefficients and standard errors
Date   Mon, 23 Dec 2013 18:03:33 +0000

<>

Your bug may be using the wrong quotation mark in a macro reference.

Focusing on your main problem:

_ b[fyear] and _se[fyear] are just constants. You put them in
temporary variables and then promptly take them out again. There is no
harm in that, but also precisely zero point to it when you can just
put the constants where you want them.

So your code can be shrunk to

use sic4calc
generate salescoeff= .  //empty variable for coefficient
generate salesstderr = . // empty variable for std error
levelsof sic4, local(levels)
foreach x of local levels {
forval z =  2001/2011 {
capture reg logsic4sales fyear if sic4==`x' & fyear==`z'
replace salescoeff = _b[fyear]  if e(sample)
replace salesstderr = _se[fyear] if e(sample)
}
}

Those are small fixes compared with a simpler big fix, which is to use
-statsby- directly.
Nick
[email protected]


On 23 December 2013 16:27, Alessandri, Todd <[email protected]> wrote:
> Hello,
> I am trying to run regressions by industry and year, and save the
> coefficients and standard errors for each industry-year model as new
> variables. There is an additional wrinkle‹I have panel data for 1998-2011
> and want to run each regression using t to t-4 only (I.e., for 2001, use
> only 1998-2001 years of data).
>
> I am new to using foreach loops and I found some prior coding on the web.
> I have tried to adapt it to my situation but two issues:
>
> 1. the code below generates an error message: "__000001 already defined"
> 2. I have not figured out how to use the rolling four year data periods.
>
> Here is the code I tried. Any suggestions would be much appreciated.
>
>
> use sic4calc
> generate salescoeff= .  //empty variable for coefficient
> generate salesstderr = . // empty variable for std error
> tempvar salescoeff salesstderr // temporary variables for each regression
> levelsof sic4, local(levels)
> foreach x of local levels {
> foreach z of numlist 2001/2011 {
> capture reg logsic4sales fyear if sic4==`x' & fyear==`z'
> generate `salescoeff' = _b[fyear] // coeff now in temp var
> replace salescoeff = `salescoeff' if e(sample) // transfer coeff from temp
> generate `salesstderr' = _se[fyear] // stderr now in temp var
> replace salesstderr = `salesstderr' if e(sample) // transfer stderr from
> temp
> drop `salescoeff' `salestderr'
> }
> }
> save sic4variables
>
> Thanks,
> Todd
>
>
>
>
> *
> *   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/

*
*   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