Nice output presentation
using Stata code
(v3.3)
Oscar Torres-Reyna
otorres@princeton.edu
First published: March 2014
Last revised: August 2024
http://www.princeton.edu/~otorres/
Contents
1. Regression models (OLS, logit, probit, fixed effects).
2. Descriptive statistics (Stata output as-is).
3. Custom tables for descriptive statistics combining
numeric and categorical variables.
4. Custom tables for hypothesis testing.
Oscar Torres-Reyna 2
REGRESSION MODELS
Oscar Torres-Reyna 3
Linear regression: single predictor
ssc install outreg2 // do this only once
sysuse nlsw88.dta // sample data
reg wage ttl_exp, robust
outreg2 using myreg.doc, replace label ctitle(Model 1) title(Table
XX: Linear regression)
dir : seeout
myreg.doc
. outreg2 using myreg.doc, replace ctitle(Model 1)
Mac users click on “dir” to go to the directory where myreg.doc is saved, open it with Word.
Windows users click here to open the file myreg.doc in Word.
Oscar Torres-Reyna 4
OLS regression: comparing multiple predictors
Adding other predictors, side-by-side comparison (make sure to close myreg.doc)
reg wage ttl_exp hours tenure, robust
outreg2 using myreg.doc, append label ctitle(Model 2)
NOTE: Other options for label: label(insert); label(proper); label(upper); label(lower).
Type help outreg2 for more details.
You also have the option to export to Excel, just use the extension *.xls.
Oscar Torres-Reyna 5
Comparing OLS with Fixed effects and time fixed effects
webuse nlswork // sample data
xtset idcode year // set as panel data
reg ln_w union ttl_exp tenure, robust
outreg2 using my_panel_reg.doc, replace label ctitle(OLS) title(Table XX: Comparing OLS and FE models)
xtreg ln_w union ttl_exp tenure, fe robust
outreg2 using my_panel_reg.doc, append label ctitle(FE indiv.) addtext(Individual FE, YES)
xtreg ln_w union ttl_exp tenure i.year, fe robust
outreg2 using my_panel_reg.doc, append label ctitle(FE/Time FE.) keep(union ttl_exp tenure)
addtext(Individual FE, YES, Year FE, YES)
In fixed effects models you do not
have to add the FE coefficients,
you can just add a note indicating
that the model includes fixed
effects. This can be added from
outreg2, see the option
addtex() above.
Oscar Torres-Reyna 6
The file my_panel_reg.doc will be
saved in the working directory
webuse lbw // sample data
logit low smoke ht lwt i.race , robust
outreg2 using mylogit_probit.doc, replace label ctitle(Logit: Log odds)
title(Table XX: Dependent Birthweight<2500g)
probit low smoke ht lwt i.race , robust
outreg2 using mylogit_probit.doc, append label ctitle(Probit: Z-score change)
Logit and probit models
Oscar Torres-Reyna 7
Logit model: odds ratio
logit low smoke ht lwt i.race , robust or
outreg2 using mylogit.doc, append label ctitle(Ods ratio) eform
For more details/options and examples type help outreg2
Oscar Torres-Reyna 8
Logit model: marginal effects
logit low smoke ht lwt i.race , robust
margins, dydx(*) post
outreg2 using mylogit.doc, append label ctitle(Marg. Eff.)
For more details/options and examples type help outreg2
Oscar Torres-Reyna 9
Logit model compare to linear probability model (LPM)
reg low smoke ht lwt i.race , robust
outreg2 using mylogit.doc, append label ctitle(LPM)
For more details/options and examples type help outreg2
Oscar Torres-Reyna 10
Logit / probit models
For predicted probabilities and marginal effects, see the following document
https://www.princeton.edu/~otorres/Margins.pdf
PU/DSS/OTR
OTR
Oscar Torres-Reyna 11
DESCRIPTIVE STATISTICS
See also:
asdoc: https://fintechprofessor.com/2018/01/31/asdoc/
tabout: https://ianwatson.com.au/stata/tabout_tutorial.pdf
Oscar Torres-Reyna 12
Using outreg2 for summary statistics: all variables in dataset
sysuse auto, clear
outreg2 using x.doc, replace sum(log)
dir : seeout
x.doc
make
Following variable is string, not included:
foreign 74 .2972973 .4601885 0 1
gear_ratio 74 3.014865 .4562871 2.19 3.89
displacement 74 197.2973 91.83722 79 425
turn 74 39.64865 4.399354 31 51
length 74 187.9324 22.26634 142 233
weight 74 3019.459 777.1936 1760 4840
trunk 74 13.75676 4.277404 5 23
headroom 74 2.993243 .8459948 1.5 5
rep78 69 3.405797 .9899323 1 5
mpg 74 21.2973 5.785503 12 41
price 74 6165.257 2949.496 3291 15906
Variable Obs Mean Std. Dev. Min Max
. outreg2 using x.doc, replace sum(log)
Mac users click here to go to the directory where x.doc is saved, open it
with Word (you can replace this name with your own)
Windows users click here to open the file x.doc in Word (you
can replace this name with your own) . Otherwise follow the
Mac instructions.
For more details/options and examples type help outreg2
Oscar Torres-Reyna 13
sysuse auto, clear
outreg2 using x.doc, replace sum(log) keep(price mpg turn)
Using outreg2 for summary statistics: selected variables
dir : seeout
x.doc
make
Following variable is string, not included:
foreign 74 .2972973 .4601885 0 1
gear_ratio 74 3.014865 .4562871 2.19 3.89
displacement 74 197.2973 91.83722 79 425
turn 74 39.64865 4.399354 31 51
length 74 187.9324 22.26634 142 233
weight 74 3019.459 777.1936 1760 4840
trunk 74 13.75676 4.277404 5 23
headroom 74 2.993243 .8459948 1.5 5
rep78 69 3.405797 .9899323 1 5
mpg 74 21.2973 5.785503 12 41
price 74 6165.257 2949.496 3291 15906
Variable Obs Mean Std. Dev. Min Max
. outreg2 using x.doc, replace sum(log) keep(price mpg turn)
Mac users click here to go to the directory where x.doc is saved, open it
with Word (you can replace this name with your own)
Windows users click here to open the file x.doc in Word (you
can replace this name with your own) . Otherwise follow the
Mac instructions.
For more details/options and examples type help outreg2
Oscar Torres-Reyna 14
Using outreg2 for summary statistics:
selected variables in dataset and selected statistics
sysuse auto, clear
outreg2 using x.doc, replace sum(log) keep(price mpg turn) eqkeep(N mean)
dir : seeout
x.doc
make
Following variable is string, not included:
foreign 74 .2972973 .4601885 0 1
gear_ratio 74 3.014865 .4562871 2.19 3.89
displacement 74 197.2973 91.83722 79 425
turn 74 39.64865 4.399354 31 51
length 74 187.9324 22.26634 142 233
weight 74 3019.459 777.1936 1760 4840
trunk 74 13.75676 4.277404 5 23
headroom 74 2.993243 .8459948 1.5 5
rep78 69 3.405797 .9899323 1 5
mpg 74 21.2973 5.785503 12 41
price 74 6165.257 2949.496 3291 15906
Variable Obs Mean Std. Dev. Min Max
. outreg2 using x.doc, replace sum(log) keep(price mpg turn) eqkeep(N mean)
Mac users click here to go to the directory where x.doc is saved, open it
with Word (you can replace this name with your own)
Windows users click here to open the file x.doc in Word (you
can replace this name with your own) . Otherwise follow the
Mac instructions.
For more details/options and examples type help outreg2
Oscar Torres-Reyna 15
*NOTE: The option “sum(detail)” will give all the summary
statistics shown below for the selected variables but it will
show in the output window results for all the variables in the
dataset. This is similar to typing “summarize, detail”
sysuse auto, clear
set more off
outreg2 using x.doc, replace sum(detail) keep(price mpg turn)
Using outreg2 for summary statistics:
selected variables in dataset and detail statistics
Mac users click here to go to the directory where x.doc is saved, open it
with Word (you can replace this name with your own)
Windows users click here to open the file x.doc in Word (you
can replace this name with your own) . Otherwise follow the
Mac instructions.
For more details/options and examples type help outreg2
Oscar Torres-Reyna 16
*NOTE: The option “sum(detail)” will give all the summary statistics shown below for
the selected variables but it will show in the output window results for all the
variables in the dataset. This is similar to typing “summarize, detail”
* The option “p50” gives the median
sysuse auto, clear
set more off
outreg2 using x.doc, replace sum(detail) keep(price mpg turn) eqkeep(N mean p50)
Using outreg2 for summary statistics:
selected variables in dataset and selected detail statistics
Mac users click here to go to the directory where x.doc is saved, open it
with Word (you can replace this name with your own)
Windows users click here to open the file x.doc in Word (you
can replace this name with your own) . Otherwise follow the
Mac instructions.
For more details/options and examples type help outreg2
Oscar Torres-Reyna 17
*NOTE: You need to specify either keeping statistics (eqkeep) and droping variables
(drop) or viceversa. You can’t specify eqkeep() and keep() at the same time
sysuse auto, clear
set more off
bysort foreign: outreg2 using x.doc, replace sum(log) eqkeep(N mean) drop(make rep78
headroom trunk weight length displacement gear_ratio)
Using outreg2 for summary statistics:
by group, selected variables in dataset and detail statistics
Mac users click here to go to the directory where x.doc is saved, open it
with Word (you can replace this name with your own)
Windows users click here to open the file x.doc in Word (you
can replace this name with your own) . Otherwise follow the
Mac instructions.
For more details/options and examples type help outreg2
Oscar Torres-Reyna 18
CUSTOM TABLES FOR
DESCRIPTIVE STATISTICS
Oscar Torres-Reyna 19
Numeric and factor variables in one table
sysuse nlsw88.dta // sample data
help table
table () (result), ///
statistic(mean wage hours ttl_exp tenure age) ///
statistic(sd wage hours ttl_exp tenure age) ///
statistic(fvpercent race married c_city south) ///
style(dtable) nformat(%8.2f mean sd) ///
sformat("(%s)" sd) sformat("%s%%" fvpercent) ///
name(table1) replace
[SEE OUTPUT IN THE NEXT SLIDE]
Oscar Torres-Reyna 20
See:
* https://www.stata.com/manuals/rtablemultiway.pdf#rtablemultiway
* https://www.stata.com/manuals/tablespredefinedstyles.pdf#tablesPredefinedstyles
Numeric and factor variables in one table
Oscar Torres-Reyna 21
South 41.9%
Not south 58.1%
Lives in the south
Central city 29.2%
Not central city 70.8%
Lives in a central city
Married 64.2%
Single 35.8%
Married
Other 1.2%
Black 26.0%
White 72.9%
Race
Age in current year 39.15 (3.06)
Job tenure (years) 5.98 (5.51)
Total work experience (years) 12.53 (4.61)
Usual hours worked 37.22 (10.51)
Hourly wage 7.77 (5.76)
Mean Standard deviation Factor-variable percent
Export table to Word
collect set table1 // after running the table command
* Add shading to specific variables
* https://www.stata.com/manuals/tablesappendix.pdf#tablesAppendix
collect style cell var[hours tenure i.race i.c_city],
shading(background(lightsteelblue))
collect title "Table XX. Descriptive statistics"
collect preview
* Export table to Word (click on the blue link)
*https://www.stata.com/manuals/tablescollectexport.pdf#tablescollecte
xport
collect export table1.docx, replace
Oscar Torres-Reyna 22
[SEE OUTPUT IN THE NEXT SLIDE]
Oscar Torres-Reyna 23
Numeric and factor variables by group
sysuse nlsw88.dta // sample data
help table
table () (union), ///
statistic(mean wage hours ttl_exp tenure age) ///
statistic(sd wage hours ttl_exp tenure age) ///
statistic(fvpercent race married c_city south) ///
style(dtable) nformat(%8.2f mean sd) ///
sformat("(%s)" sd) sformat("%s%%" fvpercent) ///
name(table1) replace
[SEE OUTPUT IN THE NEXT SLIDE]
Oscar Torres-Reyna 24
See:
* https://www.stata.com/manuals/rtablemultiway.pdf#rtablemultiway
* https://www.stata.com/manuals/tablespredefinedstyles.pdf#tablesPredefinedstyles
Numeric and factor variables by group
Oscar Torres-Reyna 25
South 46.8% 29.5% 42.5%
Not south 53.2% 70.5% 57.5%
Lives in the south
Central city 27.0% 37.5% 29.6%
Not central city 73.0% 62.5% 70.4%
Lives in a central city
Married 66.5% 60.7% 65.1%
Single 33.5% 39.3% 34.9%
Married
Other 1.1% 1.7% 1.3%
Black 24.7% 32.8% 26.7%
White 74.2% 65.5% 72.0%
Race
Factor-variable percent
Age in current year (3.04) (3.02) (3.03)
Job tenure (years) (5.41) (6.11) (5.64)
Total work experience (years) (4.62) (4.55) (4.61)
Usual hours worked (10.23) (9.11) (9.98)
Hourly wage (4.10) (4.17) (4.17)
Standard deviation
Age in current year 39.21 39.28 39.22
Job tenure (years) 6.14 7.89 6.57
Total work experience (years) 12.68 13.25 12.82
Usual hours worked 37.26 38.66 37.61
Hourly wage 7.20 8.67 7.57
Mean
Nonunion Union Total
Export table to Word
collect set table2 // after running the table command
* Add shading to specific variables
* https://www.stata.com/manuals/tablesappendix.pdf#tablesAppendix
collect style cell var[hours tenure i.race i.c_city],
shading(background(lightsteelblue))
collect title "Table XX. Descriptive statistics by being in a union"
collect preview
* Export table to Word (click on the blue link)
*https://www.stata.com/manuals/tablescollectexport.pdf#tablescollecte
xport
collect export table2.docx, replace
Oscar Torres-Reyna 26
[SEE OUTPUT IN THE NEXT SLIDE]
Oscar Torres-Reyna 27
HYPOTHESIS TESTING
Oscar Torres-Reyna 28
Hypothesis testing
sysuse nlsw88.dta
ttest wage, by(union)
Oscar Torres-Reyna 29
Pr(T < t) = 0.0000 Pr(|T| > |t|) = 0.0000 Pr(T > t) = 1.0000
Ha: diff < 0 Ha: diff != 0 Ha: diff > 0
H0: diff = 0 Degrees of freedom = 1876
diff = mean(Nonunion) - mean(Union) t = -6.6508
diff -1.469625 .2209702 -1.902999 -1.036252
Combined 1,878 7.565423 .0961874 4.168369 7.376778 7.754069
Union 461 8.674294 .1944277 4.174539 8.292218 9.056371
Nonunion 1,417 7.204669 .1090159 4.103694 6.990819 7.418519
Group Obs Mean Std. err. Std. dev. [95% conf. interval]
Two-sample t test with equal variances
Hypothesis testing
return list
Oscar Torres-Reyna 30
r(N_1) = 1417
r(mu_1) = 7.204668818936856
r(N_2) = 461
r(mu_2) = 8.674294102217784
r(df_t) = 1876
r(t) = -6.650784018445881
r(p) = 3.81232602363e-11
r(p_l) = 1.90616301182e-11
r(p_u) = .9999999999809384
r(se) = .2209702313599325
r(sd_1) = 4.1036938325326
r(sd_2) = 4.174538873345928
r(sd) = 4.168369257933639
r(level) = 95
scalars:
Hypothesis testing
table (command) (result),///
command(Yes=r(mu_2) No=r(mu_1) ///
Difference= (r(mu_2) - r(mu_1)) ///
p_value= r(p): ttest wage, by(union)) ///
nformat(%9.3f) ///
stars(p_value 0.1 "*" 0.05 "**" 0.01 "***",
shownote)
Oscar Torres-Reyna 31
See: https://www.stata.com/manuals/rtablehypothesistests.pdf#rtablehypothesistests
Hypothesis testing, multiple variables
table (command) (result), ///
command(Yes=r(mu_2) No=r(mu_1) Difference= (r(mu_2) - r(mu_1)) p_value = r(p): ttest wage, by(union)) ///
command(Yes=r(mu_2) No=r(mu_1) Difference= (r(mu_2) - r(mu_1)) p_value = r(p): ttest hours, by(union)) ///
command(Yes=r(mu_2) No=r(mu_1) Difference= (r(mu_2) - r(mu_1)) p_value = r(p): ttest ttl_exp, by(union)) ///
command(Yes=r(mu_2) No=r(mu_1) Difference= (r(mu_2) - r(mu_1)) p_value = r(p): ttest tenure, by(union)) ///
command(Yes=r(mu_2) No=r(mu_1) Difference= (r(mu_2) - r(mu_1)) p_value = r(p): ttest age, by(union)) ///
nformat(%9.3f) stars(p_value 0.1 "*" 0.05 "**" 0.01 "***", shownote) name(t_test1) replace
Oscar Torres-Reyna 32
See: https://www.stata.com/manuals/rtablehypothesistests.pdf#rtablehypothesistests
*** p<.01, ** p<.05, * p<.1
ttest age, by(union) 39.284 39.205 0.079 0.628
ttest tenure, by(union) 7.888 6.141 1.747 0.000 ***
ttest ttl_exp, by(union) 13.254 12.677 0.577 0.019 **
ttest hours, by(union) 38.659 37.262 1.397 0.009 ***
ttest wage, by(union) 8.674 7.205 1.470 0.000 ***
Yes No Difference p_value stars
Export table to Word
collect set t_test1 // after running the table command
collect label levels command 1 "Hourly wage" 2 "Usual hours worked" 3
"Work experience (years)" 4 "Job tenure" 5 "Age in current year",
modify
collect title "Table XX. T-tests comparing union vs non-union members
across different metrics"
collect preview
collect style cell command[2 4]#result[Yes No Difference p_value
stars], shading(background(lightsteelblue))
collect export t_test1.docx, replace
Oscar Torres-Reyna 33
[SEE OUTPUT IN THE NEXT SLIDE]
Oscar Torres-Reyna 34