Sunteți pe pagina 1din 59

Advanced Stata Workshop

FHSS Research Support


Center

Presentation Layout

Visualization and Graphing


Macros and Looping
Panel and Survey Data
Postestimation

Visualization and Graphing


in Stata
Life expectancy at birth vs. GNP per capita
Fraction

.1

0 2.5

2.5

loggnp
3.5

4.5

4.5

0 55 60 65 70 75 80
Life expectancy at birth

.2

.2

.15

.1
.05
Fraction

Life expectancy at birth


55 60 65 70 75 80

.3

Source: 1998 data from The World Bank Group

3.5
loggnp

Intro To Graphing In Stata


. sysuse auto, clear

graph is often optional. So


is twoway in this case.

. graph twoway scatter mpg weight //Note that you don't need to type graph or twoway

10

20

Mileage (mpg)
30

40

. scatter mpg weight

5,000

Price

10,000

15,000

Note: Nearly all graphing


commands start with
graph, and twoway is a
large family of graphs.

Creating Multiple Graphs with


by():
. twoway scatter mpg weight, by(foreign)

Domestic

Foreign

30
20
10

Mileage (mpg)

40

Note that the value label is


displayed above the graphs,
and the variable label is
displayed in the bottom right
hand corner.

2,000

3,000

4,000

5,000

2,000

Weight (lbs.)
Graphs by Car type

3,000

4,000

5,000

Overlaying twoway graphs


. twoway scatter mpg weight || lfit mpg weight

10

10

20

20

30

30

40

40

. twoway (scatter mpg weight) (lfit mpg weight)

2,000

3,000
Weight (lbs.)
Mileage (mpg)

4,000
Fitted values

The || tells Stata to put the


second graph on top of the
first one order matters! You
dont need to type twoway
twice; it applies to both.

5,000

2,000

3,000
Weight (lbs.)
Mileage (mpg)

4,000
Fitted values

This is another way of writing


the command it doesnt
matter which one you use.

5,000

"by()" statements with overlaid graphs


. twoway (qfitci mpg weight, stdf) (scatter mpg weight), by(foreign)

qfitci is a type of graph


which plots the prediction
line from a quadratic
regression, and adds a
confidence interval. The
stdf option specifies that
the confidence interval be
created on the basis

Foreign

10

20

30

40

Domestic

2000

3000

4000

5000

2000

3000

4000

5000

Weight (lbs.)
95% CI
Mileage (mpg)

Fitted values

Graphs by Car type

. twoway (qfitci mpg weight, stdf) (scatter mpg weight), by(foreign)

stdf is an option of qfitci.

by(foreign) is an option of
twoway.

"by()" statements with overlaid graphs


Another way of writing the
previous command is:
. twoway qfitci mpg weight, stdf || scatter mpg weight ||, by(foreign)
Foreign

10

20

30

40

Domestic

2000

3000

4000

5000

2000

3000

4000

5000

Weight (lbs.)
95% CI
Mileage (mpg)

Fitted values

Graphs by Car type

So:
. twoway (qfitci mpg weight, stdf) (scatter mpg weight), by(foreign)

. twoway qfitci mpg weight, stdf || scatter mpg weight ||, by(foreign)

This was is easier to


read.
This way is easier to
type.

Graphs with Many Options and Overlays


You can make pretty impressive graphs just from code, if you overlay
the graphs and specify certain options like: multiple axes, notes, titles
and subtitles, axis titles and labels, and legends.

Code for Previous Graph


.
.
.
.
.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
.
.

use http://www.stata-press.com/data/r12/uslifeexp, clear


generate diff = le_wm - le_bm
label var diff "Difference"
#delimit ;
twoway line le_wm year, yaxis(1 2) xaxis(1 2)
|| line le_bm year
|| line diff year
|| lfit diff year
||,
ytitle( "", axis(2) )
xtitle( "", axis(2) )
xlabel( 1918, axis(2) )
ylabel( 0(5)20, axis(2) grid gmin angle(horizontal) )
ylabel( 0 20(10)80, gmax angle(horizontal) )
ytitle( "Life expectancy at birth (years)" )
title( "White and black life expectancy" )
subtitle( "USA, 1900-1999" )
note( "Source: National Vital Statistics, Vol 50, No. 6"
"(1918 dip caused by 1918 Influenza Pandemic)" )
legend(label(1 "White males") label(2 "Black males") );
#delimit cr
#delimit cr

This may look scary, but it is


actually fairly
straightforward. See the
accompanying do-file for
explanation of each
component.

Using the Graph Editor


It is often easier to make changes
in the graph editor than to specify
all the options in code.

14

. tsline nci abc

ABC.com Inc.
Closing Share Price vs. Nasdaq Composite Index

12

Sep 24, 2009 - June 7, 2010


16

10

Share Price (USD)

14
12
10
8
6
4

2
0

01oct2009

01jan2010

date

NASDAQ Composite Index

01apr2010

01jul2010

Oct 1, 2009
Dec 1, 2009
Feb 1, 2010
Apr 1, 2010
Jun 1, 2010
Nov 1, 2009
Jan 1, 2010
Mar 1, 2010
May 1, 2010

NASDAQ Composite Index

ABC.com, Inc. share price

ABC.com, Inc. share price

Source: CRSP, Bloomberg

Lets make graph 1 into graph 2 by


using the graph editor tools.

Recording Edits in the Graph Editor


Before you start making changes, click the record
button. After you are done, click it again, and save
your changes as a recording so you can play
them back later. We will save this recording as
advanced_workshop_1.

Graph Element Change


Graph Title

Enter Title using quotes to separate lines, color=black

Graph Subtitle

Enter subtitle

Graph Region

X- Axis

Color = Bluish-gray
Range = 0 to 16 by 2, axis line = medium thick, add title, label
angle = horizontal, grid lines = of
title = of, minor ticks = of, suggest # of ticks = 8,
alternate spacing of adjacent labels = on, change label format,
label size=small,
axis line = medium thick

Plot 1 line

color=green, width = thick

Plot 2 line

color = blue, width = thick

Caption

Add caption

Y-Axis

Play Your Graph Recording


You can create a graph, open the graph editor, click the green play
button, and then play back your recorded edits.
Or, you can play your edits right from the code:
. tsline nci abc, play(advanced_workshop_1)

You can also run all of your


recorded edits on a diferent
graph, and just change the title:
. tsline comp_world comp_planet , play(advanced_workshop_1)

You can run your recorded edits


on a graph of a diferent type,
though in this case not all of
your edits will make sense:
. twoway (scatter nci date) (scatter abc date) ///
> , play(advanced_workshop_1)

ABC.com Inc.
Closing Share Price vs. Nasdaq Composite Index

ABC.com Inc.
Closing Share Price vs. Nasdaq Composite Index

Sep 24, 2009 - June 7, 2010


16

14

14
Share Price (USD)

Share Price (USD)

Sep 24, 2009 - June 7, 2010


16
12
10
8
6
4

12
10
8
6
4
2

0
Oct 1, 2009
Dec 1, 2009
Feb 1, 2010
Apr 1, 2010
Jun 1, 2010
Nov 1, 2009
Jan 1, 2010
Mar 1, 2010
May 1, 2010

Computer World share price


Source: CRSP, Bloomberg

Computer Planet share price

Oct 1, 2009
Dec 1, 2009
Feb 1, 2010
Apr 1, 2010
Jun 1, 2010
Nov 1, 2009
Jan 1, 2010
Mar 1, 2010
May 1, 2010

NASDAQ Composite Index


Source: CRSP, Bloomberg

ABC.com, Inc. share price

Storing and Moving Your Recordings


Graph recordings are stored as .grec files in your
personal folder, under the grec folder. Type
personal to see where this is; normally it is
C:\ado\personal. So by default Stata should store
.grec files in C:\ado\personal\grec.
.your
personal
your personal ado-directory is c:\ado\personal\
. dir c:\ado\personal\grec\
0.4k
2/21/13 9:12 advanced_workshop_1.grec
0.7k
3/01/12 9:48 jeff_test_recording_graph_edits.grec
0.9k
5/17/12 15:47 line..grec
1.3k 11/21/12 10:12 x grid.grec

Unfortunately, if you are not faculty, you are probably using lab
computers to use Stata, and when they are re-imaged, you will
lose the files in your grec folder. So you can store the recordings
on your flash drive by clicking the Browse button when you save
your recording. Now, when you are in the graph editor and click
the play button, your recording will not appear in the list because
it is not stored where Stata knows to look for it. Never fear, just
click Browse, and navigate to where your .grec file is. If you want
your recording to be available right from code, as in
play(advanced_workshop_1), you will need to move it (at least

Using Schemes in Graphing


Recordings are great if you are going to be making the same kind of
graph a lot. But a recording for a scatter plot will hardly afect a
histogram at all, and might even make it look terrible. If you want to
change the look of all graphs that you make, you may want to make a
scheme. Schemes are text files which tell Stata how to draw graphs.
. scatter le year, scheme(economist)

. sysuse uslifeexp2, clear


. scatter le year

life expectancy
50
55

55

45

40

50

40
1900

1910

1920
Year

1930

1940

1900

1910

1920
Year

1930

1940

life expectancy

60

60

45

65

65

More on Schemes
. graph query, schemes
Available schemes are
s2color
s2mono
s2manual
s2gmanual
s2gcolor
s1color
s1mono
s1rcolor
s1manual
sj
economist

see help scheme_s2color


see help scheme_s2mono
see help scheme_s2manual

see
see
see
see
see
see

help
help
help
help
help
help

scheme_s1color
scheme_s1mono
scheme_s1rcolor
scheme_s1manual
scheme_sj
scheme_economist

Schemes are very powerful, because they let your implement a certain
look without specifying a long series of options in every graph, or
running every graph through the graph editor. However, creating
schemes is fairly time consuming.
For more on creating your own schemes, see:
http://www3.eeg.uminho.pt/economia/nipe/2010_Stata_UGM/papers/Risi
ng.pdf
And

Manipulating Graphs: Memory vs. Disk


When you draw a graph, it is stored in memory, under the name Graph.
. sysuse auto, clear
. scatter price mpg

If you draw another graph, it replaces the previous one in memory, and is
. scatter
price
length
now
called
Graph.
If .you
want
have
multiple graphs up at the same time, you can use the
scatter
price to
mpg,
name(scatter1)
name option.
. cd C:\Users\nickj22\Downloads\
. graph save scatter1 mygraph1.gph

graph save moves your graph from memory to disk, saving it as a .gph
file.
. graph dir
Graph

scatter1

mygraph1.gph

graph dir lists all graphs in memory and on disk (in the current directory)
. graph drop scatter1

graph drop drops a graph from memory. Graphs contain the data files
they represent, so if the dataset is large, they can actually take up quite a

Manipulating Graphs: Demo


Graph manipulation commands are quite useful for exploratory analysis.
See do file for code.

More Example Graphs


Note: Annotated code is in the do file for all of these
Histogram, with overlaid normal distribution
Avg. education level

Avg. education level

NE

N Cntrl

20 40 60

Percent

12

12

20 40 60

Percent
10

15

16

12

Avg. education level

8
6

22

22

33

22

17

Avg. education level

Avg. education level

South

West
38

38
25
13

13

31
15

50
33

20

20

9.5

10

10
10.5
average education level
Source: US Census, 1980 and 1990

11

9.5

Percent
normal educ
Percent

9.5

10.5

average education level

11
Graphs by Census region

10

10.5

11

More Example Graphs


Use graph bar to make bar
graphs
Average July and January temperatures
80

by regions of the United States


81.0
73.5

72.1

46.1

46.2

27.9
21.7

Degrees Fahrenheit
20
40
60

73.3

N.E.

N. Central
July

Source: U.S. Census Bureau, U.S. Dept. of Commerce

South
January

West

More Example Graphs


Use graph combine to combine 3 graphs into one:

Life expectancy at birth vs. GNP per capita


Fraction

.1

0 2.5

2.5

loggnp
3.5

4.5

4.5

.2

.15

.1
.05
Fraction

0 55 60 65 70 75 80
Life expectancy at birth

.2

Life expectancy at birth


55 60 65 70 75 80

.3

Source: 1998 data from The World Bank Group

3.5
loggnp

More Example Graphs


Graph matrix is a great alternative to a correlation
matrix to investigate relationships between
variables
Correlations among 1998 life-expectancy data
50

60

70

80

20

40

60

80

3
2

Avg.
annual %
growth

1
0
-1

80
70
60

Life
expectancy
at birth

50

12

Log GNP
per
capita

10
8
6

100
80
60

safewater

40
20
-1
0
1
2
3
Source: The World Bank Group

100

10

12

More Example Graphs


Get data labels (called marker labels in Stata) from
the values of another variable
Life expectancy vs. GNP per capita

55

Life expectancy at birth (years)


60
65
70
75

80

North, Central, and South America


Canada
United States
Jamaica

Chile
Panama
Uruguay
Venezuela
Trinidad
Mexico

Dominican Republic
Ecuador
Para Colombia
Honduras
El Salvador
Peru
Nicaragua

Argentina

Brazil

Guatemala
Bolivia

Haiti

.5

5
10
GNP per capita (thousands of dollars)

Data source: World Bank, 1998

15

20 25 30

More Example Graphs


Xtline from a panel data set can overlay lines for
each value of panel variable.
Calories Consumed by Subject

3500

Calories consumed
4000
4500

5000

Jan 1 2002 - Jan 1 2003

01jan2002

01apr2002

01jul2002
Date
Tess
Arnold

01oct2002
Sam

01jan2003

Macros
Macros come in two general types:
1. Globals

Exist until Stata is closed

2. Locals

Exist until the end of the do file

Other types of macros exist, but are


rarely used

global vs. local


. global names "Ballav Nick ChongMing Joe David"
. local names2

"Jake Steven Jose Tyrell Martin"

. di "`names2'"
Jake Steven Jose Tyrell Martin
. di "$names"
Ballav Nick ChongMing Joe David
.
end of do-file
. di "`names2'"

. di "$names"
Ballav Nick ChongMing Joe David

Creating the
global
Creating the
local
- References to locals
have to
be enclosed in single
References to globals
quotes
have to
begin with a $
End of the do file
The local no longer exists
Conversely, the global
still exists

When do we need for


loops?
If a STATA program involves
repetitive actions on a group of
variables, files, or other items
Examples
Creating new variables
Recoding missing values on a list of
variables
Merging multiple datasets
Labeling variables

Determining what macros already


exist
. macro list
names:
S_level:
F1:
F2:
F7:
F8:
S_ADO:
S_StataSE:
S_FLAVOR:
S_OS:
S_OSDTL:
S_MACH:
_names2:

Ballav Nick ChongMing Joe David


95
help advice;
describe;
save
use
UPDATES;BASE;SITE;.;PERSONAL;PLUS;OLDPLACE
SE
Intercooled
Windows
64-bit
PC (64-bit x86-64)
Jake Steven Jose Tyrell Martin

The local we
created
General macros
automatically
created by Stata
The global we
created

Foreach
Syntax of foreach command
foreach lname {in|of varilist} variables {
commands referring to `lname'

}
The open brace must appear on the same line
as the foreach;
Nothing may follow the open brace except, of
course, comments; the first command to be
executed must appear on a new line;
The close brace must appear on a line by itself

Diferences in Using -in- option and -of


varlist- option in the -foreach- command
foreach i in variable1-variable5 {
Stata commands
}
There is only one variable called variable1-variable5

foreach i of varlist variable1-variable5 {


Stata commands
}
There are five variables, including variable1 through
variable5

Stata commands in recoding


variables
Without using
the
foreach
command

foreach command
with "in" option

foreach command
with "of varlist" option

foreach x in v1 v2 v3
v4 {
recode `x' (99 = .)
}

foreach x of varlist v1v4 {


recode `x' (99 = .)
}

recode v1 (99
= .)
recode v2 (99
= .)
recode v3 (99
= .)
recode v4 (99
= .)

Using macros to store variable


names
. global ind_vars = "age iq gender weight anxiety"
. reg depress $ind_vars
Source

SS

df

MS

Model
Residual

14.1382227
22.587052

5 2.82764454
85 .265730024

Total

36.7252747

90 .408058608

depress

Coef.

age
iq
gender
weight
anxiety
_cons

-.0193698
-.0093197
-.240945
-.0188543
.5563893
2.332141

Std. Err.
.0137039
.0130535
.1438568
.0229981
.0831225
1.466546

t
-1.41
-0.71
-1.67
-0.82
6.69
1.59

Global for ind.


vars
Number of obs
F( 5,
85)
Prob > F
R-squared
Adj R-squared
Root MSE

P>|t|
0.161
0.477
0.098
0.415
0.000
0.115

=
=
=
=
=
=

91
10.64
0.0000
0.3850
0.3488
.51549

[95% Conf. Interval]


-.0466168
-.0352736
-.526971
-.0645807
.3911195
-.5837454

.0078771
.0166341
.0450809
.0268721
.7216592
5.248027

Global for ind.


vars
. reg depress $ind_vars sleep satlife
Source

SS

df

MS

Model
Residual

22.2722042
13.3233014

7
81

3.18174346
.164485202

Total

35.5955056

88

.404494382

depress

Coef.

age
iq
gender
weight
anxiety
sleep
satlife
_cons

-.0252532
-.0212878
-.0288896
-.017562
.3652071
-.6100973
-.4784158
4.336996

Std. Err.
.0109484
.0103962
.1233419
.0181686
.074345
.1435988
.1009435
1.192267

t
-2.31
-2.05
-0.23
-0.97
4.91
-4.25
-4.74
3.64

Number of obs
F( 7,
81)
Prob > F
R-squared
Adj R-squared
Root MSE

P>|t|
0.024
0.044
0.815
0.337
0.000
0.000
0.000
0.000

=
=
=
=
=
=

89
19.34
0.0000
0.6257
0.5934
.40557

[95% Conf. Interval]


-.0470371
-.041973
-.2743013
-.0537117
.2172839
-.8958139
-.6792618
1.964759

-.0034692
-.0006025
.216522
.0185878
.5131304
-.3243807
-.2775698
6.709233

Running Parallel lists with


macros
. **** running parrallel lists ****
. local 1 "cat dog cow pig"
. local 2 "meow woof moo oinkoink"
. local n : word count `1'
.
. forvalues i = 1/`n' {
2.
local a : word `i' of `1'
3.
local b : word `i' of `2'
4.
di "`a' says `b'"
5.
}
cat says meow
dog says woof
cow says moo
pig says oinkoink

Results

Create a local called 1


Create local called
2
Create macro 3 = # of
words in
macro
1
Extracting word `I from local
1
Extracting word `I from local
2
Using the new
locals in a display
command with
other text

Creating a program in Stata


Command name Program name

First command to be run


when the program is
implemented

. program printit
1. display "Listing the values of four variables"
2. list make price mpg foreign
3. end

Telling Stata that


there are no
more commands
to be used as
part of the
program

Second command to be run


when the program is
implemented

.
. printit
Listing the

1.
2.
3.
4.
5.

values

of

four

variables

make

price

mpg

foreign

AMC Concord
AMC Pacer
AMC Spirit
Buick Century
Buick Electra

4,099
4,749
3,799
4,816
7,827

22
17
22
20
15

Domestic
Domestic
Domestic
Domestic
Domestic

5,788
4,453
5,189
10,372
4,082

18
26
20
16
19

Domestic
Domestic
Domestic
Domestic
Domestic

11,385
14,500
15,906
3,299
5,705

14
14
21
29
16

Domestic
Domestic
Domestic
Domestic
Domestic

4,504
5,104
3,667
3,955
3,984

22
22
24
19
30

Domestic
Domestic
Domestic
Domestic
Domestic

4,010
5,886
6,342
4,389
4,187

18
16
17
28
21

Domestic
Domestic
Domestic
Domestic
Domestic

11,497
13,594
13,466
3,829
5,379

12
12
14
22
14

Domestic
Domestic
Domestic
Domestic
Domestic

6.
7.
8.
9.
10.

Buick
Buick
Buick
Buick
Buick

LeSabre
Opel
Regal
Riviera
Skylark

11.
12.
13.
14.
15.

Cad. Deville
Cad. Eldorado
Cad. Seville
Chev. Chevette
Chev. Impala

16.
17.
18.
19.
20.

Chev.
Chev.
Chev.
Chev.
Dodge

21.
22.
23.
24.
25.

Dodge Diplomat
Dodge Magnum
Dodge St. Regis
Ford Fiesta
Ford Mustang

26.
27.
28.
29.
30.

Linc.
Linc.
Linc.
Merc.
Merc.

31.
32.
33.
34.
35.

Merc. Marquis
Merc. Monarch
Merc. XR-7
Merc. Zephyr
Olds 98

6,165
4,516
6,303
3,291
8,814

15
18
14
20
21

Domestic
Domestic
Domestic
Domestic
Domestic

36.
37.
38.
39.
40.

Olds
Olds
Olds
Olds
Olds

5,172
4,733
4,890
4,181
4,195

19
19
18
19
24

Domestic
Domestic
Domestic
Domestic
Domestic

41.
42.
43.
44.
45.

Olds Toronado
Plym. Arrow
Plym. Champ
Plym. Horizon
Plym. Sapporo

10,371
4,647
4,425
4,482
6,486

16
28
34
25
26

Domestic
Domestic
Domestic
Domestic
Domestic

46.
47.
48.
49.

Plym.
Pont.
Pont.
Pont.

4,060
5,798
4,934
5,222

18
18
18
19

Domestic
Domestic
Domestic
Domestic

Malibu
Monte Carlo
Monza
Nova
Colt

Continental
Mark V
Versailles
Bobcat
Cougar

Cutl Supr
Cutlass
Delta 88
Omega
Starfire

Volare
Catalina
Firebird
Grand Prix

Invoke the
program by
simply typing
the program
name and then
running in
Stata.

Results

SVYset and SVY Prefix

Simple vs. Complex


Sample
Many Statistical techniques assume
simple random sample
Simple random sampleeach
element of the sample has equal
probability of being sampled.

Complex Survey
Sampling weights
inverse probability of being sampled
represent weight elements in the population

Clustering
groups sampled together
primary sampling units (PSU) -- first level
clusters

Stratification
groups of clusters strata
strata sampled separately

Example
States, Counties, Schools, Students
sample states in diferent regions
sample counties within each state
sample schools within each county
sample students from schools

svyset
svyset psu? [pweight=?] , strata =
(?) fpc(?)
|| psu?, fpc(?)
psu = primary sampling unit
pweight = probability weight
fpc = finite population correction (total #
of
stratus or clusters PSU is sampled
from)
|| = next stage

SVYSET Examples

use http://www.stata-press.com/data/r12/multistage
svyset county [pw=sampwgt], strata(state) fpc(ncounties) ||
school, fpc(nschools)
save highschool
use highschool
svyset

SVY Prefix Examples

svy: proportion race sex


svy: tab race sex, ci
svy: tab race sex, count ci
svy, subpop(if sex==1): mean weight
height
svy, subpop(if sex==2): mean weight
height, over (race)
svy: reg weight sex
Note: subpop is preferred over if statement as stata will include all cases for estimating standard
errors

Take-home Message
Ask what sampling design for your
data before running analysis.
If complex survey data, consider
svyset or multilevel modeling.

xtset and xtprefix

xtsetDeclare Panel Data


xtset panelvar
specify unit observed repeatedly
xtset panelvar timevar [, tsoptions] specify time var
xtset

display current

xtset

xtset, clear

clear

xtset

Menu
Statistics > Longitudinal/panel data > Setup and utilities > Declare dataset to be panel
data

Time-Unit Options
[unitoptions]

specify

units of time

clocktime, daily, weekly, monthly, quarterly,


halfyearly, yearly

[deltaoption]

specify duration between

observations

delta (#)
delta (exp)
delta (# units)
days)

e.g.

deta (2)
delta (7*24)
delta (10 min)/(7

Xtdescribepattern of xt
data
xtdescribe [if] [in] [, options]
[,opti ons]
patterns(#)
e.g.

p(10) --

display max. 10

width(#)

w(80) --

display 80 columns

Menu
Statistics > Longitudinal/panel data > Setup and utilities >
Describe pattern of xt data

Examples
use http://www.statapress.com/data/r12/nlswork
xtset
Browse
xtdes, p(20)
xtsum hours
xttab race
xtreg ln_w grade age ttl_exp tenure south,
mle

Post Estimation in STATA

Generating variables with fitted values

After a regression, use the predict newvar syntax to create a new variable,
that contains the fitted values for each observation.
If the model is fitted only for a limited sample, use the following syntax to
get the predicted value for that sample

Generating variables with residuals

After a regression, use the predict newvar, r syntax to create a new variable,
that contains the residuals for each observation.

Reformat and write regression tables to


document files

Outreg command can be used to reformat and write regression tables to document files
Example

Outreg has lots of options that lets us customize the look of the output table.

Margins

Margins can be useful to understand regression results


Example

In the above regression, the coefficient on weight is misleading as an increase in weight affects both
weight and weight squared. So, the total effect depends on the starting value of weight.
The following command will set the variables to their means and find the derivative of expected price with
respect to weight at that point.

Marginsplot
Often, the results from margins can be hard to read as in the following example.
The command marginsplot can be used to visualize the results and understand them better.
Example

E ffe c ts o n P r(D ia b e tes )


.02
.04
.06
.08

Average Marginal Effects

20

30

40

50
age in years

1.black

60
1.female

70

Using saved results

Stata stores results from a command in various forms scalar, string, matrices etc. Such results are called
returned results
Returned results can be used to make other computations in STATA
We can type return list after we run a command to see what the returned results
Example

We can use the returned results as variables and perform computations


Example gen range = r(max) r(min)

Using saved results contd

Results are stored mainly as r() class or e() class depending on the commands used
Access r() class results return list, access e() class results ereturn list
Matrices in returned results can be used as regular matrices.
Example :

More advanced computations with matrices can be done in MATA which is a matrix language built into
STATA.

Post estimation statistics

estat ic
Available only after commands that report log likelihood
Given two models, the one with the smaller AIC and BIC values fits the data better

estat vce
- displays the covariance matrix estimates

Postfile

Results can be stored into a STATA dataset using the postfile command
This can be useful when we have to run a lot of regressions, for example - monte carlo simulations.
Lets consider an example from the STATA manual

Suppose we want the means and variances from 10,000 randomly constructed 100-observation samples
of data and store the results in results.dta
We could do that as follows (refer to the do file)

S-ar putea să vă placă și