Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Re: creating variables from regression coefficients


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Re: creating variables from regression coefficients
Date   Sun, 27 Nov 2005 17:26:18 -0000

As reproduced here the condition 

if `id' -- `i' 

looks unlikely to be what you want. 

Your code can be trimmed down a bit. 

use "C:\Program Files\Stata9\data\GEINDEX\EQ4_1985_2004.dta", clear
tempvar id
bysort SIC = gen `id' = sum(_n == 1) 
gen pred = . 
qui forval i = 1/`=`id'[_N]' {
	tempvar pred 
	regress TA REV PPE if `id' == `i'
	predict `pred' if `id' == `i', xb
	replace pred = max(pred, `pred') 
	drop `pred' 
}
save "C:\Program Files\Stata9\EQ4\EQ4_1985_2004.dta", replace

Also, do check out -statsby-. 

Nick 
n.j.cox@durham.ac.uk 

mindy.nitkin@simmons.edu
 
> I am using the program below to complete industry level 
> regressions and then
> to apply the coefficiencts from the industry regressions to 
> firms within each
> industry in order to predict the level of TA. It works very well.
> 
> I am wondering if there is a way to imput the regression 
> coefficents into my
> data set as variables for each firm's observation in addition 
> to or instead of the predictions.
> 
> Alternately, can Stata use the coefficients from the first regression:
> TA on REV PPE in a second regressioin: TA on (REV-AR) PPE. 
> The model I am trying
> to replicate changes between in first stage and second stage 
> regressions.
 
> >clear
> >use "C:\Program Files\Stata9\data\GEINDEX\EQ4_1985_2004.dta"
> > tempvar id
> > sort SIC
> > by SIC: gen `id'=1 if _n==1
> > qui replace `id'=sum(`id')
> > local i=0
> > tempvar pred0
> > qui gen `pred0'=.
> > local n=`id'[_N]
> > while `i'<`n' {
> > local i=`i'+1
> > local j=`i'-1
> > tempvar pred pred`i'
> > qui regress TA REV PPE if `id'--`i'
> > qui predict `pred' if `id'==`i', xb
> > qui egen `pred`i''=rmax(`pred' `pred`j'')
> > drop `pred' `pred`j''
> > }
> > rename `pred`i'' pred
> > save "C:\Program Files\Stata9\EQ4\EQ4_1985_2004.dta", replace

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index