Vous êtes sur la page 1sur 25

Introduction

New Graphical Tools


Conclusion

Graphics tricks for models


Bill Rising
StataCorp LP

2011 Stata Conference


Chicago, IL
July 15, 2011

Bill Rising

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Goals

Goals

Show two new Stata graphics tools


Plotting marginal effects and predictive margins using
marginsplot
Making contour plots via twoway contour and twoway
contourline

Bill Rising

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Goals

Getting Started

This will be an interactive demonstration


This will help show some of the new interface as well as some
other new tools

Start by opening up the nhanes2 dataset


. webuse nhanes2
Good for continuation from yesterday

These are survey data


. svyset
We will need to use the svy: prefix for estimation

Bill Rising

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

Pros and Cons of margins

The margins command is great!


Can easily compute averaged predicted values
Can easily compute average marginal effects
Both of these make tables of values

Curse the margins command!


The tabular output can be difficult to read

Bill Rising

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

A Simple Regression with a Quadratic

Here is a simple model for BMI as a function of age and sex


. svy: regress bmi c.age##c.age i.sex

Can see that the parabola is concave down, and that women
have smaller BMIs then men

Bill Rising

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

Taking a Look at Predictive Margins

We can take a look at the predictive margins at several ages


within each sex
. margins, at(age==(25(10)75)) over(sex)
Note: Because of using svy, we should really be specifying
vce(unconditional) for all these examples; this is being left off
to keep the commands short

It looks like the differences between sexes are constant (as


expected)
As one would expect from the concave-down parabola the BMIs
increase and then decrease

Bill Rising

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

Visualizing the Predictive Margins


We can get a much better look at the margins by using the
marginsplot command
Here it is in its simplest form
. marginsplot

23

24

Linear Prediction
25
26

27

Predictive Margins with 95% CIs

25

35

45

55

65

75

age in years
Male

Bill Rising

Female

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

Default Behavior

The default behavior is to


Draw a connected-line plot
Draw pointwise confidence intervals at each point

Stata is bright enough to use the at() variable for the x-axis and
to overlay the two curves

Bill Rising

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

Switching Axes
We can change this default behavior, of course
Here are the same data in a less-useful form (though it shows the
constant offset for the females)
. marginsplot, x(sex)

23

Linear Prediction
24
25
26

27

Predictive Margins with 95% CIs

Male

Female
1=male, 2=female
age=25
age=45
age=65

Bill Rising

age=35
age=55
age=75

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

Making a Finer Grid


We can also make a finer grid for the graph by giving more age
points
Well also turn off the CIs here
. quietly margins, at(age==(25(5)75)) over(sex)
. marginsplot, noci

23

24

Linear Prediction
25
26

27

Predictive Margins

25

30

35

40

45

50
55
age in years

Male

Bill Rising

60

65

70

75

Female

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

Average Marginal Effects

We can also look at average marginal effects


. quietly margins, at(age==(25(5)75)) dydx(*)
. marginsplot, noci

.4

Effects on Linear Prediction


.2
0

.2

Conditional Marginal Effects

25

30

35

40

45

50
55
age in years
age

Bill Rising

60

65

70

75

2.sex

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

Looking at a Logit

Working with something which is non-linear in the natural (not


the model) measure is a bit more interesting
Here is a simple logistic regression
. svy: logistic diabetes age i.(race sex)

Bill Rising

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

The Predictive Margins are More Interesting


Now the predictive margins are more interesting
. quietly margins, at(age==(25(5)75)) over(race sex)
. marginsplot, noci

Pr(Diabetes)
.1
.2

.3

Predictive Margins

25

30

35

40

45

50
55
age in years

White, Male
Black, Male
Other, Male

Bill Rising

60

65

70

75

White, Female
Black, Female
Other, Female

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

Now for Something Complicated

Well now fit a model with (too) many interactions


. svy: logit diabetes c.age##c.age##race##sex

Looking at the interaction terms is nearly worthless


This is probably getting close to overfitting

Bill Rising

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

Predictive Margins, One Last Time

Here are the same set of margins applied to this different model
. quietly margins, at(age=(20(5)75)) over(race sex)
. marginsplot, noci

Bill Rising

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

Contour Plots

Stata 12 now has both filled contour plots and contour line plots
These are both twoway plots
twoway contour for filled contour plots
twoway contourline for contour line plots

They are in the twoway dialog

Bill Rising

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

A Simple Example Dataset

Here is a dataset meant to be artificial mountains


. use mtns

Take a look at a few observations


. list in 1/10

Bill Rising

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

Making a Filled Contour


Here is the default filled contour
4

. twoway contour z y x

northing
2

1.5

.5

height in thousands of meters

2.5

0
0

2
easting

Note: If this displays with line artifacts, turn off anti-aliasing in


your pdf viewerthis is a known limitation of pdfs

Bill Rising

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

Making the Contour Prettier

Adding many levels smooths the color gradations


. twoway contour z y x, levels(40)

This makes the legend a bit absurd, though


We should shut off the legend and change the aspect ratio

Bill Rising

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

A Pretty Contour Plot


This looks quite good

northing
2

. twoway contour z y x, levels(40) clegend(off) xsize(5) ysize(

Bill Rising

2
easting

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

A Contour Line Plot

Contour line plots outline elevations instead of filling them in


They work best with color lines on

northing
2

. twoway contourline z y x, levels(40) colorlines plegend(off)

Bill Rising

2
easting

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

What If There is No Grid?

The artificial mountain dataset was defined on a grid


Both twoway contour and twoway contourline will use
interpolation to fill the rest of the plot region if the data are not
on a grid
Lets look at this Excel file about a sandstone stratum under Ohio
First well bring it into Stata
. import excel using sandstone, firstrow

Looking at it, there are gaps in the grid


. scatter northing easting

Bill Rising

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

We Can Still Make a Contour Plot


We can still make a contour plot from this
90000

. twoway contour depth northing easting


8,100

80000

8,000

70000

7,800

7,700

60000

7,600

30000

35000

40000
easting

Bill Rising

45000

50000

Graphics tricks for models

depth

northing

7,900

Introduction
New Graphical Tools
Conclusion

Visualizing Margins
Contour Plots

Taking A Look at Help

We should take a look at the help for contour plots


The help files have been spruced up, so that we can skip to the
Remarks right away!

Bill Rising

Graphics tricks for models

Introduction
New Graphical Tools
Conclusion

Conclusion

Just For Fun


Believe it or not, it is possible to make a contour plot of
predictive margins
. do margcon2

80

Here is the picture


.5
.45

age in years
40

.35
.3
.25
.2
.15
.1

20

.05
0
10

20

30
40
Body Mass Index (BMI)

Bill Rising

50

60

Graphics tricks for models

Pr(diabetes), predict()

60

.4

Vous aimerez peut-être aussi