use "D:\stata8\ado\Examples\XTFiles\invest2.dta", clear
* Note: This is an exercise to get ui by several differnt means
tsset com t
* ----method 1-----* using xtreg, fe command
qui xtreg mark inv stock , fe
predict ui , u
gen ui_fe = ui + _b[_cons] /*Because there is constant term in the fe model*/
// list ui_fe if time==1 /*The ui given by xtreg,fe command*/
* ----method 3-----* using formula
sort com t
foreach var of varlist inv mark stock{
by company: egen `var'_bar = mean(`var')
}
gen ui_formula = market_bar - _b[inv]*invest_bar - _b[stock]*stock_bar
* ----method 2-----* using dummy vars , see Greene(2000) chp14
qui tab com , gen(dum) /*Five dummy variables correponding five firms are gened*/
qui reg market invest stock dum* , nocons /*You must specify "noconstant" option here*/
mat ui_dum = (_b[dum1] \ _b[dum2] \ _b[dum3] \ _b[dum4] \ _b[dum5])
* display the results
dis _n in y "=======ui: xtreg,fe =========== "
list ui_fe if time==1
dis _n in y "=======ui: formula=========== "
list ui_formula if time==1
dis _n in y "=======ui: dummy variables =========== "
mat list ui_dum