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 industry and year excluding firm i


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Regression by industry and year excluding firm i
Date   Fri, 13 Dec 2013 18:03:47 +0000

Remarks

1. If you are cycling over observations, you don't need a variable
containing observation numbers, nor to use -levelsof-.

2. -in- is always faster than the corresponding -if-.

3. wlag_ce=!=. is presumably a typo, but to Stata it will be illegal syntax.

4. -capture replace b0= _b[_cons]- will end with the last intercept
calculated. I guess you don't want that.

5. Checking for missing values is redundant as -regress- will never
include them.

With these and some other small tricks, here is an attempt at
rewriting your code.

local X wlag_ce wato wlag_acc wacc wdsale wndsale
tokenize "`X'"

forval j = 0/6 {
gen b`j'=.
}

forval i = 1/`=_N' {
local same sic_2[`i'] == sic_2 & datadate[`i'] == datadate
qui count if `same' & _n != `i'

if r(N) > 10 {
reg wce `X' if `same' & _n != `i'
}

quietly if _rc == 0 {
replace b0 = _b[_cons] in `i'
forval j = 1/6 {
replace b`j' = _b[``j''] in `i'
}
}
}

gen pred_ce= b0 + b1*wlag_ce + b2*wato + b3*wlag_acc + ///
b4*wacc + b5*wdsale + b6*wndsale

Nick
njcoxstata@gmail.com


On 13 December 2013 17:33, Abdalla, Ahmed <ahmed.abdalla@kcl.ac.uk> wrote:
> Dear Statalist
> I run a regression to estimate core earnings for each variable in my dataset. The regression is run using all observations in a particular industry year EXCLUDING firm i. Expected core earnings for firm i is estimated using the coefficients multiplied by the actual values of variables in the model for firm i.
> I run the following code.
>
> First: I get an error message for macro length being exceeded.
> Second: I try to use other commands for looping, the loop runs but it gives me error message for invalid syntax.
> My problem is on how to exclude firm i ? I hope if you have any suggestions regarding running regressions by industry and year and excluding firm i from the estimation procedures.
>
>
> gen obs= [_n]
> gen runn=1
>
> gen b0=.
> gen b1= .
> gen b2=.
> gen b3=.
> gen b4=.
> gen b5=.
> gen b6=.
>
> levelsof obs,local(levels)
> foreach x of local levels{
> gen mark=1 if obs==runn
> gen sic_lp= sic_2 if obs ==runn
> qui summ sic_lp
> replace sic_lp = r(mean) if sic_lp==.
> gen datadate_lp= datadate if obs == runn
> qui summ datadate_lp
> replace datadate_lp = r(mean) if datadate_lp==.
> format datadate_lp %d
> gen sample =1 if sic_lp== sic_2 & datadate_lp== datadate & sale !=. & wce !=. & wlag_ce=!=. & wato !=. & wacc !=. & wlag_acc!=. & wdsale !=. & wndsale !=.
> egen sample_sum= sum(sample) if mark != 1
> capture reg wce wlag_ce wato wlag_acc wacc wdsale wndsale if sample==1 & mark != 1 & sample_sum >10
> capture replace b0= _b[_cons]
> capture replace b1= _b[wlag_ce] if obs==runn
> capture replace b2= _b[wato] if obs==runn
> capture replace b3= _b[wlag_acc] if obs==runn
> capture replace b4= _b[wacc] if obs==runn
> capture replace b5= _b[wdsale] if obs==runn
> capture replace b6= _b[wndsale] if obs==runn
> drop mark sic_lp datadate_lp sample sample_sum
> replace runn= runn+1
> }
>
> gen pred_ce= b0+ b1*wlag_ce + b2*wato +b3*wlag_acc + b4*wacc + b5*wdsale + b6*wndsale
>
>
> I appreciate your help
>
>
>
>
>
>
> *
> *   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