Hello Statalist,
Like the person in this post (https://www.stata.com/statalist/arch.../msg00673.html), I am trying to manually compute fixed effects on a panel dataset, over the panel and time variable. The post asked why computing fixed effects manually yielded different results than with fixed effects regression commands. The answers for that post lied in different samples being used in xtreg and regress (and others) as a result of missing variables. This understanding has solved part of my issue, but not completely.
The post has helped me in two instances:
1) Manually computing panel variable (country) fixed effects, and getting the same beta coefficient as xtreg, fe
2) Manually computing time variable (year) fixed effects, and getting the same beta coefficient as xtreg i.yr
However, the lingering problem is that I cannot get the same beta coefficients as when running an xtreg with BOTH i.yr and fe. An example dta is below. My code is as follows. (For variable legibility: "m" in the prefix refers to mean; "d" in the prefix refers to difference; "c" in the prefix refers to country; "y" in the prefix refers to year)
*Country fixed effects, THIS WORKS because both xtreg and regress output the same beta coefficient
tsset countrynum yr
egen nmiss = rmiss(dependent independent)
bysort countrynum: egen mc_dependent=mean(dependent) if nmiss==0
g dc_dependent=dependent-mc_dependent
bysort countrynum: egen mc_independent=mean(independent) if nmiss==0
g dc_independent=independent-mc_independent
xtreg dependent independent, fe
regress dc_dependent dc_independent
*Year fixed effects, THIS WORKS because both xtreg and regress output the same beta coefficient
gsort yr
bysort yr: egen my_dependent=mean(dependent) if nmiss==0
g dy_dependent=dependent-my_dependent
bysort yr: egen my_independent=mean(independent)if nmiss==0
g dy_independent=independent-my_independent
xtreg dependent independent i.yr
regress dy_dependent dy_independent
* Year and country fixed effects - This is the part I have questions about, xtreg and regress do not output the same beta coefficient. This post (https://stats.stackexchange.com/ques...el-regressions) suggests that the logic behind the code I am using to compute both panel and year fixed effects is correct, and there should be no problem with the missing values (I have taken care of this above) but I've been working on this for a while, tried various combinations, and there is clearly something I'm not fully grasping.
g dcy_dependent=dependent-my_dependent-mc_dependent
g dcy_independent=independent-my_independent-mc_independent
xtreg dependent independent i.yr, fe
regress dcy_dependent dcy_independent
Thank you very much.
Like the person in this post (https://www.stata.com/statalist/arch.../msg00673.html), I am trying to manually compute fixed effects on a panel dataset, over the panel and time variable. The post asked why computing fixed effects manually yielded different results than with fixed effects regression commands. The answers for that post lied in different samples being used in xtreg and regress (and others) as a result of missing variables. This understanding has solved part of my issue, but not completely.
The post has helped me in two instances:
1) Manually computing panel variable (country) fixed effects, and getting the same beta coefficient as xtreg, fe
2) Manually computing time variable (year) fixed effects, and getting the same beta coefficient as xtreg i.yr
However, the lingering problem is that I cannot get the same beta coefficients as when running an xtreg with BOTH i.yr and fe. An example dta is below. My code is as follows. (For variable legibility: "m" in the prefix refers to mean; "d" in the prefix refers to difference; "c" in the prefix refers to country; "y" in the prefix refers to year)
*Country fixed effects, THIS WORKS because both xtreg and regress output the same beta coefficient
tsset countrynum yr
egen nmiss = rmiss(dependent independent)
bysort countrynum: egen mc_dependent=mean(dependent) if nmiss==0
g dc_dependent=dependent-mc_dependent
bysort countrynum: egen mc_independent=mean(independent) if nmiss==0
g dc_independent=independent-mc_independent
xtreg dependent independent, fe
regress dc_dependent dc_independent
*Year fixed effects, THIS WORKS because both xtreg and regress output the same beta coefficient
gsort yr
bysort yr: egen my_dependent=mean(dependent) if nmiss==0
g dy_dependent=dependent-my_dependent
bysort yr: egen my_independent=mean(independent)if nmiss==0
g dy_independent=independent-my_independent
xtreg dependent independent i.yr
regress dy_dependent dy_independent
* Year and country fixed effects - This is the part I have questions about, xtreg and regress do not output the same beta coefficient. This post (https://stats.stackexchange.com/ques...el-regressions) suggests that the logic behind the code I am using to compute both panel and year fixed effects is correct, and there should be no problem with the missing values (I have taken care of this above) but I've been working on this for a while, tried various combinations, and there is clearly something I'm not fully grasping.
g dcy_dependent=dependent-my_dependent-mc_dependent
g dcy_independent=independent-my_independent-mc_independent
xtreg dependent independent i.yr, fe
regress dcy_dependent dcy_independent
Thank you very much.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input int(yr countrynum) float(dependent independent) 1991 112 .021465236 .8981605 1991 111 .3605212 .9185835 1992 111 . .9850195 1992 112 .17781587 .7908565 1993 112 .53612924 .6076079 1993 111 .4550523 .15091026 1994 112 .20062463 .9830886 1994 111 .8150345 .2802703 1995 112 .2947711 .3481235 1995 111 .9478846 .6256541 1996 112 .6419758 .59046483 1996 111 .8291642 .6529766 end
Comment