Skip to content

Instantly share code, notes, and snippets.

@lsfalimis
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsfalimis/5c68989deeb793e7aa37 to your computer and use it in GitHub Desktop.
Save lsfalimis/5c68989deeb793e7aa37 to your computer and use it in GitHub Desktop.
Stata (whole and regional) panel data analysis
/*
author: lsfalimis
blog: http://lsfalimis.github.io/
Stata setup: Stata 13 on Win
output path: C:\data
0. -search- to search for packages, -help COMMAND- to look up the manual of COMMAND where you can also find abbreviation.
1. Prepare data in excel which has id, year, y, x1, x2, x3 and region.
2. Take (natural) logarithm of y, x1, x2, x3.
3. Produce LaTeX tables of descriptive statistics with paranoiac syntax, before and after taking logarithm; using outreg2.
4. Produce plain text tables of correlation using mkcorr which doesn't support conditionals,
that's why I generate new variables: [a-c]y, [a-c]x[1-3], [a-c]ly, and [a-c]lx[1-3], CRAZY!!!
AND mkcorr DOESN'T SUPPORT APPEND!!!
Why don't I use -log- to get corr table? Because I want more decimals, but Stata can't do that, CRAZY!!!
If Stata actually can, tell me via messages, thank you!
5. Produce LaTeX tables of FEM and REM using outreg2;
I append the REM result to FEM one;
four column with *** stuff is too wide for LaTeX so I sort latter three groups in another table;
there are some results can't be display in outreg2 table so I -log- them in log file.
6. -xttest3- tests groupwise heteroskedasticity of panel data.
7. -hausman- tests that is REM better (H0) or FEM better (H1); I -qui- (-quietly-) -xtreg- because I already have them.
8. Happy graduation! This is enough to write an empirical dissertation!
*/
set more off
import excel "C:\Users\X\Desktop\X.xlsx", sheet("Sheet1") firstrow
xtset id year
foreach e of var y x1 x2 x3 {
g l`e' = ln(`e')
g a`e'=`e' if region == "AAA"
g b`e'=`e' if region == "BBB"
g c`e'=`e' if region == "CCC"
}
foreach e of var ly lx1 lx2 lx3 {
g e`e'=`e' if region == "AAA"
g m`e'=`e' if region == "BBB"
g w`e'=`e' if region == "CCC"
la var `e' "$\ln{\left(\T{`e'}\right)}$"
la var e`e' "e$\ln{\left(\T{`e'}\right)}$"
la var m`e' "m$\ln{\left(\T{`e'}\right)}$"
la var w`e' "w$\ln{\left(\T{`e'}\right)}$"
}
qui outreg2 using tab1sum1.tex, replace sum(log) dec(6) fmt(f) keep(y x1 x2 x3 ay ax1 ax2 ax3 by bx1 bx2 bx3 cy cx1 cx2 cx3)
qui outreg2 using tab2sum2.tex, replace sum(log) lab dec(6) fmt(f) keep(ly lx1 lx2 lx3 aly alx1 alx2 alx3 bly blx1 blx2 blx3 cly clx1 clx2 clx3)
mkcorr y x1 x2 x3, log(corr1) num cdec(6)
mkcorr ay ax1 ax2 ax3, log(corr2) num cdec(6)
mkcorr by bx1 bx2 bx3, log(corr3) num cdec(6)
mkcorr cy cx1 cx2 cx3, log(corr4) num cdec(6)
mkcorr ly lx1 lx2 lx3, log(corr5) num cdec(6)
mkcorr aly alx1 alx2 alx3, log(corr6) num cdec(6)
mkcorr bly blx1 blx2 blx3, log(corr7) num cdec(6)
mkcorr cly clx1 clx2 clx3, log(corr8) num cdec(6)
log using log, text
xtreg ly lx1 lx2 lx3, fe robust
outreg2 using tab3.tex, replace lab dec(6) ct(FE)
xtreg ly lx1 lx2 lx3, re
outreg2 using tab3.tex, append lab dec(6) ct(RE)
xtreg ly lx1 lx2 lx3 if region == "AAA", fe robust
outreg2 using tab4.tex, replace lab dec(6) ct(FE)
xtreg ly lx1 lx2 lx3 if region == "AAA", re
outreg2 using tab4.tex, append lab dec(6) ct(RE)
xtreg ly lx1 lx2 lx3 if region == "BBB", fe robust
outreg2 using tab4.tex, append lab dec(6) ct(FE)
xtreg ly lx1 lx2 lx3 if region == "BBB", re
outreg2 using tab4.tex, append lab dec(6) ct(RE)
xtreg ly lx1 lx2 lx3 if region == "CCC", fe robust
outreg2 using tab4.tex, append lab dec(6) ct(FE)
xtreg ly lx1 lx2 lx3 if region == "CCC", re
outreg2 using tab4.tex, append lab dec(6) ct(RE)
qui xtreg ly lx1 lx2 lx3, fe
xttest3
qui xtreg ly lx1 lx2 lx3 if region == "AAA", fe
xttest3
qui xtreg ly lx1 lx2 lx3 if region == "BBB", fe
xttest3
qui xtreg ly lx1 lx2 lx3 if region == "CCC", fe
xttest3
qui xtreg ly lx1 lx2 lx3, fe
est store fe
qui xtreg ly lx1 lx2 lx3, re
hausman fe
qui xtreg ly lx1 lx2 lx3 if region == "AAA", fe
est store fe
qui xtreg ly lx1 lx2 lx3 if region == "AAA", re
hausman fe
qui xtreg ly lx1 lx2 lx3 if region == "BBB", fe
est store fe
qui xtreg ly lx1 lx2 lx3 if region == "BBB", re
hausman fe
qui xtreg ly lx1 lx2 lx3 if region == "CCC", fe
est store fe
qui xtreg ly lx1 lx2 lx3 if region == "CCC", re
hausman fe
log close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment