You are on page 1of 23

1

Storing, analyzing, and


presenting Stata output

Julian Reif
University of Chicago
Stata Conference Boston
July 15, 2010

Handling Stata output


Storing

regsave, svret

Analyzing

Excel PivotTables

Presenting

texsave (LaTeX tables)

Why use these Stata commands?


Copy/pasting results is slow and error-prone
With these commands, write one Stata script to:








Run analyses
Store and manipulate results
Output results into a table linked to a paper, MS Excel, etc.

These commands separate the storing of results from the


outputting of results

Storing output

Storing output
Stata commands usually store output in e() and r()

. qui sysuse auto, clear


. qui summ price
. return list
sc alars:
r(N)
r(sum_w)
r(mean)
r(Var)
r(sd)
r(min)
r(max)
r(sum)

=
=
=
=
=
=
=
=

74
74
6 165.256756756757
8 699525.974268789
2 949.495884768919
3 291
1 5906
4 56229

Modules regsave and svret convert these into Stata datasets


Results can then be manipulated with standard Stata
commands




Use svret to store returned results


svret stores returned results

Can save macros and scalars stored in e(), r(), and s()

Syntax:
svret [classlist] [, long type(type)
format(%fmt) keep(returnlist)]

where
classlist is one or more of the following: e, r, s, all,
type can be one of the following: all, scalars, or macros,
and
returnlist is a list of returned results currently in
memory, e.g., r(N).
6

svret: example 1
. s ys us e au to , c le ar
( 19 78 A ut om ob ile D at a)
. s um m pr ic e
V ar ia bl e

O bs

Mea n

pr ic e

74

61 65 .25 7

. s vr et , lo ng fo rm at (% 8. 2f c)
. l is t
va ri ab le

c on te nt s

1.
2.
3.
4.
5.

r( N)
r( Va r)
r( ma x)
r (m ea n)
r( mi n)

74
8, 69 9, 52 6
1 5, 90 6
6 ,1 65 .2 6
3, 29 1

6.
7.
8.

r (s d)
r( su m)
r( su m_ w)

2 ,9 49 .5 0
45 6, 22 9
74

S td . De v.
2 94 9. 49 6

Mi n

M ax

3 29 1

1 59 06

Use regsave to store regression output


regsave stores regression output




Coefficients, standard errors


Optionally: p-values, t-stats, confidence intervals, covariances,
and more

User can optionally specify:





Coefficient and variance-covariance matrices


Labels
Table format

(Abbreviated) syntax:
regsave [coeflist] [using filename] [,
tstat pval ci level(#) addlabel(string)
addvar(string) table(name, *) *]


regsave: example 1
. sysuse auto, clear
(1978 Automobile Data)
.
. regress price mpg trunk
Source

SS

df

MS

Model
Residual

141126459
493938937

2
71

70563229.4
6956886.44

Total

635065396

73

8699525.97

price

Coef.

mpg
trunk
_cons

-220.1649
43.55851
10254.95

Std. Err.
65.59262
88.71884
2349.084

t
-3.36
0.49
4.37

Number of obs
F( 2,
71)
Prob > F
R-squared
Adj R-squared
Root MSE
P>|t|
0.001
0.625
0.000

=
=
=
=
=
=

74
10.14
0.0001
0.2222
0.2003
2637.6

[95% Conf. Interval]


-350.9529
-133.3418
5571.01

-89.3769
220.4589
14938.89

.
. regsave, tstat pval ci
.
. list

1.
2.
3.

var

coef

stderr

tstat

pval

ci_lower

ci_upper

r2

mpg
trunk
_cons

-220.1649
43.55851
10254.95

65.59263
88.71884
2349.084

-3.356549
.4909725
4.36551

.0012707
.6249601
.0000423

-350.9529
-133.3418
5571.01

-89.3769
220.4588
14938.89

74
74
74

.2222235
.2222235
.2222235

Analyzing output

10

Large sets of results can be conveniently


analyzed in Excel PivotTables




(Instructions for MS Office Excel 2007)


Step 1: Open Microsoft Office Excel
Step 2: Click Data->From text






Import outsheeted results


Click Finish, then Ok

Step 3: Click Insert->PivotTable, then Ok


Step 4: Arrange PivotTable as desired

11

regsave: example 2
sysuse auto, clear
tempfile tmpfile
gen lnprice = ln(price)
local replace "replace"
foreach reg in "probit" "logit" "regress" {
foreach regressors in "lnprice" "lnprice rep78" "lnprice rep78 trunk" {
foreach stderr in "vce(robust)" "vce(cluster foreign)" "" {
`reg foreign `regressors, `stderr
regsave using "`tmpfile", p \\\
addlabel(vce,`stderr,regressors,`regressors,reg,`reg) `replace
local replace "append"
}
}
}
* Format and outsheet results for use in PivotTable
use "`tmpfile", clear
replace var = subinstr(var,"foreign:","",.)
outsheet using results.txt, replace
12

PivotTable: view 1

13

PivotTable: view 2

14

Presenting output

15

regsave can create standard tables


. qu i s ysu se aut o.d ta, cl ear
.
. qu i r egr ess pr ice mp g t run k h ead ro om len gth
.
. qu i r egs ave us ing re sul ts, ta ble (r eg, pa ren the ses (st der r) for mat (%8 .2f c)) re pla ce
.
. qu i a reg pr ice mp g t run k h ead roo m len gth , a bso rb( for eig n)
.
. qu i r egs ave us ing re sul ts, ta ble (a reg , p are nth ese s(s tde rr) fo rma t(% 8.2 fc) ) a ppe nd
.
. us e r esu lts , c lea r
.
. li st
var

re g

a reg

1.
2.
3.
4.
5.

mp g_c oef
m pg_ std err
t run k_c oef
tru nk_ std err
h ead roo m_c oef

-17 3.9 5
(87 .75 )
8 0.9 1
( 119 .82 )
-68 0.2 9

- 139 .90
( 82. 30)
38 .45
(1 12. 24)
- 596 .43

6.
7.
8.
9.
10.

hea dro om_ std err


le ngt h_c oef
l eng th_ std err
_ con s_c oef
_co ns_ std err

( 486 .01 )
2 3.2 9
(27 .03 )
6 ,41 6.9 5
(6, 036 .94 )

(4 53. 11)
65 .43
( 28. 04)
-1 895 .75
( 6,1 26. 44)

11.
12.

N
r2

74
0.2 5

74
0 .36

16

regsave can create standard tables contd


. replace var = "" if strpos(var,"stderr")
(5 real changes made)
.
. replace var = subinstr(var,"_coef","",.)
(5 real changes made)
.
. list

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

17

var

reg

areg

mpg

-173.95
(87.75)
80.91
(119.82)
-680.29

-139.90
(82.30)
38.45
(112.24)
-596.43

(486.01)
23.29
(27.03)
6,416.95
(6,036.94)

(453.11)
65.43
(28.04)
-1895.75
(6,126.44)

74
0.25

74
0.36

trunk
headroom
length
_cons
N
r2

Use texsave to create LaTeX tables




texsave creates LaTeX tables






Supports booktabs package


100% compatible with Scientific Workplace
Supports titles, footnotes, borderlines and many LaTeX options

(Abbreviated) syntax:
texsave [varlist] using filename [if]
[in] [, title(string) size(numlist)
marker(string) hlines(numlist)
footnote(*) booktabs frag replace *]


18

texsave: example 1
. sysuse auto.dta, clear
(1978 Automobile Data)
. texsave make mpg trunk if price > 8000 using "table.tex", title(MPG and trunk space) footnote("*Variable trunk is measu
> red in cubic feet") replace

19

texsave: example 2
sysuse auto, clear
tempfile tmpfile
gen lnprice = ln(price)
local replace "replace"
foreach reg in "probit" "logit" "scobit" {
`reg foreign lnprice rep78 trunk
regsave using "`tmpfile", addlabel(Regression,"`reg") \\\
table(`reg, asterisk(5 1) parentheses(stderr) format(%8.3fc)) `replace
local replace "append"
}
* Create and format LaTeX table
use "`tmpfile", clear
replace var = subinstr(var,"foreign:","",1)
replace var = subinstr(var,"_coef","",1)
replace var = "" if strpos(var,"stderr")!=0
replace var = "lnalpha" if strpos(var,"lnalpha")!=0
label variable var "Variable name"
local fn "A */** next to coefficient indicates significance at the 5/1\% level."
texsave using "table.tex", title(My regressions) booktabs hlines(10) \\\
autonumber footnote("`fn") varlabels replace
20

texsave: example 2s table

21

Advanced texsave options




frag


marker, align, location




Give the table a tag and specify its alignment and location

headerlines(string), footlines(string), headlines(string)




Create a table that can be linked to a LaTeX document via


/input{} command

Insert additional LaTeX code before, after, or in the header of


your table

Many LaTeX table formatting options also available





22

Alignment and size of footnote


Font styles (italics, bold-faced, etc.)

Feedback and feature requests welcome




Email: jreif@uchicago.edu

23

You might also like