0% found this document useful (0 votes)
35 views34 pages

Outreg2(1)

The document provides a comprehensive guide on using Stata for creating output presentations, focusing on regression models, descriptive statistics, and custom tables. It includes detailed instructions and code examples for various statistical analyses, including OLS, logit, and probit models, as well as methods for exporting results to Word. The guide is authored by Oscar Torres-Reyna and was first published in March 2014, with the latest revision in August 2024.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views34 pages

Outreg2(1)

The document provides a comprehensive guide on using Stata for creating output presentations, focusing on regression models, descriptive statistics, and custom tables. It includes detailed instructions and code examples for various statistical analyses, including OLS, logit, and probit models, as well as methods for exporting results to Word. The guide is authored by Oscar Torres-Reyna and was first published in March 2014, with the latest revision in August 2024.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Nice output presentation

using Stata code


(v3.3)

Oscar Torres-Reyna
otorres@princeton.edu

First published: March 2014


http://www.princeton.edu/~otorres/
Last revised: August 2024
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)
. outreg2 using myreg.doc, replace ctitle(Model 1)
myreg.doc Windows users click here to open the file myreg.doc in Word.

dir : seeout
Mac users click on “dir” to go to the directory where myreg.doc is saved, open it with 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)

The file my_panel_reg.doc will be


saved in the working directory

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
Logit and probit models
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)

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.)

Oscar Torres-Reyna 9
For more details/options and examples type help outreg2
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

Oscar Torres-Reyna
OTR 11
PU/DSS/OTR
See also:
asdoc: https://fintechprofessor.com/2018/01/31/asdoc/
tabout: https://ianwatson.com.au/stata/tabout_tutorial.pdf

DESCRIPTIVE STATISTICS

Oscar Torres-Reyna 12
Using outreg2 for summary statistics: all variables in dataset

sysuse auto, clear


outreg2 using x.doc, replace sum(log)
. outreg2 using x.doc, replace sum(log)

Variable Obs Mean Std. Dev. Min Max

price 74 6165.257 2949.496 3291 15906


mpg 74 21.2973 5.785503 12 41
rep78 69 3.405797 .9899323 1 5
headroom 74 2.993243 .8459948 1.5 5
trunk 74 13.75676 4.277404 5 23

weight 74 3019.459 777.1936 1760 4840


length 74 187.9324 22.26634 142 233
turn 74 39.64865 4.399354 31 51
displacement 74 197.2973 91.83722 79 425
gear_ratio 74 3.014865 .4562871 2.19 3.89

foreign 74 .2972973 .4601885 0 1

Following variable is string, not included:


make
Windows users click here to open the file x.doc in Word (you
x.doc
dir : seeout
can replace this name with your own) . Otherwise follow the
Mac instructions.

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)

Oscar Torres-Reyna
For more details/options and examples type help outreg2 13
Using outreg2 for summary statistics: selected variables

sysuse auto, clear


outreg2 using x.doc, replace sum(log) keep(price mpg turn)
. outreg2 using x.doc, replace sum(log) keep(price mpg turn)

Variable Obs Mean Std. Dev. Min Max

price 74 6165.257 2949.496 3291 15906


mpg 74 21.2973 5.785503 12 41
rep78 69 3.405797 .9899323 1 5
headroom 74 2.993243 .8459948 1.5 5
trunk 74 13.75676 4.277404 5 23

weight 74 3019.459 777.1936 1760 4840


length 74 187.9324 22.26634 142 233
turn 74 39.64865 4.399354 31 51
displacement 74 197.2973 91.83722 79 425
gear_ratio 74 3.014865 .4562871 2.19 3.89

foreign 74 .2972973 .4601885 0 1

Following variable is string, not included:


make
Windows users click here to open the file x.doc in Word (you
x.doc
dir : seeout can replace this name with your own) . Otherwise follow the
Mac instructions.

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)

Oscar Torres-Reyna
For more details/options and examples type help outreg2 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)
. outreg2 using x.doc, replace sum(log) keep(price mpg turn) eqkeep(N mean)

Variable Obs Mean Std. Dev. Min Max

price 74 6165.257 2949.496 3291 15906


mpg 74 21.2973 5.785503 12 41
rep78 69 3.405797 .9899323 1 5
headroom 74 2.993243 .8459948 1.5 5
trunk 74 13.75676 4.277404 5 23

weight 74 3019.459 777.1936 1760 4840


length 74 187.9324 22.26634 142 233
turn 74 39.64865 4.399354 31 51
displacement 74 197.2973 91.83722 79 425
gear_ratio 74 3.014865 .4562871 2.19 3.89

foreign 74 .2972973 .4601885 0 1

Following variable is string, not included:


make
Windows users click here to open the file x.doc in Word (you
x.doc
dir : seeout
can replace this name with your own) . Otherwise follow the
Mac instructions.

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)

Oscar Torres-Reyna
For more details/options and examples type help outreg2 15
Using outreg2 for summary statistics:
selected variables in dataset and detail statistics

*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)

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.

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)

Oscar Torres-Reyna 16
For more details/options and examples type help outreg2
Using outreg2 for summary statistics:
selected variables in dataset and selected detail statistics
*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)

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.

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)

Oscar Torres-Reyna 17
For more details/options and examples type help outreg2
Using outreg2 for summary statistics:
by group, selected variables in dataset and detail statistics

*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)

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.

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)

Oscar Torres-Reyna
For more details/options and examples type help outreg2 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]

See:
* https://www.stata.com/manuals/rtablemultiway.pdf#rtablemultiway
* https://www.stata.com/manuals/tablespredefinedstyles.pdf#tablesPredefinedstyles
Oscar Torres-Reyna 20
Numeric and factor variables in one table

Mean Standard deviation Factor-variable percent

Hourly wage 7.77 (5.76)


Usual hours worked 37.22 (10.51)
Total work experience (years) 12.53 (4.61)
Job tenure (years) 5.98 (5.51)
Age in current year 39.15 (3.06)
Race
White 72.9%
Black 26.0%
Other 1.2%
Married
Single 35.8%
Married 64.2%
Lives in a central city
Not central city 70.8%
Central city 29.2%
Lives in the south
Not south 58.1%
South 41.9%

Oscar Torres-Reyna 21
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

[SEE OUTPUT IN THE NEXT SLIDE]


Oscar Torres-Reyna 22
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]

See:
* https://www.stata.com/manuals/rtablemultiway.pdf#rtablemultiway
* https://www.stata.com/manuals/tablespredefinedstyles.pdf#tablesPredefinedstyles
Oscar Torres-Reyna 24
Numeric and factor variables by group
Nonunion Union Total

Mean
Hourly wage 7.20 8.67 7.57
Usual hours worked 37.26 38.66 37.61
Total work experience (years) 12.68 13.25 12.82
Job tenure (years) 6.14 7.89 6.57
Age in current year 39.21 39.28 39.22
Standard deviation
Hourly wage (4.10) (4.17) (4.17)
Usual hours worked (10.23) (9.11) (9.98)
Total work experience (years) (4.62) (4.55) (4.61)
Job tenure (years) (5.41) (6.11) (5.64)
Age in current year (3.04) (3.02) (3.03)
Factor-variable percent
Race
White 74.2% 65.5% 72.0%
Black 24.7% 32.8% 26.7%
Other 1.1% 1.7% 1.3%
Married
Single 33.5% 39.3% 34.9%
Married 66.5% 60.7% 65.1%
Lives in a central city
Not central city 73.0% 62.5% 70.4%
Central city 27.0% 37.5% 29.6%
Lives in the south
Not south 53.2% 70.5% 57.5%
South 46.8% 29.5% 42.5%

Oscar Torres-Reyna 25
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

[SEE OUTPUT IN THE NEXT SLIDE]


Oscar Torres-Reyna 26
Oscar Torres-Reyna 27
HYPOTHESIS TESTING

Oscar Torres-Reyna 28
Hypothesis testing
sysuse nlsw88.dta

ttest wage, by(union)

Two-sample t test with equal variances

Group Obs Mean Std. err. Std. dev. [95% conf. interval]

Nonunion 1,417 7.204669 .1090159 4.103694 6.990819 7.418519


Union 461 8.674294 .1944277 4.174539 8.292218 9.056371

Combined 1,878 7.565423 .0961874 4.168369 7.376778 7.754069

diff -1.469625 .2209702 -1.902999 -1.036252

diff = mean(Nonunion) - mean(Union) t = -6.6508


H0: diff = 0 Degrees of freedom = 1876

Ha: diff < 0 Ha: diff != 0 Ha: diff > 0


Pr(T < t) = 0.0000 Pr(|T| > |t|) = 0.0000 Pr(T > t) = 1.0000

Oscar Torres-Reyna 29
Hypothesis testing
return list

scalars:
r(level) = 95
r(sd) = 4.168369257933639
r(sd_2) = 4.174538873345928
r(sd_1) = 4.1036938325326
r(se) = .2209702313599325
r(p_u) = .9999999999809384
r(p_l) = 1.90616301182e-11
r(p) = 3.81232602363e-11
r(t) = -6.650784018445881
r(df_t) = 1876
r(mu_2) = 8.674294102217784
r(N_2) = 461
r(mu_1) = 7.204668818936856
r(N_1) = 1417

Oscar Torres-Reyna 30
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)

Yes No Difference p_value stars

ttest wage, by(union) 8.674 7.205 1.470 0.000 ***

*** p<.01, ** p<.05, * p<.1

See: https://www.stata.com/manuals/rtablehypothesistests.pdf#rtablehypothesistests
Oscar Torres-Reyna 31
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

Yes No Difference p_value stars

ttest wage, by(union) 8.674 7.205 1.470 0.000 ***


ttest hours, by(union) 38.659 37.262 1.397 0.009 ***
ttest ttl_exp, by(union) 13.254 12.677 0.577 0.019 **
ttest tenure, by(union) 7.888 6.141 1.747 0.000 ***
ttest age, by(union) 39.284 39.205 0.079 0.628

*** p<.01, ** p<.05, * p<.1

See: https://www.stata.com/manuals/rtablehypothesistests.pdf#rtablehypothesistests
Oscar Torres-Reyna 32
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

[SEE OUTPUT IN THE NEXT SLIDE]


Oscar Torres-Reyna 33
Oscar Torres-Reyna 34

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy