Below is my code. The coefficient I get from xtreg is 3.39. The coefficient I get from demeaned variables is 3.38. I believe the coefficient should be exactly the same!
Please correct code. Much appreciated!!
************************************************** *******************
sysuse auto, clear
drop if missing(rep78)
*Fixed effects by demeaning data
*Demean by foreign
foreach x in price weight {
egen mean_`x' = mean(`x'), by(foreign)
gen c_f_`x' = mean_`x' - `x'
}
*Demean by rep78
foreach x in price weight {
egen mean_c_f_`x' = mean(c_f_`x'), by(rep78)
gen c_f_r_`x' = mean_c_f_`x' - c_f_`x' // r stands for rep78. So centered by foreign, rep78
}
*Compare coefficients I obtained with demeaned data and xtreg.
reg c_f_r_price c_f_r_weight, robust
xtset foreign
xtreg price weight i.rep78, fe
Please correct code. Much appreciated!!
************************************************** *******************
sysuse auto, clear
drop if missing(rep78)
*Fixed effects by demeaning data
*Demean by foreign
foreach x in price weight {
egen mean_`x' = mean(`x'), by(foreign)
gen c_f_`x' = mean_`x' - `x'
}
*Demean by rep78
foreach x in price weight {
egen mean_c_f_`x' = mean(c_f_`x'), by(rep78)
gen c_f_r_`x' = mean_c_f_`x' - c_f_`x' // r stands for rep78. So centered by foreign, rep78
}
*Compare coefficients I obtained with demeaned data and xtreg.
reg c_f_r_price c_f_r_weight, robust
xtset foreign
xtreg price weight i.rep78, fe
Comment